Skip to content

Commit 694efdb

Browse files
Merge pull request #15540 from cajieh/migrate-enzyme-console-dynamic-plugin-sdk-unit-tests-rtl
CONSOLE-4603: Migrate enzyme "packages/console-dynamic-plugin-sdk" unit tests to Re…
2 parents 4290cf0 + afc5103 commit 694efdb

File tree

1 file changed

+62
-86
lines changed

1 file changed

+62
-86
lines changed
Lines changed: 62 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,89 @@
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';
34
import configureMockStore from 'redux-mock-store';
45
import thunk from 'redux-thunk';
56
import AppInitSDK from '../AppInitSDK';
67
import * as configSetup from '../configSetup';
78
import * as apiDiscovery from '../k8s/api-discovery/api-discovery';
89
import * as hooks from '../useReduxStore';
910

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() };
1733

1834
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+
);
2442
};
2543

26-
let useReduxStoreSpy;
27-
let configSetupSpy;
28-
beforeEach(() => {
29-
useReduxStoreSpy = jest.spyOn(hooks, 'useReduxStore');
30-
configSetupSpy = jest.spyOn(configSetup, 'setUtilsConfig');
31-
});
32-
3344
afterEach(() => {
34-
jest.resetAllMocks();
45+
jest.clearAllMocks();
3546
});
3647

3748
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();
4653
});
4754

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();
5760
});
5861

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);
6866
});
6967

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 });
8073
});
8174

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();
9581
});
9682

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);
11288
});
11389
});

0 commit comments

Comments
 (0)