Skip to content

Commit e0c9881

Browse files
committed
preserve transient traits, add test
1 parent d715329 commit e0c9881

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

flagsmith-core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ const Flagsmith = class {
112112
};
113113
});
114114
traits.forEach(trait => {
115-
userTraits[trait.trait_key.toLowerCase().replace(/ /g, '_')] = trait.trait_value
115+
userTraits[trait.trait_key.toLowerCase().replace(/ /g, '_')] = trait.transient ?
116+
{transient: true, value: trait.trait_value} : trait.trait_value
116117
});
117118

118119
this.oldFlags = { ...this.flags };

test/cache.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
testIdentity,
99
} from './test-constants';
1010
import SyncStorageMock from './mocks/sync-storage-mock';
11+
import { promises as fs } from 'fs'
1112

1213
describe('Cache', () => {
1314

@@ -282,4 +283,34 @@ describe('Cache', () => {
282283
},
283284
);
284285
});
286+
test('should cache transient traits correctly', async () => {
287+
const onChange = jest.fn();
288+
const testIdentityWithTransientTraits = 'test_identity_with_transient_traits'
289+
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
290+
cacheFlags: true,
291+
identity: testIdentityWithTransientTraits,
292+
traits: {
293+
transient_trait: {
294+
value: 'Example',
295+
transient: true,
296+
}
297+
},
298+
onChange,
299+
});
300+
mockFetch.mockResolvedValueOnce({status: 200, text: () => fs.readFile(`./test/data/identities_${testIdentityWithTransientTraits}.json`, 'utf8')})
301+
await flagsmith.init(initConfig);
302+
expect(onChange).toHaveBeenCalledTimes(1);
303+
expect(mockFetch).toHaveBeenCalledTimes(1);
304+
expect(getStateToCheck(flagsmith.getState())).toEqual({
305+
...identityState,
306+
identity: testIdentityWithTransientTraits,
307+
traits: {
308+
...identityState.traits,
309+
transient_trait: {
310+
transient: true,
311+
value: 'Example',
312+
}
313+
}
314+
})
315+
});
285316
});

test/data/identities_test_identity_with_transient_traits.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@
5555
{
5656
"trait_key": "string_trait",
5757
"trait_value": "Example"
58+
},
59+
{
60+
"trait_key": "transient_trait",
61+
"trait_value": "Example",
62+
"transient": true
5863
}
5964
]
6065
}

test/test-constants.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,6 @@ export const identityState = {
4444
off_value: { id: 80319, enabled: false, value: null },
4545
},
4646
};
47-
export const identityLocalState = {
48-
...identityState,
49-
'traits': {
50-
...identityState.traits,
51-
'transient_trait': {
52-
'value': 'Example',
53-
'transient': true,
54-
}
55-
}
56-
};
5747
export const defaultStateAlt = {
5848
...defaultState,
5949
flags: {

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export interface IFlagsmithResponse {
112112
traits?: {
113113
trait_key: string;
114114
trait_value: IFlagsmithValue;
115+
transient?: boolean;
115116
}[];
116117
flags?: {
117118
enabled: boolean;

0 commit comments

Comments
 (0)