Skip to content

Commit 261c038

Browse files
test polishing
1 parent 2fdf2f0 commit 261c038

2 files changed

Lines changed: 6 additions & 9 deletions

File tree

src/__tests__/SplitTreatments.test.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,17 @@ describe.each([
252252
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY);
253253
});
254254

255-
it('rerenders and re-evaluates feature flags if client changes.', () => {
255+
it('rerenders and re-evaluates feature flags if client changes.', async () => {
256256
wrapper.rerender(<Component names={names} attributes={attributes} splitKey={'otherKey'} />);
257-
act(() => (outerFactory as any).client('otherKey').__emitter__.emit(Event.SDK_READY));
257+
await act(() => (outerFactory as any).client('otherKey').__emitter__.emit(Event.SDK_READY));
258258

259259
// Initial render + 2 renders (in 3 updates) -> automatic batching https://reactjs.org/blog/2022/03/29/react-v18.html#new-feature-automatic-batching
260260
expect(renderTimes).toBe(3);
261261
expect(outerFactory.client().getTreatmentsWithConfig).toBeCalledTimes(1);
262262
expect(outerFactory.client('otherKey').getTreatmentsWithConfig).toBeCalledTimes(1);
263263
});
264264

265-
it('rerenders and re-evaluate splfeature flagsits when Split context changes (in both SplitFactory and SplitClient components).', async () => {
265+
it('rerenders and re-evaluate feature flags when Split context changes (in both SplitFactory and SplitClient components).', async () => {
266266
// changes in SplitContext implies that either the factory, the client (user key), or its status changed, what might imply a change in treatments
267267
const outerFactory = SplitSdk(sdkBrowser);
268268
const names = ['split1', 'split2'];
@@ -307,8 +307,6 @@ describe.each([
307307
expect(renderTimesComp1).toBe(2);
308308
expect(renderTimesComp2).toBe(2); // updateOnSdkReadyFromCache === false, in second component
309309

310-
// delay SDK events to guarantee a different lastUpdate timestamp for SplitTreatments to re-evaluate
311-
await new Promise(resolve => setTimeout(resolve, 10));
312310
act(() => {
313311
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY_TIMED_OUT);
314312
(outerFactory as any).client('user2').__emitter__.emit(Event.SDK_READY_TIMED_OUT);
@@ -317,7 +315,6 @@ describe.each([
317315
expect(renderTimesComp1).toBe(3);
318316
expect(renderTimesComp2).toBe(3);
319317

320-
await new Promise(resolve => setTimeout(resolve, 10));
321318
act(() => {
322319
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY);
323320
(outerFactory as any).client('user2').__emitter__.emit(Event.SDK_READY);
@@ -326,7 +323,6 @@ describe.each([
326323
expect(renderTimesComp1).toBe(3); // updateOnSdkReady === false, in first component
327324
expect(renderTimesComp2).toBe(4);
328325

329-
await new Promise(resolve => setTimeout(resolve, 10));
330326
act(() => {
331327
(outerFactory as any).client().__emitter__.emit(Event.SDK_UPDATE);
332328
(outerFactory as any).client('user2').__emitter__.emit(Event.SDK_UPDATE);

src/__tests__/useSplitClient.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ test('useSplitClient must update on SDK events', () => {
6868
<SplitClient splitKey={'user_2'} updateOnSdkUpdate={true}>
6969
{React.createElement(() => {
7070
const status = useSplitClient('user_2', undefined, undefined, { updateOnSdkUpdate: true });
71-
countNestedComponent++;
72-
7371
expect(status.client).toBe(user2Client);
72+
73+
// useSplitClient doesn't re-render twice if it is in the context of a SplitClient with same user key and there is a SDK event
74+
countNestedComponent++;
7475
switch (countNestedComponent) {
7576
case 1:
7677
expect(status.isReady).toBe(false);

0 commit comments

Comments
 (0)