Skip to content

Commit

Permalink
Merge pull request #218 from Flagsmith/chore/add-tests
Browse files Browse the repository at this point in the history
Chore: add tests
  • Loading branch information
dabeeeenster authored Mar 20, 2024
2 parents 2a7e519 + 4646b35 commit f991d8a
Show file tree
Hide file tree
Showing 15 changed files with 13,372 additions and 3,132 deletions.
11 changes: 0 additions & 11 deletions .babelrc

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Pull Requests

on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]


jobs:
package:
runs-on: ubuntu-latest
name: Test

steps:
- name: Cloning repo
uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '18.x'

- run: npm i
- run: npm run build
- run: npm test
7 changes: 7 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
'@babel/preset-react',
],
};
19 changes: 10 additions & 9 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ const Flagsmith = class {
cacheOptions,
angularHttpClient,
_trigger,
_triggerLoadingStateChange,
_triggerLoadingState,
}: IInitConfig) {

return new Promise((resolve, reject) => {
Expand All @@ -373,6 +373,7 @@ const Flagsmith = class {
this.onChange = onChange;
const WRONG_FLAGSMITH_CONFIG = 'Wrong Flagsmith Configuration: preventFetch is true and no defaulFlags provided'
this._trigger = _trigger || this._trigger;
this._triggerLoadingState = _triggerLoadingState || this._triggerLoadingState;
this.onError = (message:any)=> {
this.setLoadingState({
...this.loadingState,
Expand Down Expand Up @@ -401,6 +402,7 @@ const Flagsmith = class {
}
this.enableAnalytics = enableAnalytics ? enableAnalytics : false;
this.flags = Object.assign({}, defaultFlags) || {};
this.traits = Object.assign({}, traits) || {};
this.initialised = true;
this.ticks = 10000;
if(Object.keys(this.flags).length){
Expand Down Expand Up @@ -545,7 +547,6 @@ const Flagsmith = class {
})
}


if (this.enableAnalytics) {
if (this.analyticsInterval) {
clearInterval(this.analyticsInterval);
Expand Down Expand Up @@ -599,10 +600,10 @@ const Flagsmith = class {
}
}

if (this.flags) { // retrieved flags from local storage
if (cachePopulated) { // retrieved flags from local storage
const shouldFetchFlags = !preventFetch && (!this.cacheOptions.skipAPI||!cachePopulated)
this._onChange!(null,
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits },
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
this._loadedState(null, FlagSource.CACHE, shouldFetchFlags)
);
this.oldFlags = this.flags;
Expand All @@ -629,12 +630,12 @@ const Flagsmith = class {
} else {
if (defaultFlags) {
this._onChange!(null,
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits },
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
this._loadedState(null, FlagSource.DEFAULT_FLAGS)
);
} else if (this.flags) { // flags exist due to set state being called e.g. from nextJS serverState
this._onChange?.(null,
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits },
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
this._loadedState(null, FlagSource.DEFAULT_FLAGS)
);
} else {
Expand All @@ -650,13 +651,13 @@ const Flagsmith = class {
this.getFlags(resolve, reject);
} else {
if (defaultFlags) {
this._onChange?.(null, { isFromServer: false, flagsChanged: true, traitsChanged:!!this.traits },this._loadedState(null, FlagSource.CACHE));
this._onChange?.(null, { isFromServer: false, flagsChanged: true, traitsChanged:!!this.traits && !!Object.keys(this.traits).length },this._loadedState(null, FlagSource.DEFAULT_FLAGS));
}else if (this.flags) {
let error = null
if(Object.keys(this.flags).length === 0){
error = WRONG_FLAGSMITH_CONFIG
}
this._onChange?.(null, { isFromServer: false, flagsChanged: true, traitsChanged:!!this.traits },this._loadedState(error, FlagSource.DEFAULT_FLAGS));
this._onChange?.(null, { isFromServer: false, flagsChanged: true, traitsChanged:!!this.traits && !!Object.keys(this.traits).length },this._loadedState(error, FlagSource.DEFAULT_FLAGS));

}
resolve(true);
Expand Down Expand Up @@ -752,7 +753,7 @@ const Flagsmith = class {
}
logout() {
this.identity = null;
this.traits = null;
this.traits = {};
if (this.initialised) {
return this.getFlags();
}
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: 'jest-environment-jsdom',
}
Loading

0 comments on commit f991d8a

Please sign in to comment.