-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathwithSplitClient.test.tsx
More file actions
110 lines (98 loc) · 4.31 KB
/
withSplitClient.test.tsx
File metadata and controls
110 lines (98 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as React from 'react';
import { render } from '@testing-library/react';
/** Mocks */
import { mockSdk, Event } from './testUtils/mockSplitFactory';
jest.mock('@splitsoftware/splitio/client', () => {
return { SplitFactory: mockSdk() };
});
import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client';
import { sdkBrowser } from './testUtils/sdkConfigs';
import * as SplitClient from '../SplitClient';
const SplitClientSpy = jest.spyOn(SplitClient, 'SplitClient');
import { testAttributesBinding, TestComponentProps } from './testUtils/utils';
/** Test target */
import { withSplitFactory } from '../withSplitFactory';
import { withSplitClient } from '../withSplitClient';
describe('withSplitClient', () => {
test('passes no-ready props to the child if client is not ready.', () => {
const Component = withSplitFactory(sdkBrowser)(
withSplitClient('user1')(
({ client, isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate }) => {
expect(client).not.toBe(null);
expect([isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate]).toStrictEqual([false, false, false, false, false, 0]);
return null;
}
)
);
render(<Component />);
});
test('passes ready props to the child if client is ready.', (done) => {
const outerFactory = SplitSdk(sdkBrowser);
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY);
(outerFactory.manager().names as jest.Mock).mockReturnValue(['split1']);
outerFactory.client().ready().then(() => {
const Component = withSplitFactory(undefined, outerFactory)(
withSplitClient('user1')(
({ client, isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate }) => {
expect(client).toBe(outerFactory.client('user1'));
expect([isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate]).toStrictEqual([false, false, false, false, false, 0]);
return null;
}
)
);
render(<Component />);
done();
});
});
test('passes Split props and outer props to the child.', () => {
const Component = withSplitFactory(sdkBrowser)<{ outerProp1: string, outerProp2: number }>(
withSplitClient('user1')<{ outerProp1: string, outerProp2: number }>(
({ outerProp1, outerProp2, client, isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate }) => {
expect(outerProp1).toBe('outerProp1');
expect(outerProp2).toBe(2);
expect(client).not.toBe(null);
expect([isReady, isReadyFromCache, hasTimedout, isTimedout, isDestroyed, lastUpdate]).toStrictEqual([false, false, false, false, false, 0]);
return null;
}
)
);
render(<Component outerProp1='outerProp1' outerProp2={2} />);
});
test('passes Status props to SplitClient.', () => {
const updateOnSdkUpdate = true;
const updateOnSdkTimedout = false;
const updateOnSdkReady = true;
const updateOnSdkReadyFromCache = false;
const Component = withSplitFactory(sdkBrowser)(
withSplitClient('user1')<{ outerProp1: string, outerProp2: number }>(
() => null, updateOnSdkUpdate, updateOnSdkTimedout, updateOnSdkReady, updateOnSdkReadyFromCache
)
);
render(<Component outerProp1='outerProp1' outerProp2={2} />);
expect(SplitClientSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
updateOnSdkUpdate,
updateOnSdkTimedout,
updateOnSdkReady,
updateOnSdkReadyFromCache,
}),
expect.anything()
);
});
test('attributes binding test with utility', (done) => {
function Component({ attributesFactory, attributesClient, splitKey, testSwitch, factory }: TestComponentProps) {
const FactoryComponent = withSplitFactory(undefined, factory, attributesFactory)<{ attributesClient: SplitIO.Attributes, splitKey: any }>(
({ attributesClient, splitKey }) => {
const ClientComponent = withSplitClient(splitKey, attributesClient)(
() => {
testSwitch(done, splitKey);
return null;
})
return <ClientComponent />;
}
)
return <FactoryComponent attributesClient={attributesClient} splitKey={splitKey} />
}
testAttributesBinding(Component);
});
});