Refactor default value for select (#5343)
In this PR, we are refactoring two things: - leverage field.defaultValue for Select and MultiSelect settings form (instead of option.isDefault) - use quoted string (ex: "'USD'") for string default values to embrace backend format --------- Co-authored-by: Thaïs Guigon <guigon.thais@gmail.com>
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
import { applySimpleQuotesToString } from '../applySimpleQuotesToString';
|
||||
|
||||
describe('applySimpleQuotesToString', () => {
|
||||
it('wraps the input string with single quotes', () => {
|
||||
const input = 'Hello, World!';
|
||||
|
||||
const result = applySimpleQuotesToString(input);
|
||||
|
||||
expect(result).toBe(`'${input}'`);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,32 @@
|
||||
import { stripSimpleQuotesFromString } from '../stripSimpleQuotesFromString';
|
||||
|
||||
describe('stripSimpleQuotesFromString', () => {
|
||||
it('removes surrounding single quotes from a string', () => {
|
||||
// Given
|
||||
const input = "'Hello, World!'";
|
||||
|
||||
// When
|
||||
const output = stripSimpleQuotesFromString(input);
|
||||
|
||||
// Then
|
||||
expect(output).toBe('Hello, World!');
|
||||
});
|
||||
|
||||
it.each([
|
||||
// Given
|
||||
['no simple quotes'],
|
||||
["'only at start"],
|
||||
["only at end'"],
|
||||
["mid'dle"],
|
||||
[''],
|
||||
])(
|
||||
'returns the input without changes if the string does not start and end with single quotes (%s)',
|
||||
(input) => {
|
||||
// When
|
||||
const output = stripSimpleQuotesFromString(input);
|
||||
|
||||
// Then
|
||||
expect(output).toBe(input);
|
||||
},
|
||||
);
|
||||
});
|
||||
@ -0,0 +1,3 @@
|
||||
export const applySimpleQuotesToString = <T extends string>(
|
||||
value: T,
|
||||
): `'${T}'` => `'${value}'`;
|
||||
@ -0,0 +1,8 @@
|
||||
import { simpleQuotesStringSchema } from '~/utils/validation-schemas/simpleQuotesStringSchema';
|
||||
|
||||
export const stripSimpleQuotesFromString = <Input extends string>(
|
||||
value: Input,
|
||||
) =>
|
||||
(simpleQuotesStringSchema.safeParse(value).success
|
||||
? value.slice(1, -1)
|
||||
: value) as Input extends `'${infer Output}'` ? Output : Input;
|
||||
@ -0,0 +1,38 @@
|
||||
import { SafeParseError } from 'zod';
|
||||
|
||||
import { simpleQuotesStringSchema } from '../simpleQuotesStringSchema';
|
||||
|
||||
describe('simpleQuotesStringSchema', () => {
|
||||
it('validates a string with simple quotes', () => {
|
||||
// Given
|
||||
const input = "'with simple quotes'";
|
||||
|
||||
// When
|
||||
const result = simpleQuotesStringSchema.parse(input);
|
||||
|
||||
// Then
|
||||
expect(result).toBe(input);
|
||||
});
|
||||
|
||||
it.each([
|
||||
// Given
|
||||
['no simple quotes'],
|
||||
["'only at start"],
|
||||
["only at end'"],
|
||||
["mid'dle"],
|
||||
[''],
|
||||
])('fails for strings not wrapped in simple quotes (%s)', (input) => {
|
||||
// When
|
||||
const result = simpleQuotesStringSchema.safeParse(input);
|
||||
|
||||
// Then
|
||||
expect(result.success).toBe(false);
|
||||
expect((result as SafeParseError<string>).error.errors).toEqual([
|
||||
{
|
||||
code: 'custom',
|
||||
message: 'String should be wrapped in simple quotes',
|
||||
path: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,15 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const simpleQuotesStringSchema: z.ZodType<
|
||||
`'${string}'`,
|
||||
z.ZodTypeDef,
|
||||
string
|
||||
> = z
|
||||
.string()
|
||||
.refine(
|
||||
(value: string): value is `'${string}'` =>
|
||||
value.startsWith("'") && value.endsWith("'"),
|
||||
{
|
||||
message: 'String should be wrapped in simple quotes',
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user