Skip to content

Commit 4f4f13d

Browse files
[FSSDK-9492] [React] Fetch segments in setuser (#205)
* Add segment call to setuser * Fix fetchqualifiedseqment signature * Add fetchsegments on setuser test * Add await to relevent setuser tests * Allow null userid on setUser * Add default user * Fix defaultuser refference error * Return success == false on failed segments fetch * Update tests * Update segment messages * Refactor error messages
1 parent cfebb2f commit 4f4f13d

File tree

2 files changed

+109
-64
lines changed

2 files changed

+109
-64
lines changed

src/client.spec.ts

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,24 @@ describe('ReactSDKClient', () => {
143143
});
144144

145145
it('fulfills the returned promise with success: true when a user is set', async () => {
146-
instance.setUser({
146+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);
147+
await instance.setUser({
147148
id: 'user12345',
148149
});
149150
const result = await instance.onReady();
150151
expect(result.success).toBe(true);
151152
});
152153

154+
it('fulfills the returned promise with success: false when fetchqualifiedsegment is false', async () => {
155+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => false);
156+
157+
await instance.setUser({
158+
id: 'user12345',
159+
});
160+
const result = await instance.onReady();
161+
expect(result.success).toBe(false);
162+
});
163+
153164
describe('if Optimizely client is null', () => {
154165
beforeEach(() => {
155166
// Mocks dataReadyPromise value instead of _client = null because test initialization of instance causes dataReadyPromise to return { success: true }
@@ -164,7 +175,7 @@ describe('ReactSDKClient', () => {
164175
});
165176

166177
it('fulfills the returned promise with success: false when a user is set', async () => {
167-
instance.setUser({
178+
await instance.setUser({
168179
id: 'user12345',
169180
});
170181
const result = await instance.onReady();
@@ -178,7 +189,7 @@ describe('ReactSDKClient', () => {
178189
resolveInnerClientOnReady = res;
179190
});
180191
mockInnerClientOnReady.mockReturnValueOnce(mockReadyPromise);
181-
instance.setUser({
192+
await instance.setUser({
182193
id: 'user999',
183194
});
184195
resolveInnerClientOnReady!({ success: true });
@@ -194,7 +205,8 @@ describe('ReactSDKClient', () => {
194205
resolveInnerClientOnReady = res;
195206
});
196207
mockInnerClientOnReady.mockReturnValueOnce(mockReadyPromise);
197-
instance.setUser({
208+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);
209+
await instance.setUser({
198210
id: 'user999',
199211
});
200212
resolveInnerClientOnReady!({ success: true });
@@ -204,9 +216,9 @@ describe('ReactSDKClient', () => {
204216
});
205217

206218
describe('setUser', () => {
207-
it('updates the user object with id and attributes', () => {
219+
it('updates the user object with id and attributes', async () => {
208220
const instance = createInstance(config);
209-
instance.setUser({
221+
await instance.setUser({
210222
id: 'xxfueaojfe8&86',
211223
attributes: {
212224
foo: 'bar',
@@ -220,11 +232,11 @@ describe('ReactSDKClient', () => {
220232
});
221233
});
222234

223-
it('adds and removes update handlers', () => {
235+
it('adds and removes update handlers', async () => {
224236
const instance = createInstance(config);
225237
const onUserUpdateListener = jest.fn();
226238
const dispose = instance.onUserUpdate(onUserUpdateListener);
227-
instance.setUser({
239+
await instance.setUser({
228240
id: 'newUser',
229241
});
230242
expect(onUserUpdateListener).toBeCalledTimes(1);
@@ -233,17 +245,32 @@ describe('ReactSDKClient', () => {
233245
attributes: {},
234246
});
235247
dispose();
236-
instance.setUser({
248+
await instance.setUser({
237249
id: 'newUser2',
238250
});
239251
expect(onUserUpdateListener).toBeCalledTimes(1);
240252
});
241253

254+
it('calls fetchqualifiedsegements internally on each setuser call', async () => {
255+
const instance = createInstance(config);
256+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);
257+
258+
await instance.setUser({
259+
id: 'xxfueaojfe8&86',
260+
});
261+
262+
await instance.setUser({
263+
id: 'xxfueaojfe8&87',
264+
});
265+
266+
expect(instance.fetchQualifiedSegments).toBeCalledTimes(2);
267+
});
268+
242269
describe('pre-set user and user overrides', () => {
243270
let instance: ReactSDKClient;
244-
beforeEach(() => {
271+
beforeEach(async () => {
245272
instance = createInstance(config);
246-
instance.setUser({
273+
await instance.setUser({
247274
id: 'user1',
248275
attributes: {
249276
foo: 'bar',
@@ -898,7 +925,7 @@ describe('ReactSDKClient', () => {
898925
});
899926

900927
describe('if Optimizely client is null', () => {
901-
it('does not return an object with variables of all types returned from the inner sdk ', () => {
928+
it('does not return an object with variables of all types returned from the inner sdk ', async () => {
902929
(mockInnerClient.getOptimizelyConfig as jest.Mock).mockReturnValue({
903930
featuresMap: {
904931
feat1: {
@@ -956,7 +983,7 @@ describe('ReactSDKClient', () => {
956983
}
957984
);
958985
const instance = createInstance(config);
959-
instance.setUser({
986+
await instance.setUser({
960987
id: 'user1123',
961988
});
962989
// @ts-ignore
@@ -966,7 +993,7 @@ describe('ReactSDKClient', () => {
966993
});
967994
});
968995

969-
it('returns an object with variables of all types returned from the inner sdk ', () => {
996+
it('returns an object with variables of all types returned from the inner sdk ', async () => {
970997
(mockInnerClient.getOptimizelyConfig as jest.Mock).mockReturnValue({
971998
featuresMap: {
972999
feat1: {
@@ -1024,7 +1051,7 @@ describe('ReactSDKClient', () => {
10241051
}
10251052
);
10261053
const instance = createInstance(config);
1027-
instance.setUser({
1054+
await instance.setUser({
10281055
id: 'user1123',
10291056
});
10301057
const result = instance.getFeatureVariables('feat1');
@@ -1042,7 +1069,7 @@ describe('ReactSDKClient', () => {
10421069

10431070
describe('getAllFeatureVariables', () => {
10441071
describe('if Optimizely client is null', () => {
1045-
it('does not return an object with variables of all types returned from the inner sdk ', () => {
1072+
it('does not return an object with variables of all types returned from the inner sdk ', async () => {
10461073
const anyClient = mockInnerClient.getAllFeatureVariables as jest.Mock;
10471074
anyClient.mockReturnValue({
10481075
bvar: true,
@@ -1056,14 +1083,14 @@ describe('ReactSDKClient', () => {
10561083
const instance = createInstance(config);
10571084
// @ts-ignore
10581085
instance._client = null;
1059-
instance.setUser({
1086+
await instance.setUser({
10601087
id: 'user1123',
10611088
});
10621089
const result = instance.getAllFeatureVariables('feat1', 'user1');
10631090
expect(result).toEqual({});
10641091
});
10651092

1066-
it('cannot use pre-set and override user for getAllFeatureVariables', () => {
1093+
it('cannot use pre-set and override user for getAllFeatureVariables', async () => {
10671094
const mockFn = mockInnerClient.getAllFeatureVariables as jest.Mock;
10681095
mockFn.mockReturnValue({
10691096
bvar: true,
@@ -1077,7 +1104,7 @@ describe('ReactSDKClient', () => {
10771104
const instance = createInstance(config);
10781105
// @ts-ignore
10791106
instance._client = null;
1080-
instance.setUser({
1107+
await instance.setUser({
10811108
id: 'user1',
10821109
attributes: {
10831110
foo: 'bar',
@@ -1096,7 +1123,7 @@ describe('ReactSDKClient', () => {
10961123
expect(result).toEqual({});
10971124
});
10981125

1099-
it('returns an object with variables of all types returned from the inner sdk ', () => {
1126+
it('returns an object with variables of all types returned from the inner sdk ', async () => {
11001127
const anyClient = mockInnerClient.getAllFeatureVariables as jest.Mock;
11011128
anyClient.mockReturnValue({
11021129
bvar: true,
@@ -1108,7 +1135,7 @@ describe('ReactSDKClient', () => {
11081135
},
11091136
});
11101137
const instance = createInstance(config);
1111-
instance.setUser({
1138+
await instance.setUser({
11121139
id: 'user1123',
11131140
});
11141141
const result = instance.getAllFeatureVariables('feat1', 'user1');
@@ -1123,7 +1150,7 @@ describe('ReactSDKClient', () => {
11231150
});
11241151
});
11251152

1126-
it('can use pre-set and override user for getAllFeatureVariables', () => {
1153+
it('can use pre-set and override user for getAllFeatureVariables', async () => {
11271154
const mockFn = mockInnerClient.getAllFeatureVariables as jest.Mock;
11281155
mockFn.mockReturnValue({
11291156
bvar: true,
@@ -1135,7 +1162,7 @@ describe('ReactSDKClient', () => {
11351162
},
11361163
});
11371164
const instance = createInstance(config);
1138-
instance.setUser({
1165+
await instance.setUser({
11391166
id: 'user1',
11401167
attributes: {
11411168
foo: 'bar',
@@ -1200,7 +1227,7 @@ describe('ReactSDKClient', () => {
12001227
});
12011228

12021229
it('should return false if fetch fails', async () => {
1203-
instance.setUser({
1230+
await instance.setUser({
12041231
id: 'user1',
12051232
});
12061233

@@ -1211,7 +1238,7 @@ describe('ReactSDKClient', () => {
12111238
});
12121239

12131240
it('should return true if fetch successful', async () => {
1214-
instance.setUser({
1241+
await instance.setUser({
12151242
id: 'user1',
12161243
});
12171244

@@ -1224,9 +1251,9 @@ describe('ReactSDKClient', () => {
12241251

12251252
describe('onForcedVariationsUpdate', () => {
12261253
let instance: ReactSDKClient;
1227-
beforeEach(() => {
1254+
beforeEach(async () => {
12281255
instance = createInstance(config);
1229-
instance.setUser({
1256+
await instance.setUser({
12301257
id: 'xxfueaojfe8&86',
12311258
attributes: {
12321259
foo: 'bar',
@@ -1278,10 +1305,10 @@ describe('ReactSDKClient', () => {
12781305
});
12791306

12801307
describe('if Optimizely client is null', () => {
1281-
it('should return false', () => {
1308+
it('should return false', async () => {
12821309
// @ts-ignore
12831310
instance._client = null;
1284-
instance.setUser({
1311+
await instance.setUser({
12851312
id: 'user1',
12861313
});
12871314
const mockFn = mockOptimizelyUserContext.removeAllForcedDecisions as jest.Mock;
@@ -1295,8 +1322,8 @@ describe('ReactSDKClient', () => {
12951322
});
12961323
});
12971324

1298-
it('should return true if user context has been set ', () => {
1299-
instance.setUser({
1325+
it('should return true if user context has been set ', async () => {
1326+
await instance.setUser({
13001327
id: 'user1',
13011328
});
13021329
const mockFn = mockOptimizelyUserContext.removeAllForcedDecisions as jest.Mock;
@@ -1312,9 +1339,9 @@ describe('ReactSDKClient', () => {
13121339

13131340
describe('setForcedDecision', () => {
13141341
let instance: ReactSDKClient;
1315-
beforeEach(() => {
1342+
beforeEach(async () => {
13161343
instance = createInstance(config);
1317-
instance.setUser({
1344+
await instance.setUser({
13181345
id: 'user1',
13191346
attributes: {
13201347
foo: 'bar',
@@ -1421,9 +1448,9 @@ describe('ReactSDKClient', () => {
14211448

14221449
describe('removeForcedDecision', () => {
14231450
let instance: ReactSDKClient;
1424-
beforeEach(() => {
1451+
beforeEach(async () => {
14251452
instance = createInstance(config);
1426-
instance.setUser({
1453+
await instance.setUser({
14271454
id: 'user1',
14281455
attributes: {
14291456
foo: 'bar',

0 commit comments

Comments
 (0)