Skip to content

Commit

Permalink
test polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Jul 23, 2023
1 parent 2fdf2f0 commit 261c038
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/__tests__/SplitTreatments.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,17 @@ describe.each([
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY);
});

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

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

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

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

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

await new Promise(resolve => setTimeout(resolve, 10));
act(() => {
(outerFactory as any).client().__emitter__.emit(Event.SDK_UPDATE);
(outerFactory as any).client('user2').__emitter__.emit(Event.SDK_UPDATE);
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/useSplitClient.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ test('useSplitClient must update on SDK events', () => {
<SplitClient splitKey={'user_2'} updateOnSdkUpdate={true}>
{React.createElement(() => {
const status = useSplitClient('user_2', undefined, undefined, { updateOnSdkUpdate: true });
countNestedComponent++;

expect(status.client).toBe(user2Client);

// 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
countNestedComponent++;
switch (countNestedComponent) {
case 1:
expect(status.isReady).toBe(false);
Expand Down

0 comments on commit 261c038

Please sign in to comment.