diff --git a/awx/ui_next/src/screens/Organization/OrganizationAdd/OrganizationAdd.test.jsx b/awx/ui_next/src/screens/Organization/OrganizationAdd/OrganizationAdd.test.jsx index 72bffd067d..2a6ffb2b28 100644 --- a/awx/ui_next/src/screens/Organization/OrganizationAdd/OrganizationAdd.test.jsx +++ b/awx/ui_next/src/screens/Organization/OrganizationAdd/OrganizationAdd.test.jsx @@ -1,10 +1,9 @@ import React from 'react'; - +import { createMemoryHistory } from 'history'; import { mountWithContexts, waitForElement, } from '../../../../testUtils/enzymeHelpers'; - import OrganizationAdd from './OrganizationAdd'; import { OrganizationsAPI } from '../../../api'; @@ -27,33 +26,25 @@ describe('', () => { }); test('should navigate to organizations list when cancel is clicked', () => { - const history = { - push: jest.fn(), - }; + const history = createMemoryHistory({}); const wrapper = mountWithContexts(, { context: { router: { history } }, }); - expect(history.push).not.toHaveBeenCalled(); wrapper.find('button[aria-label="Cancel"]').prop('onClick')(); - expect(history.push).toHaveBeenCalledWith('/organizations'); + expect(history.location.pathname).toEqual('/organizations'); }); test('should navigate to organizations list when close (x) is clicked', () => { - const history = { - push: jest.fn(), - }; + const history = createMemoryHistory({}); const wrapper = mountWithContexts(, { context: { router: { history } }, }); - expect(history.push).not.toHaveBeenCalled(); wrapper.find('button[aria-label="Close"]').prop('onClick')(); - expect(history.push).toHaveBeenCalledWith('/organizations'); + expect(history.location.pathname).toEqual('/organizations'); }); - test('successful form submission should trigger redirect', async done => { - const history = { - push: jest.fn(), - }; + test('successful form submission should trigger redirect', async () => { + const history = createMemoryHistory({}); const orgData = { name: 'new name', description: 'new description', @@ -77,11 +68,10 @@ describe('', () => { [3], [] ); - expect(history.push).toHaveBeenCalledWith('/organizations/5'); - done(); + expect(history.location.pathname).toEqual('/organizations/5'); }); - test('handleSubmit should post instance groups', async done => { + test('handleSubmit should post instance groups', async () => { const orgData = { name: 'new name', description: 'new description', @@ -104,7 +94,6 @@ describe('', () => { [] ); expect(OrganizationsAPI.associateInstanceGroup).toHaveBeenCalledWith(5, 3); - done(); }); test('AnsibleSelect component renders if there are virtual environments', () => { diff --git a/awx/ui_next/src/screens/Organization/OrganizationEdit/OrganizationEdit.test.jsx b/awx/ui_next/src/screens/Organization/OrganizationEdit/OrganizationEdit.test.jsx index 102b49392f..0a9a4f9e82 100644 --- a/awx/ui_next/src/screens/Organization/OrganizationEdit/OrganizationEdit.test.jsx +++ b/awx/ui_next/src/screens/Organization/OrganizationEdit/OrganizationEdit.test.jsx @@ -1,8 +1,7 @@ import React from 'react'; - +import { createMemoryHistory } from 'history'; import { OrganizationsAPI } from '@api'; import { mountWithContexts } from '@testUtils/enzymeHelpers'; - import OrganizationEdit from './OrganizationEdit'; jest.mock('@api'); @@ -65,17 +64,14 @@ describe('', () => { }); test('should navigate to organization detail when cancel is clicked', () => { - const history = { - push: jest.fn(), - }; + const history = createMemoryHistory({}); const wrapper = mountWithContexts( , { context: { router: { history } } } ); - expect(history.push).not.toHaveBeenCalled(); wrapper.find('button[aria-label="Cancel"]').prop('onClick')(); - expect(history.push).toHaveBeenCalledWith('/organizations/1/details'); + expect(history.location.pathname).toEqual('/organizations/1/details'); }); }); diff --git a/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx b/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx index 41032535bf..1cb52e4e5d 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateAdd/JobTemplateAdd.test.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import { createMemoryHistory } from 'history'; import { mountWithContexts, waitForElement } from '@testUtils/enzymeHelpers'; import { sleep } from '@testUtils/testUtils'; import JobTemplateAdd from './JobTemplateAdd'; @@ -110,9 +111,7 @@ describe('', () => { }); test('should navigate to job template detail after form submission', async () => { - const history = { - push: jest.fn(), - }; + const history = createMemoryHistory({}); JobTemplatesAPI.create.mockResolvedValueOnce({ data: { id: 1, @@ -128,20 +127,18 @@ describe('', () => { jobTemplateData ); await sleep(0); - expect(history.push).toHaveBeenCalledWith( + expect(history.location.pathname).toEqual( '/templates/job_template/1/details' ); }); test('should navigate to templates list when cancel is clicked', async () => { - const history = { - push: jest.fn(), - }; + const history = createMemoryHistory({}); const wrapper = mountWithContexts(, { context: { router: { history } }, }); await waitForElement(wrapper, 'EmptyStateBody', el => el.length === 0); wrapper.find('button[aria-label="Cancel"]').invoke('onClick')(); - expect(history.push).toHaveBeenCalledWith('/templates'); + expect(history.location.pathname).toEqual('/templates'); }); }); diff --git a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx index 363c1ca920..0ae8ea0c01 100644 --- a/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx +++ b/awx/ui_next/src/screens/Template/JobTemplateEdit/JobTemplateEdit.test.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import { createMemoryHistory } from 'history'; import { sleep } from '@testUtils/testUtils'; import { mountWithContexts, waitForElement } from '@testUtils/enzymeHelpers'; import { JobTemplatesAPI, LabelsAPI, ProjectsAPI } from '@api'; @@ -163,7 +164,7 @@ describe('', () => { await waitForElement(wrapper, 'EmptyStateBody', el => el.length === 0); }); - test('handleSubmit should call api update', async done => { + test('handleSubmit should call api update', async () => { const wrapper = mountWithContexts( ); @@ -206,11 +207,10 @@ describe('', () => { expect(JobTemplatesAPI.disassociateLabel).toHaveBeenCalledTimes(2); expect(JobTemplatesAPI.associateLabel).toHaveBeenCalledTimes(2); expect(JobTemplatesAPI.generateLabel).toHaveBeenCalledTimes(2); - done(); }); - test('should navigate to job template detail when cancel is clicked', async done => { - const history = { push: jest.fn() }; + test('should navigate to job template detail when cancel is clicked', async () => { + const history = createMemoryHistory({}); const wrapper = mountWithContexts( , { context: { router: { history } } } @@ -220,11 +220,9 @@ describe('', () => { 'button[aria-label="Cancel"]', e => e.length === 1 ); - expect(history.push).not.toHaveBeenCalled(); cancelButton.prop('onClick')(); - expect(history.push).toHaveBeenCalledWith( + expect(history.location.pathname).toEqual( '/templates/job_template/1/details' ); - done(); }); }); diff --git a/awx/ui_next/testUtils/enzymeHelpers.test.jsx b/awx/ui_next/testUtils/enzymeHelpers.test.jsx index 0b830e8634..f5caaf5f75 100644 --- a/awx/ui_next/testUtils/enzymeHelpers.test.jsx +++ b/awx/ui_next/testUtils/enzymeHelpers.test.jsx @@ -1,4 +1,5 @@ import React, { Component } from 'react'; +import { createMemoryHistory } from 'history'; import { Link } from 'react-router-dom'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; @@ -21,7 +22,7 @@ describe('mountWithContexts', () => { const Child = withI18n()(({ i18n }) => (
{i18n._(t`Text content`)}
)); - const Parent = () => (); + const Parent = () => ; const wrapper = mountWithContexts(); expect(wrapper.find('Parent')).toMatchSnapshot(); }); @@ -41,23 +42,17 @@ describe('mountWithContexts', () => { it('should mount and render with stubbed context', () => { const context = { router: { - history: { - push: jest.fn(), - replace: jest.fn(), - createHref: jest.fn(), - }, + history: createMemoryHistory({}), route: { location: {}, - match: {} - } - } + match: {}, + }, + }, }; const wrapper = mountWithContexts( - ( -
- home -
- ), +
+ home +
, { context } ); @@ -66,7 +61,7 @@ describe('mountWithContexts', () => { link.simulate('click', { button: 0 }); wrapper.update(); - expect(context.router.history.push).toHaveBeenCalledWith('/'); + expect(context.router.history.location.pathname).toEqual('/'); }); }); @@ -101,10 +96,7 @@ describe('mountWithContexts', () => { )} ); - const wrapper = mountWithContexts( - , - { context: { config } } - ); + const wrapper = mountWithContexts(, { context: { config } }); expect(wrapper.find('Foo')).toMatchSnapshot(); }); }); @@ -115,26 +107,26 @@ describe('mountWithContexts', () => { * after a short amount of time. */ class TestAsyncComponent extends Component { - constructor (props) { + constructor(props) { super(props); this.state = { displayElement: false }; } - componentDidMount () { + componentDidMount() { setTimeout(() => this.setState({ displayElement: true }), 500); } - render () { + render() { const { displayElement } = this.state; if (displayElement) { - return (
); + return
; } return null; } } describe('waitForElement', () => { - it('waits for the element and returns it', async (done) => { + it('waits for the element and returns it', async done => { const selector = '#test-async-component'; const wrapper = mountWithContexts(); expect(wrapper.exists(selector)).toEqual(false); @@ -145,7 +137,7 @@ describe('waitForElement', () => { done(); }); - it('eventually throws an error for elements that don\'t exist', async (done) => { + it("eventually throws an error for elements that don't exist", async done => { const wrapper = mountWithContexts(
); let error; @@ -154,7 +146,11 @@ describe('waitForElement', () => { } catch (err) { error = err; } finally { - expect(error).toEqual(new Error('Expected condition for <#does-not-exist> not met: el => el.length === 1')); + expect(error).toEqual( + new Error( + 'Expected condition for <#does-not-exist> not met: el => el.length === 1' + ) + ); done(); } });