Skip to content

Commit 2105d94

Browse files
committed
Fixes #39731 - replace enzyme tests with RTL for BulkChangePuppetProxy
1 parent 7273a04 commit 2105d94

2 files changed

Lines changed: 96 additions & 52 deletions

File tree

Lines changed: 91 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,108 @@
11
import React from 'react';
2-
import { mount } from '@theforeman/test';
2+
import { screen, waitFor } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
import '@testing-library/jest-dom';
35

46
import { openBulkModal } from 'foremanReact/common/BulkModalStateHelper';
57
import { ForemanActionsBarContext } from 'foremanReact/components/HostDetails/ActionsBar';
8+
import { rtlHelpers } from 'foremanReact/common/rtlTestHelpers';
9+
import API from 'foremanReact/redux/API/API';
610

711
import BulkChangePuppetProxyScene from '../index';
8-
import BulkChangeProxyCommon from '../../BulkChangeProxyCommon';
912

10-
jest.mock('foremanReact/components/HostDetails/ActionsBar', () => ({
11-
ForemanActionsBarContext: jest.requireActual('react').createContext(),
13+
jest.mock('foremanReact/redux/API', () => ({
14+
...jest.requireActual('foremanReact/redux/API'),
1215
}));
1316

14-
jest.mock('../../BulkChangeProxyCommon', () => ({
15-
__esModule: true,
16-
default: jest.fn(() => null),
17-
}));
17+
const { renderWithStoreAndI18n } = rtlHelpers;
1818

19-
describe('BulkChangePuppetProxyScene', () => {
20-
const fetchBulkParams = jest.fn();
21-
const refreshTableData = jest.fn();
22-
const contextValue = {
23-
selectAllHostsMode: false,
24-
selectedCount: 2,
25-
selectedResults: [1, 2],
26-
fetchBulkParams,
27-
refreshTableData,
28-
};
19+
const MODAL_ID = 'bulk-change-puppet-proxy';
20+
21+
const fetchBulkParams = jest.fn(() => 'id ^ (1,2)');
22+
const refreshTableData = jest.fn();
23+
24+
const defaultContextValue = {
25+
selectAllHostsMode: false,
26+
selectedCount: 2,
27+
selectedResults: [1, 2],
28+
fetchBulkParams,
29+
refreshTableData,
30+
};
31+
32+
const smartProxiesResponse = {
33+
data: {
34+
results: [{ id: 1, name: 'proxy1.example.com' }],
35+
},
36+
};
2937

38+
const renderScene = ({ contextValue = defaultContextValue } = {}) =>
39+
renderWithStoreAndI18n(
40+
<ForemanActionsBarContext.Provider value={contextValue}>
41+
<BulkChangePuppetProxyScene />
42+
</ForemanActionsBarContext.Provider>
43+
);
44+
45+
describe('BulkChangePuppetProxyScene', () => {
3046
beforeEach(() => {
47+
openBulkModal(MODAL_ID, false);
48+
API.get.mockImplementation(() => Promise.resolve(smartProxiesResponse));
49+
});
50+
51+
afterEach(() => {
3152
jest.clearAllMocks();
32-
openBulkModal('bulk-change-puppet-proxy', false);
3353
});
3454

35-
it('opens with bulk modal state and passes expected props', () => {
36-
openBulkModal('bulk-change-puppet-proxy', true);
37-
const wrapper = mount(
38-
<ForemanActionsBarContext.Provider value={contextValue}>
39-
<BulkChangePuppetProxyScene />
40-
</ForemanActionsBarContext.Provider>
41-
);
42-
43-
const componentType =
44-
BulkChangeProxyCommon.default || BulkChangeProxyCommon;
45-
const props = wrapper.find(componentType).props();
46-
47-
expect(props).toEqual(
48-
expect.objectContaining({
49-
isCAProxy: false,
50-
fetchBulkParams,
51-
selectedCount: 2,
52-
selectedResults: [1, 2],
53-
selectAllHostsMode: false,
54-
isOpen: true,
55-
closeModal: expect.any(Function),
56-
onSuccess: refreshTableData,
57-
selectMessage: 'Select a Puppet Proxy',
58-
handleErrorMessage: 'Failed to change Puppet Proxy',
59-
changeMessage: 'Change Puppet Proxy',
60-
allHostsMessage:
61-
'Changing the Puppet proxy will affect {boldCount} selected hosts. Some hosts may already have been associated with the selected Puppet proxy.',
62-
someHostsMessage:
63-
'Changing the Puppet proxy will affect {boldCount} selected {count, plural, one {host} other {hosts}}. Some hosts may already have been associated with the selected Puppet proxy.',
64-
})
65-
);
66-
67-
wrapper.unmount();
55+
it('does not show the modal when bulk modal state is closed', () => {
56+
renderScene();
57+
58+
expect(
59+
screen.queryByRole('dialog', { name: 'Change Puppet Proxy' })
60+
).not.toBeInTheDocument();
61+
});
62+
63+
it('opens the modal with Puppet Proxy content when bulk modal is open', async () => {
64+
openBulkModal(MODAL_ID, true);
65+
renderScene();
66+
67+
expect(
68+
await screen.findByRole('dialog', { name: 'Change Puppet Proxy' })
69+
).toBeInTheDocument();
70+
expect(
71+
screen.getByText(/Changing the Puppet proxy will affect/)
72+
).toBeInTheDocument();
73+
expect(screen.getByText('2')).toBeInTheDocument();
74+
expect(
75+
await screen.findByText('Select a Puppet Proxy')
76+
).toBeInTheDocument();
77+
expect(screen.queryByText('Select a Puppet CA Proxy')).not.toBeInTheDocument();
78+
expect(screen.queryByText('Change Puppet CA Proxy')).not.toBeInTheDocument();
79+
expect(
80+
await screen.findByRole('button', { name: 'Change Puppet Proxy' })
81+
).toBeDisabled();
82+
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
83+
});
84+
85+
it('shows the all-hosts warning when select all hosts mode is enabled', async () => {
86+
openBulkModal(MODAL_ID, true);
87+
renderScene({
88+
contextValue: { ...defaultContextValue, selectAllHostsMode: true },
89+
});
90+
91+
await screen.findByRole('dialog', { name: 'Change Puppet Proxy' });
92+
expect(screen.getByText('All')).toBeInTheDocument();
93+
});
94+
95+
it('closes the modal when Cancel is clicked', async () => {
96+
openBulkModal(MODAL_ID, true);
97+
renderScene();
98+
99+
await screen.findByRole('dialog', { name: 'Change Puppet Proxy' });
100+
await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
101+
102+
await waitFor(() => {
103+
expect(
104+
screen.queryByRole('dialog', { name: 'Change Puppet Proxy' })
105+
).not.toBeInTheDocument();
106+
});
68107
});
69108
});

webpack/test_setup.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'foremanJSTestSetup';
2+
3+
jest.mock('foremanReact/redux/API/APISelectors', () =>
4+
jest.requireActual('foremanReact/redux/API/APISelectors')
5+
);

0 commit comments

Comments
 (0)