diff --git a/flagsmith-core.ts b/flagsmith-core.ts index 706710f..a551c54 100644 --- a/flagsmith-core.ts +++ b/flagsmith-core.ts @@ -454,9 +454,7 @@ 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({ @@ -464,7 +462,7 @@ const Flagsmith = class { identity: json.evaluationContext?.identity ? { ...json.evaluationContext?.identity, traits: { - ...json.evaluationContext?.identity?.traits || {}, + // Traits passed in flagsmith.init will overwrite server values ...traits || {}, } } : undefined, diff --git a/test/cache.test.ts b/test/cache.test.ts index 46998f3..26f9a77 100644 --- a/test/cache.test.ts +++ b/test/cache.test.ts @@ -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', - }, - }, - }, - }, - }) - }); }); diff --git a/test/init.test.ts b/test/init.test.ts index e4f2948..8aecfb6 100644 --- a/test/init.test.ts +++ b/test/init.test.ts @@ -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);