Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't use cached traits to initialize sdk state #282

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading