|
1 | | -import React from 'react'; |
2 | | -import { render } from '@testing-library/react'; |
3 | | - |
4 | 1 | /** Mocks */ |
5 | | -import { mockSdk } from './testUtils/mockSplitSdk'; |
6 | | -jest.mock('@splitsoftware/splitio/client', () => { |
7 | | - return { SplitFactory: mockSdk() }; |
8 | | -}); |
9 | | -import { SplitFactory as SplitSdk } from '@splitsoftware/splitio/client'; |
10 | | -import { sdkBrowser } from './testUtils/sdkConfigs'; |
| 2 | +const useSplitClientMock = jest.fn(); |
| 3 | +jest.mock('../useSplitClient', () => ({ |
| 4 | + useSplitClient: useSplitClientMock |
| 5 | +})); |
11 | 6 |
|
12 | 7 | /** Test target */ |
13 | | -import { SplitFactory } from '../SplitFactory'; |
14 | | -import { SplitClient } from '../SplitClient'; |
15 | 8 | import { useClient } from '../useClient'; |
16 | | -import { testAttributesBinding, TestComponentProps } from './testUtils/utils'; |
17 | 9 |
|
18 | 10 | describe('useClient', () => { |
19 | 11 |
|
20 | | - test('returns the main client from the context updated by SplitFactory.', () => { |
21 | | - const outerFactory = SplitSdk(sdkBrowser); |
22 | | - let client; |
23 | | - render( |
24 | | - <SplitFactory factory={outerFactory} > |
25 | | - {React.createElement(() => { |
26 | | - client = useClient(); |
27 | | - return null; |
28 | | - })} |
29 | | - </SplitFactory> |
30 | | - ); |
31 | | - expect(client).toBe(outerFactory.client()); |
32 | | - }); |
33 | | - |
34 | | - test('returns the client from the context updated by SplitClient.', () => { |
35 | | - const outerFactory = SplitSdk(sdkBrowser); |
36 | | - let client; |
37 | | - render( |
38 | | - <SplitFactory factory={outerFactory} > |
39 | | - <SplitClient splitKey='user2' > |
40 | | - {React.createElement(() => { |
41 | | - client = useClient(); |
42 | | - return null; |
43 | | - })} |
44 | | - </SplitClient> |
45 | | - </SplitFactory> |
46 | | - ); |
47 | | - expect(client).toBe(outerFactory.client('user2')); |
48 | | - }); |
49 | | - |
50 | | - test('returns a new client from the factory at Split context given a splitKey.', () => { |
51 | | - const outerFactory = SplitSdk(sdkBrowser); |
52 | | - let client; |
53 | | - render( |
54 | | - <SplitFactory factory={outerFactory} > |
55 | | - {React.createElement(() => { |
56 | | - (outerFactory.client as jest.Mock).mockClear(); |
57 | | - client = useClient('user2', 'user'); |
58 | | - return null; |
59 | | - })} |
60 | | - </SplitFactory> |
61 | | - ); |
62 | | - expect(outerFactory.client as jest.Mock).toBeCalledWith('user2', 'user'); |
63 | | - expect(outerFactory.client as jest.Mock).toHaveReturnedWith(client); |
64 | | - }); |
65 | | - |
66 | | - test('returns null if invoked outside Split context.', () => { |
67 | | - let client; |
68 | | - let sharedClient; |
69 | | - render( |
70 | | - React.createElement(() => { |
71 | | - client = useClient(); |
72 | | - sharedClient = useClient('user2', 'user'); |
73 | | - return null; |
74 | | - }) |
75 | | - ); |
76 | | - expect(client).toBe(null); |
77 | | - expect(sharedClient).toBe(null); |
78 | | - }); |
79 | | - |
80 | | - test('attributes binding test with utility', (done) => { |
81 | | - |
82 | | - // eslint-disable-next-line react/prop-types |
83 | | - const InnerComponent = ({ splitKey, attributesClient, testSwitch}) => { |
84 | | - useClient(splitKey, 'user', attributesClient); |
85 | | - testSwitch(done, splitKey); |
86 | | - return null; |
87 | | - }; |
| 12 | + test('calls useSplitClient with the correct arguments and returns the client.', () => { |
| 13 | + const attributes = { someAttribute: 'someValue' }; |
| 14 | + const client = 'client'; |
| 15 | + useSplitClientMock.mockReturnValue({ client, isReady: false }); |
88 | 16 |
|
89 | | - function Component({ attributesFactory, attributesClient, splitKey, testSwitch, factory }: TestComponentProps) { |
90 | | - return ( |
91 | | - <SplitFactory factory={factory} attributes={attributesFactory} > |
92 | | - <InnerComponent splitKey={splitKey} attributesClient={attributesClient} testSwitch={testSwitch} /> |
93 | | - </SplitFactory> |
94 | | - ); |
95 | | - } |
| 17 | + expect(useClient('someKey', 'someTrafficType', attributes)).toBe(client); |
96 | 18 |
|
97 | | - testAttributesBinding(Component); |
| 19 | + expect(useSplitClientMock).toHaveBeenCalledTimes(1); |
| 20 | + expect(useSplitClientMock).toHaveBeenCalledWith({ splitKey: 'someKey', trafficType: 'someTrafficType', attributes }); |
98 | 21 | }); |
99 | 22 |
|
100 | 23 | }); |
0 commit comments