Skip to content

Commit a030d57

Browse files
authored
Merge pull request #245 from Flagsmith/feat/merge-cached-flagsmith-traits
fix: Merge flagsmith.init traits with cached values
2 parents 69209a3 + 907964b commit a030d57

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

flagsmith-core.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,14 @@ const Flagsmith = class {
421421
cachePopulated = true;
422422
traitsChanged = getChanges(this.traits, json.traits)
423423
flagsChanged = getChanges(this.flags, json.flags)
424-
this.setState(json);
424+
// When populating state from cache, we merge traits passed in flagsmith.init
425+
this.setState({
426+
...json,
427+
traits: ({
428+
...(json.traits||{}),
429+
...(traits||{})
430+
})
431+
});
425432
this.log("Retrieved flags from cache", json);
426433
}
427434
}

lib/flagsmith-es/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "4.1.0",
3+
"version": "4.1.1",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/cache.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,24 @@ describe('Cache', () => {
165165
...defaultStateAlt,
166166
});
167167
});
168+
test('should not get flags from API when skipAPI is set', async () => {
169+
const onChange = jest.fn();
170+
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
171+
cacheFlags: true,
172+
onChange,
173+
cacheOptions: { ttl: 1000, skipAPI: true },
174+
});
175+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
176+
...defaultStateAlt,
177+
ts: new Date().valueOf(),
178+
}));
179+
await flagsmith.init(initConfig);
180+
expect(onChange).toHaveBeenCalledTimes(1);
181+
expect(mockFetch).toHaveBeenCalledTimes(0);
182+
expect(getStateToCheck(flagsmith.getState())).toEqual({
183+
...defaultStateAlt,
184+
});
185+
});
168186
test('should validate flags are unchanged when fetched', async () => {
169187
const onChange = jest.fn();
170188
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
@@ -282,4 +300,26 @@ describe('Cache', () => {
282300
},
283301
);
284302
});
303+
test('should merge any newly passed traits with cache', async () => {
304+
const onChange = jest.fn();
305+
const { flagsmith, initConfig, AsyncStorage } = getFlagsmith({
306+
onChange,
307+
cacheFlags: true,
308+
preventFetch: true,
309+
});
310+
const storage = new SyncStorageMock();
311+
await storage.setItem('BULLET_TRAIN_DB', JSON.stringify({
312+
...identityState,
313+
}));
314+
const ts = Date.now();
315+
await flagsmith.init({
316+
...initConfig,
317+
traits: { ts },
318+
AsyncStorage: storage,
319+
});
320+
expect(flagsmith.getAllTraits()).toEqual({
321+
...identityState.traits,
322+
ts
323+
})
324+
});
285325
});

0 commit comments

Comments
 (0)