mirror of
https://github.com/ansible/awx.git
synced 2026-02-18 03:30:02 -03:30
144 lines
4.7 KiB
JavaScript
144 lines
4.7 KiB
JavaScript
import React from 'react';
|
|
import { act } from 'react-dom/test-utils';
|
|
import { createMemoryHistory } from 'history';
|
|
import { SettingsProvider } from 'contexts/Settings';
|
|
import { SettingsAPI } from 'api';
|
|
import {
|
|
mountWithContexts,
|
|
waitForElement,
|
|
} from '../../../../../testUtils/enzymeHelpers';
|
|
import mockAllOptions from '../../shared/data.allSettingOptions.json';
|
|
import mockJobSettings from '../../shared/data.jobSettings.json';
|
|
import JobsEdit from './JobsEdit';
|
|
|
|
jest.mock('../../../../api');
|
|
|
|
describe('<JobsEdit />', () => {
|
|
let wrapper;
|
|
let history;
|
|
|
|
beforeEach(() => {
|
|
SettingsAPI.revertCategory.mockResolvedValue({});
|
|
SettingsAPI.updateAll.mockResolvedValue({});
|
|
SettingsAPI.readCategory.mockResolvedValue({
|
|
data: mockJobSettings,
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
history = createMemoryHistory({
|
|
initialEntries: ['/settings/jobs/edit'],
|
|
});
|
|
await act(async () => {
|
|
wrapper = mountWithContexts(
|
|
<SettingsProvider value={mockAllOptions.actions}>
|
|
<JobsEdit />
|
|
</SettingsProvider>,
|
|
{
|
|
context: { router: { history } },
|
|
}
|
|
);
|
|
});
|
|
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
|
|
});
|
|
|
|
test('initially renders without crashing', () => {
|
|
expect(wrapper.find('JobsEdit').length).toBe(1);
|
|
});
|
|
|
|
test('should successfully send default values to api on form revert all', async () => {
|
|
expect(SettingsAPI.revertCategory).toHaveBeenCalledTimes(0);
|
|
expect(wrapper.find('RevertAllAlert')).toHaveLength(0);
|
|
await act(async () => {
|
|
wrapper
|
|
.find('button[aria-label="Revert all to default"]')
|
|
.invoke('onClick')();
|
|
});
|
|
wrapper.update();
|
|
expect(wrapper.find('RevertAllAlert')).toHaveLength(1);
|
|
await act(async () => {
|
|
wrapper
|
|
.find('RevertAllAlert button[aria-label="Confirm revert all"]')
|
|
.invoke('onClick')();
|
|
});
|
|
wrapper.update();
|
|
expect(SettingsAPI.revertCategory).toHaveBeenCalledTimes(1);
|
|
expect(SettingsAPI.revertCategory).toHaveBeenCalledWith('jobs');
|
|
});
|
|
|
|
test('should successfully send request to api on form submission', async () => {
|
|
expect(SettingsAPI.updateAll).toHaveBeenCalledTimes(0);
|
|
await act(async () => {
|
|
wrapper.find('Form').invoke('onSubmit')();
|
|
});
|
|
expect(SettingsAPI.updateAll).toHaveBeenCalledTimes(1);
|
|
const {
|
|
EVENT_STDOUT_MAX_BYTES_DISPLAY,
|
|
STDOUT_MAX_BYTES_DISPLAY,
|
|
...jobRequest
|
|
} = mockJobSettings;
|
|
expect(SettingsAPI.updateAll).toHaveBeenCalledWith(jobRequest);
|
|
});
|
|
|
|
test('should display error message on unsuccessful submission', async () => {
|
|
const error = {
|
|
response: {
|
|
data: { detail: 'An error occurred' },
|
|
},
|
|
};
|
|
SettingsAPI.updateAll.mockImplementation(() => Promise.reject(error));
|
|
expect(wrapper.find('FormSubmitError').length).toBe(0);
|
|
expect(SettingsAPI.updateAll).toHaveBeenCalledTimes(0);
|
|
await act(async () => {
|
|
wrapper.find('Form').invoke('onSubmit')();
|
|
});
|
|
wrapper.update();
|
|
expect(wrapper.find('FormSubmitError').length).toBe(1);
|
|
expect(SettingsAPI.updateAll).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test('should navigate to job settings detail when cancel is clicked', async () => {
|
|
await act(async () => {
|
|
wrapper.find('button[aria-label="Cancel"]').invoke('onClick')();
|
|
});
|
|
expect(history.location.pathname).toEqual('/settings/jobs/details');
|
|
});
|
|
|
|
test('should display ContentError on throw', async () => {
|
|
SettingsAPI.readCategory.mockImplementationOnce(() =>
|
|
Promise.reject(new Error())
|
|
);
|
|
await act(async () => {
|
|
wrapper = mountWithContexts(
|
|
<SettingsProvider value={mockAllOptions.actions}>
|
|
<JobsEdit />
|
|
</SettingsProvider>
|
|
);
|
|
});
|
|
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
|
|
expect(wrapper.find('ContentError').length).toBe(1);
|
|
});
|
|
|
|
test('Form input fields that are invisible (due to being set manually via a settings file) should not prevent submitting the form', async () => {
|
|
const mockOptions = Object.assign({}, mockAllOptions);
|
|
// If AWX_ISOLATION_BASE_PATH has been set in a settings file it will be absent in the PUT options
|
|
delete mockOptions['actions']['PUT']['AWX_ISOLATION_BASE_PATH'];
|
|
await act(async () => {
|
|
wrapper = mountWithContexts(
|
|
<SettingsProvider value={mockOptions.actions}>
|
|
<JobsEdit />
|
|
</SettingsProvider>
|
|
);
|
|
});
|
|
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
|
|
await act(async () => {
|
|
wrapper.find('Form').invoke('onSubmit')();
|
|
});
|
|
expect(SettingsAPI.updateAll).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|