mirror of
https://github.com/ansible/awx.git
synced 2026-05-20 07:17:40 -02:30
Merge pull request #10665 from keithjgrant/disallow-test-network-requests
Disallow network requests in tests
This commit is contained in:
@@ -3,7 +3,7 @@ import React, { useState } from 'react';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import { withRouter } from 'react-router-dom';
|
import { useLocation } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
ButtonVariant,
|
ButtonVariant,
|
||||||
@@ -36,7 +36,6 @@ function Search({
|
|||||||
onReplaceSearch,
|
onReplaceSearch,
|
||||||
onRemove,
|
onRemove,
|
||||||
qsConfig,
|
qsConfig,
|
||||||
location,
|
|
||||||
searchableKeys,
|
searchableKeys,
|
||||||
relatedSearchableKeys,
|
relatedSearchableKeys,
|
||||||
onShowAdvancedSearch,
|
onShowAdvancedSearch,
|
||||||
@@ -45,6 +44,7 @@ function Search({
|
|||||||
enableNegativeFiltering,
|
enableNegativeFiltering,
|
||||||
enableRelatedFuzzyFiltering,
|
enableRelatedFuzzyFiltering,
|
||||||
}) {
|
}) {
|
||||||
|
const location = useLocation();
|
||||||
const [isSearchDropdownOpen, setIsSearchDropdownOpen] = useState(false);
|
const [isSearchDropdownOpen, setIsSearchDropdownOpen] = useState(false);
|
||||||
const [searchKey, setSearchKey] = useState(
|
const [searchKey, setSearchKey] = useState(
|
||||||
(() => {
|
(() => {
|
||||||
@@ -341,4 +341,4 @@ Search.defaultProps = {
|
|||||||
enableRelatedFuzzyFiltering: true,
|
enableRelatedFuzzyFiltering: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default withRouter(Search);
|
export default Search;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import { useLocation, withRouter } from 'react-router-dom';
|
import { useLocation } from 'react-router-dom';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
@@ -147,7 +147,4 @@ Sort.defaultProps = {
|
|||||||
onSort: null,
|
onSort: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
export { Sort as _Sort };
|
export default Sort;
|
||||||
const Wrapped = withRouter(Sort);
|
|
||||||
Wrapped.displayName = 'Sort';
|
|
||||||
export default Wrapped;
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
waitForElement,
|
waitForElement,
|
||||||
} from '../../../testUtils/enzymeHelpers';
|
} from '../../../testUtils/enzymeHelpers';
|
||||||
|
|
||||||
import Sort, { _Sort as SortUnwrapped } from './Sort';
|
import Sort from './Sort';
|
||||||
|
|
||||||
jest.mock('react-router-dom', () => ({
|
jest.mock('react-router-dom', () => ({
|
||||||
...jest.requireActual('react-router-dom'),
|
...jest.requireActual('react-router-dom'),
|
||||||
@@ -168,7 +168,7 @@ describe('<Sort />', () => {
|
|||||||
const numericColumns = [{ name: 'ID', key: 'id' }];
|
const numericColumns = [{ name: 'ID', key: 'id' }];
|
||||||
|
|
||||||
const wrapper = shallow(
|
const wrapper = shallow(
|
||||||
<SortUnwrapped
|
<Sort
|
||||||
qsConfig={qsConfigNumDown}
|
qsConfig={qsConfigNumDown}
|
||||||
columns={numericColumns}
|
columns={numericColumns}
|
||||||
onSort={jest.fn()}
|
onSort={jest.fn()}
|
||||||
@@ -187,7 +187,7 @@ describe('<Sort />', () => {
|
|||||||
const numericColumns = [{ name: 'ID', key: 'id' }];
|
const numericColumns = [{ name: 'ID', key: 'id' }];
|
||||||
|
|
||||||
const wrapper = shallow(
|
const wrapper = shallow(
|
||||||
<SortUnwrapped
|
<Sort
|
||||||
qsConfig={qsConfigNumUp}
|
qsConfig={qsConfigNumUp}
|
||||||
columns={numericColumns}
|
columns={numericColumns}
|
||||||
onSort={jest.fn()}
|
onSort={jest.fn()}
|
||||||
@@ -206,7 +206,7 @@ describe('<Sort />', () => {
|
|||||||
const alphaColumns = [{ name: 'Name', key: 'name' }];
|
const alphaColumns = [{ name: 'Name', key: 'name' }];
|
||||||
|
|
||||||
const wrapper = shallow(
|
const wrapper = shallow(
|
||||||
<SortUnwrapped
|
<Sort
|
||||||
qsConfig={qsConfigAlphaDown}
|
qsConfig={qsConfigAlphaDown}
|
||||||
columns={alphaColumns}
|
columns={alphaColumns}
|
||||||
onSort={jest.fn()}
|
onSort={jest.fn()}
|
||||||
@@ -225,7 +225,7 @@ describe('<Sort />', () => {
|
|||||||
const alphaColumns = [{ name: 'Name', key: 'name' }];
|
const alphaColumns = [{ name: 'Name', key: 'name' }];
|
||||||
|
|
||||||
const wrapper = shallow(
|
const wrapper = shallow(
|
||||||
<SortUnwrapped
|
<Sort
|
||||||
qsConfig={qsConfigAlphaDown}
|
qsConfig={qsConfigAlphaDown}
|
||||||
columns={alphaColumns}
|
columns={alphaColumns}
|
||||||
onSort={jest.fn()}
|
onSort={jest.fn()}
|
||||||
|
|||||||
@@ -199,13 +199,6 @@ describe('<UserAndTeamAccessAdd/>', () => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
jest.mock('react-router-dom', () => ({
|
|
||||||
...jest.requireActual('react-router-dom'),
|
|
||||||
useParams: () => ({
|
|
||||||
id: 'a',
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
await act(async () =>
|
await act(async () =>
|
||||||
wrapper.find('SelectableCard[label="Job templates"]').prop('onClick')({
|
wrapper.find('SelectableCard[label="Job templates"]').prop('onClick')({
|
||||||
fetchItems: JobTemplatesAPI.read,
|
fetchItems: JobTemplatesAPI.read,
|
||||||
|
|||||||
@@ -1,21 +1,18 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { shallow } from 'enzyme';
|
||||||
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
|
|
||||||
|
|
||||||
import InstanceGroups from './InstanceGroups';
|
import InstanceGroups from './InstanceGroups';
|
||||||
|
|
||||||
describe('<InstanceGroups/>', () => {
|
describe('<InstanceGroups/>', () => {
|
||||||
let pageWrapper;
|
test('should set breadcrumbs', () => {
|
||||||
let pageSections;
|
const wrapper = shallow(<InstanceGroups />);
|
||||||
|
|
||||||
beforeEach(() => {
|
const header = wrapper.find('ScreenHeader');
|
||||||
pageWrapper = mountWithContexts(<InstanceGroups />);
|
expect(header.prop('streamType')).toEqual('instance_group');
|
||||||
pageSections = pageWrapper.find('PageSection');
|
expect(header.prop('breadcrumbConfig')).toEqual({
|
||||||
});
|
'/instance_groups': 'Instance Groups',
|
||||||
|
'/instance_groups/add': 'Create new instance group',
|
||||||
test('initially renders without crashing', () => {
|
'/instance_groups/container_group/add': 'Create new container group',
|
||||||
expect(pageWrapper.length).toBe(1);
|
});
|
||||||
expect(pageSections.length).toBe(1);
|
|
||||||
expect(pageSections.first().props().variant).toBe('light');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ function Job({ setBreadcrumb }) {
|
|||||||
<PageSection>
|
<PageSection>
|
||||||
<Card>
|
<Card>
|
||||||
<ContentError error={error}>
|
<ContentError error={error}>
|
||||||
{error.response.status === 404 && (
|
{error.response?.status === 404 && (
|
||||||
<span>
|
<span>
|
||||||
{t`The page you requested could not be found.`}{' '}
|
{t`The page you requested could not be found.`}{' '}
|
||||||
<Link to="/jobs">{t`View all Jobs.`}</Link>
|
<Link to="/jobs">{t`View all Jobs.`}</Link>
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import { act } from 'react-dom/test-utils';
|
||||||
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
|
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
|
||||||
|
|
||||||
import Job from './Job';
|
import Job from './Job';
|
||||||
|
|
||||||
|
jest.mock('../../api');
|
||||||
jest.mock('react-router-dom', () => ({
|
jest.mock('react-router-dom', () => ({
|
||||||
...jest.requireActual('react-router-dom'),
|
...jest.requireActual('react-router-dom'),
|
||||||
|
useParams: () => ({
|
||||||
|
id: 1,
|
||||||
|
typeSegment: 'project',
|
||||||
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
describe('<Job />', () => {
|
describe('<Job />', () => {
|
||||||
test('initially renders successfully', () => {
|
test('initially renders successfully', async () => {
|
||||||
mountWithContexts(<Job />);
|
await act(async () => {
|
||||||
|
await mountWithContexts(<Job setBreadcrumb={() => {}} />);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,6 +2,14 @@ import React from 'react';
|
|||||||
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
|
import { mountWithContexts } from '../../../testUtils/enzymeHelpers';
|
||||||
import SettingList from './SettingList';
|
import SettingList from './SettingList';
|
||||||
|
|
||||||
|
jest.mock('../../api');
|
||||||
|
jest.mock('hooks/useBrandName', () => ({
|
||||||
|
__esModule: true,
|
||||||
|
default: () => ({
|
||||||
|
current: 'AWX',
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
describe('<SettingList />', () => {
|
describe('<SettingList />', () => {
|
||||||
let wrapper;
|
let wrapper;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
|||||||
@@ -1,39 +1,11 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Route } from 'react-router-dom';
|
import { shallow } from 'enzyme';
|
||||||
import { act } from 'react-dom/test-utils';
|
|
||||||
import { createMemoryHistory } from 'history';
|
|
||||||
import {
|
|
||||||
mountWithContexts,
|
|
||||||
waitForElement,
|
|
||||||
} from '../../../../testUtils/enzymeHelpers';
|
|
||||||
|
|
||||||
import UserOrganizations from './UserOrganizations';
|
import UserOrganizations from './UserOrganizations';
|
||||||
|
|
||||||
describe('<UserOrganizations />', () => {
|
describe('<UserOrganizations />', () => {
|
||||||
test('userOrganizations mounts successfully', () => {
|
test('should render UserOrganizationList', () => {
|
||||||
const history = createMemoryHistory({
|
const wrapper = shallow(<UserOrganizations />);
|
||||||
initialEntries: ['/users/1/organizations'],
|
expect(wrapper.find('UserOrganizationList')).toHaveLength(1);
|
||||||
});
|
|
||||||
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');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,14 +5,11 @@ import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
|
|||||||
|
|
||||||
import UserTokens from './UserTokens';
|
import UserTokens from './UserTokens';
|
||||||
|
|
||||||
|
jest.mock('../../../api');
|
||||||
|
|
||||||
describe('<UserTokens />', () => {
|
describe('<UserTokens />', () => {
|
||||||
let wrapper;
|
let wrapper;
|
||||||
|
|
||||||
test('renders successfully', () => {
|
|
||||||
wrapper = mountWithContexts(<UserTokens />);
|
|
||||||
expect(wrapper.length).toBe(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('shows Application information modal after successful creation', async () => {
|
test('shows Application information modal after successful creation', async () => {
|
||||||
const history = createMemoryHistory({
|
const history = createMemoryHistory({
|
||||||
initialEntries: ['/users/1/tokens/add'],
|
initialEntries: ['/users/1/tokens/add'],
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ export const asyncFlush = () => new Promise((resolve) => setImmediate(resolve));
|
|||||||
|
|
||||||
let hasConsoleError = false;
|
let hasConsoleError = false;
|
||||||
let hasConsoleWarn = false;
|
let hasConsoleWarn = false;
|
||||||
|
let networkRequestUrl = false;
|
||||||
const { error, warn } = global.console;
|
const { error, warn } = global.console;
|
||||||
|
|
||||||
global.console = {
|
global.console = {
|
||||||
@@ -26,8 +27,10 @@ global.console = {
|
|||||||
// fail tests that log errors.
|
// fail tests that log errors.
|
||||||
// adapted from https://github.com/facebook/jest/issues/6121#issuecomment-708330601
|
// adapted from https://github.com/facebook/jest/issues/6121#issuecomment-708330601
|
||||||
error: (...args) => {
|
error: (...args) => {
|
||||||
hasConsoleError = true;
|
if (!networkRequestUrl) {
|
||||||
error(...args);
|
hasConsoleError = true;
|
||||||
|
error(...args);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
warn: (...args) => {
|
warn: (...args) => {
|
||||||
hasConsoleWarn = true;
|
hasConsoleWarn = true;
|
||||||
@@ -35,7 +38,39 @@ global.console = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const logNetworkRequestError = url => {
|
||||||
|
networkRequestUrl = url || true;
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
data: {},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
jest.mock('axios', () => {
|
||||||
|
return {
|
||||||
|
create: () => ({
|
||||||
|
get: logNetworkRequestError,
|
||||||
|
post: logNetworkRequestError,
|
||||||
|
delete: logNetworkRequestError,
|
||||||
|
put: logNetworkRequestError,
|
||||||
|
patch: logNetworkRequestError,
|
||||||
|
options: logNetworkRequestError,
|
||||||
|
interceptors: {
|
||||||
|
response: {
|
||||||
|
use: () => {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
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) {
|
if (hasConsoleError) {
|
||||||
hasConsoleError = false;
|
hasConsoleError = false;
|
||||||
throw new Error('Error logged to console');
|
throw new Error('Error logged to console');
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ jest.mock('../api/models/WorkflowJobTemplates');
|
|||||||
jest.mock('../api/models/WorkflowJobTemplateNodes');
|
jest.mock('../api/models/WorkflowJobTemplateNodes');
|
||||||
jest.mock('../api/models/CredentialInputSources');
|
jest.mock('../api/models/CredentialInputSources');
|
||||||
jest.mock('../api/models/ExecutionEnvironments');
|
jest.mock('../api/models/ExecutionEnvironments');
|
||||||
|
jest.mock('../api/models/Applications');
|
||||||
|
jest.mock('../api/models/NotificationTemplates');
|
||||||
|
jest.mock('../api/models/Teams');
|
||||||
|
|
||||||
describe('delete details', () => {
|
describe('delete details', () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
Reference in New Issue
Block a user