|
1 | | -import { shallow, mount } from 'enzyme'; |
2 | | -import { Provider } from 'react-redux'; |
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import * as reactRedux from 'react-redux'; |
3 | 4 | import configureMockStore from 'redux-mock-store'; |
4 | 5 | import thunk from 'redux-thunk'; |
5 | 6 | import AppInitSDK from '../AppInitSDK'; |
6 | 7 | import * as configSetup from '../configSetup'; |
7 | 8 | import * as apiDiscovery from '../k8s/api-discovery/api-discovery'; |
8 | 9 | import * as hooks from '../useReduxStore'; |
9 | 10 |
|
10 | | -jest.mock('react-redux', () => { |
11 | | - const ActualReactRedux = jest.requireActual('react-redux'); |
12 | | - return { |
13 | | - ...ActualReactRedux, |
14 | | - useStore: jest.fn(), |
15 | | - }; |
16 | | -}); |
| 11 | +jest.mock('react-redux', () => ({ |
| 12 | + Provider: jest.fn(({ children }) => children), |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../useReduxStore', () => ({ |
| 16 | + useReduxStore: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('../configSetup', () => ({ |
| 20 | + setUtilsConfig: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +jest.mock('../k8s/api-discovery/api-discovery', () => ({ |
| 24 | + initApiDiscovery: jest.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +const { useReduxStore: useReduxStoreMock } = hooks as jest.Mocked<typeof hooks>; |
| 28 | +const mockStore = configureMockStore([thunk]); |
| 29 | +const store = mockStore({}); |
| 30 | +const mockProvider = (reactRedux as jest.Mocked<typeof reactRedux>).Provider; |
| 31 | +const mockConfig = { appFetch: jest.fn() }; |
| 32 | +const mockApiDiscoveryConfig = { apiDiscovery: jest.fn(), appFetch: jest.fn() }; |
17 | 33 |
|
18 | 34 | describe('AppInitSDK', () => { |
19 | | - const mockStore = configureMockStore(); |
20 | | - const store = mockStore([thunk]); |
21 | | - const mockConfig = { |
22 | | - apiDiscovery: jest.fn(), |
23 | | - appFetch: jest.fn(), |
| 35 | + const renderAppInitSDK = (config, storeContext = { store, storeContextPresent: true }) => { |
| 36 | + useReduxStoreMock.mockReturnValue(storeContext); |
| 37 | + return render( |
| 38 | + <AppInitSDK configurations={config}> |
| 39 | + <div>Hello, OpenShift!</div> |
| 40 | + </AppInitSDK>, |
| 41 | + ); |
24 | 42 | }; |
25 | 43 |
|
26 | | - let useReduxStoreSpy; |
27 | | - let configSetupSpy; |
28 | | - beforeEach(() => { |
29 | | - useReduxStoreSpy = jest.spyOn(hooks, 'useReduxStore'); |
30 | | - configSetupSpy = jest.spyOn(configSetup, 'setUtilsConfig'); |
31 | | - }); |
32 | | - |
33 | 44 | afterEach(() => { |
34 | | - jest.resetAllMocks(); |
| 45 | + jest.clearAllMocks(); |
35 | 46 | }); |
36 | 47 |
|
37 | 48 | it('should not wrap children with Provider', () => { |
38 | | - useReduxStoreSpy.mockImplementation(() => ({ store, storeContextPresent: true })); |
39 | | - const wrapper = shallow( |
40 | | - <AppInitSDK configurations={mockConfig}> |
41 | | - <div data-test-id="child-id">Hello!!</div> |
42 | | - </AppInitSDK>, |
43 | | - ); |
44 | | - expect(wrapper.find(Provider)).toHaveLength(0); |
45 | | - expect(wrapper.find('[data-test-id="child-id"]')).toHaveLength(1); |
| 49 | + renderAppInitSDK(mockConfig, { store, storeContextPresent: true }); |
| 50 | + |
| 51 | + expect(mockProvider).not.toHaveBeenCalled(); |
| 52 | + expect(screen.getByText('Hello, OpenShift!')).toBeVisible(); |
46 | 53 | }); |
47 | 54 |
|
48 | | - it('should wrap children with Provider', () => { |
49 | | - useReduxStoreSpy.mockImplementation(() => ({ store, storeContextPresent: false })); |
50 | | - const wrapper = shallow( |
51 | | - <AppInitSDK configurations={mockConfig}> |
52 | | - <div data-test-id="child-id">Hello!!</div> |
53 | | - </AppInitSDK>, |
54 | | - ); |
55 | | - expect(wrapper.find(Provider)).toHaveLength(1); |
56 | | - expect(wrapper.find('[data-test-id="child-id"]')).toHaveLength(1); |
| 55 | + it('should wrap children with a Provider if no store context is present', () => { |
| 56 | + renderAppInitSDK(mockConfig, { store, storeContextPresent: false }); |
| 57 | + |
| 58 | + expect(mockProvider).toHaveBeenCalled(); |
| 59 | + expect(screen.getByText('Hello, OpenShift!')).toBeVisible(); |
57 | 60 | }); |
58 | 61 |
|
59 | | - it('should call the hook useReduxStore', () => { |
60 | | - useReduxStoreSpy.mockImplementation(() => ({ store, storeContextPresent: true })); |
61 | | - shallow( |
62 | | - <AppInitSDK configurations={mockConfig}> |
63 | | - <div data-test-id="child-id">Hello!!</div> |
64 | | - </AppInitSDK>, |
65 | | - ); |
66 | | - expect(useReduxStoreSpy).toHaveBeenCalled(); |
67 | | - expect(useReduxStoreSpy).toHaveBeenCalledTimes(1); |
| 62 | + it('should call the useReduxStore hook', () => { |
| 63 | + renderAppInitSDK(mockConfig, { store, storeContextPresent: true }); |
| 64 | + |
| 65 | + expect(useReduxStoreMock).toHaveBeenCalledTimes(1); |
68 | 66 | }); |
69 | 67 |
|
70 | | - it('should call the util setUtilsConfig with proper config', () => { |
71 | | - useReduxStoreSpy.mockImplementation(() => ({ store, storeContextPresent: true })); |
72 | | - mount( |
73 | | - <AppInitSDK configurations={mockConfig}> |
74 | | - <div data-test-id="child-id">Hello!!</div> |
75 | | - </AppInitSDK>, |
76 | | - ); |
77 | | - expect(configSetupSpy).toHaveBeenCalled(); |
78 | | - expect(configSetupSpy).toHaveBeenCalledTimes(1); |
79 | | - expect(configSetupSpy).toHaveBeenCalledWith({ appFetch: mockConfig.appFetch }); |
| 68 | + it('should call the setUtilsConfig utility with the proper config', () => { |
| 69 | + renderAppInitSDK(mockConfig, { store, storeContextPresent: true }); |
| 70 | + |
| 71 | + expect(configSetup.setUtilsConfig).toHaveBeenCalledTimes(1); |
| 72 | + expect(configSetup.setUtilsConfig).toHaveBeenCalledWith({ appFetch: mockConfig.appFetch }); |
80 | 73 | }); |
81 | 74 |
|
82 | | - it('should call apiDiscovery with store instance if provided and not default one', () => { |
83 | | - useReduxStoreSpy.mockImplementation(() => ({ store, storeContextPresent: true })); |
84 | | - const initApiDiscoverySpy = jest.spyOn(apiDiscovery, 'initApiDiscovery'); |
85 | | - initApiDiscoverySpy.mockImplementation(jest.fn()); |
86 | | - mount( |
87 | | - <AppInitSDK configurations={mockConfig}> |
88 | | - <div data-test-id="child-id">Hello!!</div> |
89 | | - </AppInitSDK>, |
90 | | - ); |
91 | | - expect(mockConfig.apiDiscovery).toHaveBeenCalled(); |
92 | | - expect(mockConfig.apiDiscovery).toHaveBeenCalledTimes(1); |
93 | | - expect(mockConfig.apiDiscovery).toHaveBeenCalledWith(store); |
94 | | - expect(initApiDiscoverySpy).toHaveBeenCalledTimes(0); |
| 75 | + it('should call the provided apiDiscovery function if it exists', () => { |
| 76 | + renderAppInitSDK(mockApiDiscoveryConfig, { store, storeContextPresent: true }); |
| 77 | + |
| 78 | + expect(mockApiDiscoveryConfig.apiDiscovery).toHaveBeenCalledTimes(1); |
| 79 | + expect(mockApiDiscoveryConfig.apiDiscovery).toHaveBeenCalledWith(store); |
| 80 | + expect(apiDiscovery.initApiDiscovery).not.toHaveBeenCalled(); |
95 | 81 | }); |
96 | 82 |
|
97 | | - it('should trigger default apiDiscovery if apiDiscovery is not provided with config', () => { |
98 | | - const mockConfigData = { |
99 | | - appFetch: jest.fn(), |
100 | | - }; |
101 | | - useReduxStoreSpy.mockImplementation(() => ({ store, storeContextPresent: false })); |
102 | | - const initApiDiscoverySpy = jest.spyOn(apiDiscovery, 'initApiDiscovery'); |
103 | | - initApiDiscoverySpy.mockImplementation(jest.fn()); |
104 | | - mount( |
105 | | - <AppInitSDK configurations={mockConfigData}> |
106 | | - <div data-test-id="child-id">Hello!!</div> |
107 | | - </AppInitSDK>, |
108 | | - ); |
109 | | - expect(initApiDiscoverySpy).toHaveBeenCalled(); |
110 | | - expect(initApiDiscoverySpy).toHaveBeenCalledTimes(1); |
111 | | - expect(initApiDiscoverySpy).toHaveBeenCalledWith(store); |
| 83 | + it('should trigger the default initApiDiscovery if no apiDiscovery function is provided', () => { |
| 84 | + renderAppInitSDK(mockConfig, { store, storeContextPresent: false }); |
| 85 | + |
| 86 | + expect(apiDiscovery.initApiDiscovery).toHaveBeenCalledTimes(1); |
| 87 | + expect(apiDiscovery.initApiDiscovery).toHaveBeenCalledWith(store); |
112 | 88 | }); |
113 | 89 | }); |
0 commit comments