From d4c9534eccd246f7d2cbfbc58442a0daa35d2fbc Mon Sep 17 00:00:00 2001 From: lucasgarfield Date: Thu, 15 Feb 2024 12:02:06 +0100 Subject: [PATCH] V2Wizard/Details: Add request assertion tests Adds tests to verify requets for images with names and descriptions are formulated correctly. Test functions shared/common to multiple steps were extracted to the test utilities. --- .../steps/Details/Details.test.tsx | 83 +++++++++++++++++++ .../steps/Registration/Registration.test.tsx | 17 +--- .../CreateImageWizardV2/wizardTestUtils.tsx | 38 ++++++++- 3 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 src/test/Components/CreateImageWizardV2/steps/Details/Details.test.tsx diff --git a/src/test/Components/CreateImageWizardV2/steps/Details/Details.test.tsx b/src/test/Components/CreateImageWizardV2/steps/Details/Details.test.tsx new file mode 100644 index 00000000..dfc58a22 --- /dev/null +++ b/src/test/Components/CreateImageWizardV2/steps/Details/Details.test.tsx @@ -0,0 +1,83 @@ +import { screen } from '@testing-library/react'; +import { userEvent } from '@testing-library/user-event'; + +import { CREATE_BLUEPRINT } from '../../../../../constants'; +import { clickNext } from '../../../../testUtils'; +import { + blueprintRequest, + clickRegisterLater, + enterBlueprintName, + goToRegistrationStep, + interceptBlueprintRequest, + render, +} from '../../wizardTestUtils'; + +jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({ + useChrome: () => ({ + auth: { + getUser: () => { + return { + identity: { + internal: { + org_id: 5, + }, + }, + }; + }, + }, + isBeta: () => false, + isProd: () => true, + getEnvironment: () => 'prod', + }), +})); + +const goToDetailsStep = async () => { + await clickNext(); + await clickNext(); + await clickNext(); +}; + +const enterBlueprintDescription = async () => { + const blueprintDescription = await screen.findByRole('textbox', { + name: /blueprint description/i, + }); + await userEvent.type(blueprintDescription, 'Now with extra carmine!'); +}; + +const goToReviewStep = async () => { + await clickNext(); +}; + +describe('registration request generated correctly', () => { + test('without description', async () => { + await render(); + await goToRegistrationStep(); + await clickRegisterLater(); + await goToDetailsStep(); + await enterBlueprintName(); + await goToReviewStep(); + const receivedRequest = await interceptBlueprintRequest(CREATE_BLUEPRINT); + + const expectedRequest = { ...blueprintRequest }; + + expect(receivedRequest).toEqual(expectedRequest); + }); + + test('with description', async () => { + await render(); + await goToRegistrationStep(); + await clickRegisterLater(); + await goToDetailsStep(); + await enterBlueprintName(); + await enterBlueprintDescription(); + await goToReviewStep(); + const receivedRequest = await interceptBlueprintRequest(CREATE_BLUEPRINT); + + const expectedRequest = { + ...blueprintRequest, + description: 'Now with extra carmine!', + }; + + expect(receivedRequest).toEqual(expectedRequest); + }); +}); diff --git a/src/test/Components/CreateImageWizardV2/steps/Registration/Registration.test.tsx b/src/test/Components/CreateImageWizardV2/steps/Registration/Registration.test.tsx index 1cdd6fc9..7b9f5936 100644 --- a/src/test/Components/CreateImageWizardV2/steps/Registration/Registration.test.tsx +++ b/src/test/Components/CreateImageWizardV2/steps/Registration/Registration.test.tsx @@ -11,6 +11,8 @@ import { enterBlueprintName, render, interceptBlueprintRequest, + goToRegistrationStep, + clickRegisterLater, } from '../../wizardTestUtils'; jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({ @@ -32,14 +34,6 @@ jest.mock('@redhat-cloud-services/frontend-components/useChrome', () => ({ }), })); -const goToRegistrationStep = async () => { - const bareMetalCheckBox = await screen.findByRole('checkbox', { - name: /bare metal installer checkbox/i, - }); - await userEvent.click(bareMetalCheckBox); - await clickNext(); -}; - const selectActivationKey = async () => { const activationKeyDropdown = await screen.findByRole('textbox', { name: 'Select activation key', @@ -70,13 +64,6 @@ const deselectPredictiveAnalytics = async () => { await userEvent.click(checkBox); }; -const clickRegisterLater = async () => { - const radioButton = await screen.findByRole('radio', { - name: 'Register later', - }); - await userEvent.click(radioButton); -}; - const goToReviewStep = async () => { await clickNext(); await clickNext(); diff --git a/src/test/Components/CreateImageWizardV2/wizardTestUtils.tsx b/src/test/Components/CreateImageWizardV2/wizardTestUtils.tsx index 9315a71e..2aca2597 100644 --- a/src/test/Components/CreateImageWizardV2/wizardTestUtils.tsx +++ b/src/test/Components/CreateImageWizardV2/wizardTestUtils.tsx @@ -5,8 +5,12 @@ import { userEvent } from '@testing-library/user-event'; import { MockedRequest } from 'msw'; import CreateImageWizard from '../../../Components/CreateImageWizardV2/CreateImageWizard'; +import { + CreateBlueprintRequest, + ImageRequest, +} from '../../../store/imageBuilderApi'; import { server } from '../../mocks/server'; -import { renderCustomRoutesWithReduxRouter } from '../../testUtils'; +import { clickNext, renderCustomRoutesWithReduxRouter } from '../../testUtils'; export function spyOnRequest(pathname: string) { return new Promise((resolve) => { @@ -34,10 +38,42 @@ const routes = [ }, ]; +export const imageRequest: ImageRequest = { + architecture: 'x86_64', + image_type: 'image-installer', + upload_request: { + options: {}, + type: 'aws.s3', + }, +}; + +export const blueprintRequest: CreateBlueprintRequest = { + name: 'Red Velvet', + description: '', + distribution: 'rhel-93', + image_requests: [imageRequest], + customizations: {}, +}; + export const render = async () => { await renderCustomRoutesWithReduxRouter('imagewizard', {}, routes); }; +export const goToRegistrationStep = async () => { + const bareMetalCheckBox = await screen.findByRole('checkbox', { + name: /bare metal installer checkbox/i, + }); + await userEvent.click(bareMetalCheckBox); + await clickNext(); +}; + +export const clickRegisterLater = async () => { + const radioButton = await screen.findByRole('radio', { + name: 'Register later', + }); + await userEvent.click(radioButton); +}; + export const enterBlueprintName = async () => { const blueprintName = await screen.findByRole('textbox', { name: /blueprint name/i,