diff --git a/.eslintrc.yml b/.eslintrc.yml index 7fff977d..0ece5295 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -86,7 +86,9 @@ rules: - error - functions: false no-undef: 2 - no-unused-vars: 2 + no-unused-vars: + - 2 + - varsIgnorePattern: "^_" no-var: 2 no-with: 2 object-shorthand: 2 diff --git a/src/test/SmartComponents/CreateImageWizard/CreateImageWizard.test.js b/src/test/SmartComponents/CreateImageWizard/CreateImageWizard.test.js index 9c207ade..a86d8539 100644 --- a/src/test/SmartComponents/CreateImageWizard/CreateImageWizard.test.js +++ b/src/test/SmartComponents/CreateImageWizard/CreateImageWizard.test.js @@ -1,12 +1,14 @@ import '@testing-library/jest-dom'; import React from 'react'; -import { screen, getByText, waitFor, waitForElementToBeRemoved } from '@testing-library/react'; +import { screen, getByText, waitForElementToBeRemoved } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { renderWithReduxRouter } from '../../testUtils'; import CreateImageWizard from '../../../SmartComponents/CreateImageWizard/CreateImageWizard'; import api from '../../../api.js'; +let historySpy = undefined; + function verifyButtons() { // these buttons exist everywhere const next = screen.getByRole('button', { name: /Next/ }); @@ -16,16 +18,13 @@ function verifyButtons() { return [ next, back, cancel ]; } -function verifyCancelButton(cancel) { +function verifyCancelButton(cancel, history) { cancel.click(); // this goes back to the landing page - return waitFor( - () => [ - screen.getByTestId('create-image-action'), - screen.getByTestId('images-table'), - ] - ); + // but jsdom will not render the new page so we can't assert on that + expect(history).toHaveBeenCalledTimes(1); + expect(history).toHaveBeenCalledWith('/landing'); } // mock the insights dependency @@ -47,6 +46,11 @@ beforeAll(() => { }; }); +afterEach(() => { + jest.clearAllMocks(); + historySpy = undefined; +}); + // restore global mock afterAll(() => { global.insights = undefined; @@ -73,7 +77,8 @@ describe('Create Image Wizard', () => { describe('Step Release', () => { beforeEach(() => { - renderWithReduxRouter(); + const { _component, history } = renderWithReduxRouter(); + historySpy = jest.spyOn(history, 'push'); // left sidebar navigation const sidebar = screen.getByRole('navigation'); @@ -99,9 +104,9 @@ describe('Step Release', () => { expect(back).toHaveClass('pf-m-disabled'); }); - test('clicking Cancel loads landing page', async () => { + test('clicking Cancel loads landing page', () => { const [ , , cancel ] = verifyButtons(); - await verifyCancelButton(cancel); + verifyCancelButton(cancel, historySpy); }); test('allows chosing a release', () => { @@ -114,7 +119,8 @@ describe('Step Release', () => { describe('Step Target environment', () => { beforeEach(() => { - renderWithReduxRouter(); + const { _component, history } = renderWithReduxRouter(); + historySpy = jest.spyOn(history, 'push'); // left sidebar navigation const sidebar = screen.getByRole('navigation'); @@ -138,9 +144,9 @@ describe('Step Target environment', () => { screen.getByTestId('release-select'); }); - test('clicking Cancel loads landing page', async () => { + test('clicking Cancel loads landing page', () => { const [ , , cancel ] = verifyButtons(); - await verifyCancelButton(cancel); + verifyCancelButton(cancel, historySpy); }); test('choosing S3 shows region and bucket fields', () => { @@ -205,7 +211,8 @@ describe('Step Target environment', () => { describe('Step Registration', () => { beforeEach(() => { - renderWithReduxRouter(); + const { _component, history } = renderWithReduxRouter(); + historySpy = jest.spyOn(history, 'push'); // left sidebar navigation const sidebar = screen.getByRole('navigation'); @@ -230,9 +237,9 @@ describe('Step Registration', () => { screen.getByText('Secret access key'); }); - test('clicking Cancel loads landing page', async () => { + test('clicking Cancel loads landing page', () => { const [ , , cancel ] = verifyButtons(); - await verifyCancelButton(cancel); + verifyCancelButton(cancel, historySpy); }); test('should allow choosing activation keys', () => { @@ -274,7 +281,8 @@ describe('Step Registration', () => { describe('Step Review', () => { beforeEach(() => { - renderWithReduxRouter(); + const { _component, history } = renderWithReduxRouter(); + historySpy = jest.spyOn(history, 'push'); // left sidebar navigation const sidebar = screen.getByRole('navigation'); @@ -297,9 +305,9 @@ describe('Step Review', () => { screen.getByText('Register the system'); }); - test('clicking Cancel loads landing page', async () => { + test('clicking Cancel loads landing page', () => { const cancel = screen.getByRole('button', { name: /Cancel/ }); - await verifyCancelButton(cancel); + verifyCancelButton(cancel, historySpy); }); }); diff --git a/src/test/testUtils.js b/src/test/testUtils.js index fa2c443d..4814e221 100644 --- a/src/test/testUtils.js +++ b/src/test/testUtils.js @@ -18,6 +18,7 @@ export const renderWithReduxRouter = (component, mockedStore = defaultStore, rou {component} - ) + ), + history }; };