Skip to content

Commit

Permalink
fix(select): Remove creatable logic (#62)
Browse files Browse the repository at this point in the history
* chore: update schema

* fix: tests

* fix

* chore: remove test and mocks

---------

Co-authored-by: Joe Ng'ethe <[email protected]>
  • Loading branch information
remotecom and joe-ngethe-remote authored Jan 19, 2024
1 parent 07c6808 commit 0a6273c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 77 deletions.
81 changes: 19 additions & 62 deletions src/tests/createHeadlessForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
schemaInputTypeRadioOptionsWithDetails,
schemaInputTypeSelectSoloDeprecated,
schemaInputTypeSelectSolo,
schemaInputTypeSelectString,
schemaInputTypeSelectMultipleDeprecated,
schemaInputTypeSelectMultiple,
schemaInputTypeSelectMultipleOptional,
Expand Down Expand Up @@ -432,42 +431,35 @@ describe('createHeadlessForm', () => {
});

describe('field support', () => {
function assertOptionsAllowed({ handleValidation, fieldName, validOptions, isString = false }) {
function assertOptionsAllowed({ handleValidation, fieldName, validOptions }) {
const validateForm = (vals) => friendlyError(handleValidation(vals));

// All allowed options are valid
validOptions.forEach((value) => {
expect(validateForm({ [fieldName]: value })).toBeUndefined();
});

if (!isString) {
// Any other arbitrary value is not valid.
expect(validateForm({ [fieldName]: 'blah-blah' })).toEqual({
[fieldName]: 'The option "blah-blah" is not valid.',
});

// Given undefined, it says it's a required field.
expect(validateForm({})).toEqual({
[fieldName]: 'Required field',
});
// Any other arbitrary value is not valid.
expect(validateForm({ [fieldName]: 'blah-blah' })).toEqual({
[fieldName]: 'The option "blah-blah" is not valid.',
});

// As required field, empty string ("") is also considered empty. @BUG RMT-518
// Expectation: The error to be "The option '' is not valid."
expect(validateForm({ [fieldName]: '' })).toEqual({
[fieldName]: 'Required field',
});
// Given undefined, it says it's a required field.
expect(validateForm({})).toEqual({
[fieldName]: 'Required field',
});

// As required field, null is also considered empty @BUG RMT-518
// Expectation: The error to be "The option null is not valid."
expect(validateForm({ [fieldName]: null })).toEqual({
[fieldName]: 'Required field',
});
}
// As required field, empty string ("") is also considered empty. @BUG RMT-518
// Expectation: The error to be "The option '' is not valid."
expect(validateForm({ [fieldName]: '' })).toEqual({
[fieldName]: 'Required field',
});

if (isString) {
// Any other arbitrary value is valid.
expect(validateForm({ [fieldName]: 'blah-blah' })).toBeUndefined();
}
// As required field, null is also considered empty @BUG RMT-518
// Expectation: The error to be "The option null is not valid."
expect(validateForm({ [fieldName]: null })).toEqual({
[fieldName]: 'Required field',
});
}

it('support "text" field type', () => {
Expand Down Expand Up @@ -639,41 +631,6 @@ describe('createHeadlessForm', () => {
});
});

it('supports "select" field type with string option', () => {
const { fields, handleValidation } = createHeadlessForm(schemaInputTypeSelectString);
const fieldSelect = fields[0];
expect(fieldSelect).toMatchObject({
name: 'browsers',
label: 'Browsers (solo)',
description: 'This solo select also includes a disabled option.',
options: [
{
value: 'chr',
label: 'Chrome',
},
{
value: 'ff',
label: 'Firefox',
},
{
value: 'ie',
label: 'Internet Explorer',
disabled: true,
},
{ value: undefined, type: 'string', label: '{Create another}' },
],
});

expect(fieldSelect).not.toHaveProperty('multiple');

assertOptionsAllowed({
handleValidation,
fieldName: 'browsers',
validOptions: ['chr', 'ff', 'ie'],
isString: true,
});
});

it('supports "select" field type with multiple options @deprecated', () => {
const result = createHeadlessForm(schemaInputTypeSelectMultipleDeprecated);
expect(result).toMatchObject({
Expand Down
9 changes: 0 additions & 9 deletions src/tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,6 @@ export const mockSelectInputSolo = {
},
};

export const mockSelectInputSoloCreatable = {
...mockSelectInputSolo,
oneOf: [...mockSelectInputSolo.oneOf, { type: 'string', title: '{Create another}' }],
};

export const mockSelectInputMultiple = {
title: 'Browsers (multiple)',
description: 'This multi-select also includes a disabled option.',
Expand Down Expand Up @@ -942,10 +937,6 @@ export const schemaInputTypeSelectSolo = JSONSchemaBuilder()
.setRequiredFields(['browsers'])
.build();

export const schemaInputTypeSelectString = JSONSchemaBuilder().addInput({
browsers: mockSelectInputSoloCreatable,
});

/** @deprecated */
export const schemaInputTypeSelectMultipleDeprecated = JSONSchemaBuilder()
.addInput({
Expand Down
8 changes: 2 additions & 6 deletions src/yupSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ const validateMaxDate = (value, minDate) => {

const yupSchemas = {
text: validateOnlyStrings,
radioOrSelect: (options, isString) => {
if (isString) {
return string().nullable();
}
radioOrSelect: (options) => {
return string()
.nullable()
.transform((value) => {
Expand Down Expand Up @@ -215,9 +212,8 @@ const getYupSchema = ({ inputType, ...field }) => {
const jsonType = getJsonTypeInArray(field.jsonType);

if (field.options?.length > 0) {
const isString = field.options?.findIndex((option) => option.type === 'string') > -1;
const optionValues = getOptions(field);
return yupSchemas.radioOrSelect(optionValues, isString);
return yupSchemas.radioOrSelect(optionValues);
}

if (field.format === 'date') {
Expand Down

0 comments on commit 0a6273c

Please sign in to comment.