fail tests that initiate network requests

This commit is contained in:
Keith J. Grant 2021-07-12 15:05:52 -07:00
parent 6715b88633
commit 3e79fa2dcb
8 changed files with 77 additions and 56 deletions

View File

@ -1,21 +1,18 @@
import React from 'react';
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
import { shallow } from 'enzyme';
import InstanceGroups from './InstanceGroups';
describe('<InstanceGroups/>', () => {
let pageWrapper;
let pageSections;
test('should set breadcrumbs', () => {
const wrapper = shallow(<InstanceGroups />);
beforeEach(() => {
pageWrapper = mountWithContexts(<InstanceGroups />);
pageSections = pageWrapper.find('PageSection');
});
test('initially renders without crashing', () => {
expect(pageWrapper.length).toBe(1);
expect(pageSections.length).toBe(1);
expect(pageSections.first().props().variant).toBe('light');
const header = wrapper.find('ScreenHeader');
expect(header.prop('streamType')).toEqual('instance_group');
expect(header.prop('breadcrumbConfig')).toEqual({
'/instance_groups': 'Instance Groups',
'/instance_groups/add': 'Create new instance group',
'/instance_groups/container_group/add': 'Create new container group',
});
});
});

View File

@ -119,7 +119,7 @@ function Job({ setBreadcrumb }) {
<PageSection>
<Card>
<ContentError error={error}>
{error.response.status === 404 && (
{error.response?.status === 404 && (
<span>
{t`The page you requested could not be found.`}{' '}
<Link to="/jobs">{t`View all Jobs.`}</Link>

View File

@ -1,15 +1,22 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
import Job from './Job';
jest.mock('../../api');
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
typeSegment: 'project',
}),
}));
describe('<Job />', () => {
test('initially renders successfully', () => {
mountWithContexts(<Job />);
test('initially renders successfully', async () => {
await act(async () => {
await mountWithContexts(<Job setBreadcrumb={() => {}} />);
});
});
});

View File

@ -2,6 +2,14 @@ import React from 'react';
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
import SettingList from './SettingList';
jest.mock('../../api');
jest.mock('../../util/useBrandName', () => ({
__esModule: true,
default: () => ({
current: 'AWX',
}),
}));
describe('<SettingList />', () => {
let wrapper;
beforeEach(() => {

View File

@ -1,39 +1,11 @@
import React from 'react';
import { Route } from 'react-router-dom';
import { act } from 'react-dom/test-utils';
import { createMemoryHistory } from 'history';
import {
mountWithContexts,
waitForElement,
} from '../../../../testUtils/enzymeHelpers';
import { shallow } from 'enzyme';
import UserOrganizations from './UserOrganizations';
describe('<UserOrganizations />', () => {
test('userOrganizations mounts successfully', () => {
const history = createMemoryHistory({
initialEntries: ['/users/1/organizations'],
});
let wrapper;
act(() => {
wrapper = mountWithContexts(
<Route
path="/users/:id/organizations"
component={() => <UserOrganizations />}
/>,
{
context: {
router: {
history,
route: {
location: history.location,
match: { params: { id: 1 } },
},
},
},
}
);
});
waitForElement(wrapper, 'UserOrganizationList');
test('should render UserOrganizationList', () => {
const wrapper = shallow(<UserOrganizations />);
expect(wrapper.find('UserOrganizationList')).toHaveLength(1);
});
});

View File

@ -5,14 +5,11 @@ import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import UserTokens from './UserTokens';
jest.mock('../../../api');
describe('<UserTokens />', () => {
let wrapper;
test('renders successfully', () => {
wrapper = mountWithContexts(<UserTokens />);
expect(wrapper.length).toBe(1);
});
test('shows Application information modal after successful creation', async () => {
const history = createMemoryHistory({
initialEntries: ['/users/1/tokens/add'],

View File

@ -16,6 +16,7 @@ export const asyncFlush = () => new Promise(resolve => setImmediate(resolve));
let hasConsoleError = false;
let hasConsoleWarn = false;
let networkRequestUrl = false;
const { error, warn } = global.console;
global.console = {
@ -26,8 +27,10 @@ global.console = {
// fail tests that log errors.
// adapted from https://github.com/facebook/jest/issues/6121#issuecomment-708330601
error: (...args) => {
hasConsoleError = true;
error(...args);
if (!networkRequestUrl) {
hasConsoleError = true;
error(...args);
}
},
warn: (...args) => {
hasConsoleWarn = true;
@ -35,7 +38,41 @@ global.console = {
},
};
const logNetworkRequestError = url => {
networkRequestUrl = url || true;
return {
status: 200,
data: {},
};
};
jest.mock('axios', () => {
const axiosActual = jest.requireActual('axios');
return {
...axiosActual,
create: () => ({
get: logNetworkRequestError,
post: logNetworkRequestError,
delete: logNetworkRequestError,
put: logNetworkRequestError,
patch: logNetworkRequestError,
options: logNetworkRequestError,
interceptors: {
response: {
use: () => {},
},
},
}),
};
});
afterEach(() => {
if (networkRequestUrl) {
const url = networkRequestUrl;
networkRequestUrl = false;
throw new Error(
`Network request was attempted to URL ${url} — API should be stubbed using jest.mock()`
);
}
if (hasConsoleError) {
hasConsoleError = false;
throw new Error('Error logged to console');

View File

@ -23,6 +23,9 @@ jest.mock('../api/models/WorkflowJobTemplates');
jest.mock('../api/models/WorkflowJobTemplateNodes');
jest.mock('../api/models/CredentialInputSources');
jest.mock('../api/models/ExecutionEnvironments');
jest.mock('../api/models/Applications');
jest.mock('../api/models/NotificationTemplates');
jest.mock('../api/models/Teams');
describe('delete details', () => {
afterEach(() => {