CreateImageWizard: target env validation

Validation is not enforced on the image output step
when selecting and then deselecting an image type. This
is because the only validation which is set is `Required`.
However, after deselecting an image type the input becomes
a key-value object with all the values set to false. This
commit adds a custom validator to check that at least one
value in the object is set to true. Fixes #575
This commit is contained in:
Gianluca Zuccarelli 2022-03-24 15:43:21 +00:00 committed by jkozol
parent 787e1ba5ce
commit 78123bff0e
5 changed files with 40 additions and 1 deletions

View file

@ -24,6 +24,7 @@ import {
import {
fileSystemConfigurationValidator,
targetEnvironmentValidator,
} from './validators';
const handleKeyDown = (e, handleClose) => {
@ -433,7 +434,7 @@ const CreateImageWizard = () => {
});
} }
defaultArch="x86_64"
customValidatorMapper={ { fileSystemConfigurationValidator, } }
customValidatorMapper={ { fileSystemConfigurationValidator, targetEnvironmentValidator } }
schema={ {
fields: [
{

View file

@ -50,6 +50,9 @@ export default {
validate: [
{
type: validatorTypes.REQUIRED
},
{
type: 'targetEnvironmentValidator'
}
],
}

View file

@ -1 +1,2 @@
export { default as fileSystemConfigurationValidator } from './fileSystemConfigurationValidator';
export { default as targetEnvironmentValidator } from './targetEnvironmentValidator';

View file

@ -0,0 +1,14 @@
const TargetEnvironmentValidator = () => targets => {
if (!targets) {
return undefined;
}
// at least one of the target environments must
// be set to true. This reduces the value to
// a single boolean which is a flag for whether
// at least one target has been selected or not
let valid = Object.values(targets).reduce((prev, curr) => curr || prev, false);
return !valid ? 'Please select an image' : undefined;
};
export default TargetEnvironmentValidator;

View file

@ -221,6 +221,26 @@ describe('Step Image output', () => {
expect(destination).toBeEnabled();
expect(destination).toContainElement(required);
});
test('selecting and deselecting a tile disables the next button', () => {
setUp();
const [ next, , ] = verifyButtons();
const awsTile = screen.getByTestId('upload-aws');
// this has already been clicked once in the setup function
awsTile.click(); // deselect
const googleTile = screen.getByTestId('upload-google');
googleTile.click(); // select
googleTile.click(); // deselect
const azureTile = screen.getByTestId('upload-azure');
azureTile.click(); // select
azureTile.click(); // deselect
expect(next).toBeDisabled();
});
});
describe('Step Upload to AWS', () => {