test: Fix problem with mocked server.use calls

Test `component renders error state correctly` was outputting errors for both AWS and Azure tests.
This was caused by calling `server.use()` after the wizard was already rendered.

Fixed by moving the one time server mock response to the very beginning of the unit test. Also added `server.resetHandlers()` to be run after each of the unit tests.

Thanks to this fix, azure tests could be merged again. Which means no more splitting into `CreateImageWizard.azure.test.js` and `CreateImageWizard.azure.2.test.js`
This commit is contained in:
regexowl 2023-08-28 16:26:40 +02:00 committed by Lucas Garfield
parent e71d98da08
commit 38e2852734
3 changed files with 33 additions and 99 deletions

View file

@ -1,86 +0,0 @@
import React from 'react';
import '@testing-library/jest-dom';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { rest } from 'msw';
import CreateImageWizard from '../../../Components/CreateImageWizard/CreateImageWizard';
import ShareImageModal from '../../../Components/ShareImageModal/ShareImageModal';
import { PROVISIONING_API } from '../../../constants.js';
import { server } from '../../mocks/server.js';
import { clickNext, renderCustomRoutesWithReduxRouter } from '../../testUtils';
const routes = [
{
path: 'insights/image-builder/*',
element: <div />,
},
{
path: 'insights/image-builder/imagewizard/:composeId?',
element: <CreateImageWizard />,
},
{
path: 'insights/image-builder/share/:composeId',
element: <ShareImageModal />,
},
];
jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({
useChrome: () => ({
auth: {
getUser: () => {
return {
identity: {
internal: {
org_id: 5,
},
},
};
},
},
isBeta: () => false,
isProd: () => true,
getEnvironment: () => 'prod',
}),
}));
describe('Step Upload to Azure', () => {
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
jest.clearAllMocks();
});
const user = userEvent.setup();
const setUp = async () => {
renderCustomRoutesWithReduxRouter('imagewizard', {}, routes);
// select Azure as upload destination
await user.click(screen.getByTestId('upload-azure'));
await clickNext();
expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent(
'Target environment - Microsoft Azure'
);
};
test('component renders error state correctly', async () => {
setUp();
server.use(
rest.get(`${PROVISIONING_API}/sources`, (req, res, ctx) =>
res(ctx.status(500))
)
);
await screen.findByText(
/Sources cannot be reached, try again later or enter an account info for upload manually\./i
);
//
});
// set test timeout on 10 seconds
}, 15000);

View file

@ -3,9 +3,12 @@ import '@testing-library/jest-dom';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { rest } from 'msw';
import CreateImageWizard from '../../../Components/CreateImageWizard/CreateImageWizard';
import ShareImageModal from '../../../Components/ShareImageModal/ShareImageModal';
import { PROVISIONING_API } from '../../../constants';
import { server } from '../../mocks/server';
import {
clickBack,
clickNext,
@ -49,6 +52,17 @@ jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({
let router = undefined;
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
jest.clearAllMocks();
router = undefined;
server.resetHandlers();
});
describe('Step Upload to Azure', () => {
const getSourceDropdown = async () => {
const sourceDropdown = await screen.findByRole('textbox', {
@ -60,16 +74,6 @@ describe('Step Upload to Azure', () => {
return sourceDropdown;
};
beforeAll(() => {
// scrollTo is not defined in jsdom
window.HTMLElement.prototype.scrollTo = function () {};
});
afterEach(() => {
jest.clearAllMocks();
router = undefined;
});
const user = userEvent.setup();
const setUp = async () => {
({ router } = renderCustomRoutesWithReduxRouter('imagewizard', {}, routes));
@ -222,5 +226,21 @@ describe('Step Upload to Azure', () => {
expect(groups).toBeInTheDocument();
expect(screen.getByLabelText('Resource group theirGroup2')).toBeVisible();
});
// set test timeout on 10 seconds
test('component renders error state correctly', async () => {
server.use(
rest.get(`${PROVISIONING_API}/sources`, (req, res, ctx) =>
res(ctx.status(500))
)
);
setUp();
await screen.findByText(
/Sources cannot be reached, try again later or enter an account info for upload manually\./i
);
//
});
// set test timeout to 15 seconds
}, 15000);

View file

@ -114,6 +114,7 @@ beforeAll(() => {
afterEach(() => {
jest.clearAllMocks();
router = undefined;
server.resetHandlers();
});
describe('Create Image Wizard', () => {
@ -319,13 +320,12 @@ describe('Step Upload to AWS', () => {
});
test('component renders error state correctly', async () => {
await setUp();
server.use(
rest.get(`${PROVISIONING_API}/sources`, (req, res, ctx) =>
res(ctx.status(500))
)
);
await setUp();
await screen.findByText(
/sources cannot be reached, try again later or enter an aws account id manually\./i
);