Skip to content

Commit

Permalink
fix: Don't use cached traits to initialize sdk state (#282)
Browse files Browse the repository at this point in the history
* fix: Don't use cached traits to initialize sdk state

* remove log

* fix tests

* should not compare traits changes with local storage

* remove traits tests
  • Loading branch information
tiagoapolo authored Jan 17, 2025
1 parent 4afc3bf commit 94bcd3e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 63 deletions.
4 changes: 1 addition & 3 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,15 @@ const Flagsmith = class {
}
if (setState) {
cachePopulated = true;
traitsChanged = getChanges(this.evaluationContext.identity?.traits, json.evaluationContext?.identity?.traits)
flagsChanged = getChanges(this.flags, json.flags)
// When populating state from cache, we merge traits passed in flagsmith.init
this.setState({
...json,
evaluationContext: toEvaluationContext({
...json.evaluationContext,
identity: json.evaluationContext?.identity ? {
...json.evaluationContext?.identity,
traits: {
...json.evaluationContext?.identity?.traits || {},
// Traits passed in flagsmith.init will overwrite server values
...traits || {},
}
} : undefined,
Expand Down
59 changes: 0 additions & 59 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,63 +334,4 @@ describe('Cache', () => {
},
);
});
test('should merge any newly passed traits with cache', async () => {
const onChange = jest.fn();
const { flagsmith, initConfig, AsyncStorage } = getFlagsmith({
onChange,
cacheFlags: true,
preventFetch: true,
});
const storage = new SyncStorageMock();
await storage.setItem(FLAGSMITH_KEY, JSON.stringify({
...identityState,
}));
const ts = Date.now();
await flagsmith.init({
...initConfig,
traits: { ts },
AsyncStorage: storage,
});
expect(flagsmith.getAllTraits()).toEqual({
//@ts-ignore
...identityState.traits,
ts,
});
});
test('should cache transient traits correctly', async () => {
const onChange = jest.fn();
const testIdentityWithTransientTraits = 'test_identity_with_transient_traits'
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
cacheFlags: true,
identity: testIdentityWithTransientTraits,
traits: {
transient_trait: {
value: 'Example',
transient: true,
}
},
onChange,
});
mockFetch.mockResolvedValueOnce({status: 200, text: () => fs.readFile(`./test/data/identities_${testIdentityWithTransientTraits}.json`, 'utf8')})
await flagsmith.init(initConfig);
expect(onChange).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(getStateToCheck(flagsmith.getState())).toEqual({
...identityState,
evaluationContext: {
...identityState.evaluationContext,
identity: {
...identityState.evaluationContext.identity,
identifier: testIdentityWithTransientTraits,
traits: {
...identityState.evaluationContext.identity.traits,
transient_trait: {
transient: true,
value: 'Example',
},
},
},
},
})
});
});
11 changes: 10 additions & 1 deletion test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,16 @@ describe('Flagsmith.init', () => {
await AsyncStorage.setItem(FLAGSMITH_KEY, JSON.stringify(identityState));
await flagsmith.init(initConfig);

expect(getStateToCheck(flagsmith.getState())).toEqual(identityState);
expect(getStateToCheck(flagsmith.getState())).toEqual({
...identityState,
evaluationContext: {
...identityState.evaluationContext,
identity: {
...identityState.evaluationContext.identity,
traits: {},
},
},
});

await waitFor(() => {
expect(onError).toHaveBeenCalledTimes(1);
Expand Down

0 comments on commit 94bcd3e

Please sign in to comment.