V2Wizard/Registration: Add request assertion tests

We need to start using `undefined` as the default state for when a value
has not been defined. Previously we had used things like `’’` for string
typed values. But this causes problems later when generating the request
we send to image-builder. Using `undefined` is explicit and will make
generating the requests much easier (as we don’t need to check for `’’`,
determine the intent, and convert it to undefined if necessary).
Explicit is better than implicit.

With that in mind, tests have been added to ensure that the correct
request is sent to the API for every option on the Registration step.
This is facilitated using a new `spyOnRequest()` function. In the
future, we will have similar tests for the rest of the steps.

A few other minor things:

1. We need to get the `store` using `useStore()`
for when we later call `store.getState()` because the tests use a different
store that is configured in the renderer than the one we were importing.

2. In the wizardSlice, a new type RegistrationType is added that provides
additional type safety instead of using `string`.
This commit is contained in:
lucasgarfield 2024-02-13 19:51:25 +01:00 committed by Lucas Garfield
parent 841edd195a
commit 8c31e6f54f
7 changed files with 320 additions and 16 deletions

View file

@ -12,6 +12,7 @@ import {
} from '@patternfly/react-core';
import { SpinnerIcon } from '@patternfly/react-icons';
import { useChrome } from '@redhat-cloud-services/frontend-components/useChrome';
import { useStore } from 'react-redux';
import { useNavigate, useParams } from 'react-router-dom';
import {
@ -33,7 +34,7 @@ const ReviewWizardFooter = () => {
const navigate = useNavigate();
const { composeId } = useParams();
const [isOpen, setIsOpen] = useState(false);
const store = useStore();
const onToggleClick = () => {
setIsOpen(!isOpen);
};
@ -47,7 +48,7 @@ const ReviewWizardFooter = () => {
const getBlueprintPayload = async () => {
const userData = await auth?.getUser();
const orgId = userData?.identity?.internal?.org_id;
const requestBody = orgId && mapRequestFromState(orgId);
const requestBody = orgId && mapRequestFromState(store, orgId);
return requestBody;
};

View file

@ -1,4 +1,6 @@
import { RootState, store } from '../../../store';
import { Store } from 'redux';
import { RootState } from '../../../store';
import {
AwsUploadRequestOptions,
CreateBlueprintRequest,
@ -18,7 +20,6 @@ import {
selectBaseUrl,
selectBlueprintDescription,
selectBlueprintName,
selectCustomRepositories,
selectDistribution,
selectGcpAccountType,
selectGcpEmail,
@ -33,7 +34,10 @@ import {
* @param {string} orgID organization ID
* @returns {CreateBlueprintRequest} blueprint creation request payload
*/
export const mapRequestFromState = (orgID: string): CreateBlueprintRequest => {
export const mapRequestFromState = (
store: Store,
orgID: string
): CreateBlueprintRequest => {
const state = store.getState();
const imageRequests = getImageRequests(state);
const customizations = getCustomizations(state, orgID);
@ -127,7 +131,7 @@ const getCustomizations = (state: RootState, orgID: string): Customizations => {
subscription: getSubscription(state, orgID),
packages: undefined,
payload_repositories: undefined,
custom_repositories: selectCustomRepositories(state),
custom_repositories: undefined,
openscap: undefined,
filesystem: undefined,
users: undefined,
@ -146,19 +150,36 @@ const getCustomizations = (state: RootState, orgID: string): Customizations => {
};
};
const getSubscription = (state: RootState, orgID: string): Subscription => {
const getSubscription = (
state: RootState,
orgID: string
): Subscription | undefined => {
const registrationType = selectRegistrationType(state);
const activationKey = selectActivationKey(state);
if (registrationType === 'register-later') {
return undefined;
}
if (activationKey === undefined) {
throw new Error(
'Activation key unexpectedly undefined while generating subscription customization'
);
}
const initialSubscription = {
'activation-key': selectActivationKey(state) || '',
'activation-key': activationKey,
organization: Number(orgID),
'server-url': selectServerUrl(state),
'base-url': selectBaseUrl(state),
};
switch (selectRegistrationType(state)) {
case 'register-now-insights':
return { ...initialSubscription, insights: true };
switch (registrationType) {
case 'register-now-rhc':
return { ...initialSubscription, insights: true, rhc: true };
default:
return { ...initialSubscription, insights: false };
case 'register-now-insights':
return { ...initialSubscription, insights: true, rhc: false };
case 'register-now':
return { ...initialSubscription, insights: false, rhc: false };
}
};