Skip to content

Commit

Permalink
feat: refactor parseUserInput and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Khayal Alasgarov committed Nov 1, 2024
1 parent 1c5600c commit d61938a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/forms/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export { defaultFormConfig } from './patterns/index.js';
import { type PagePattern } from './patterns/page/config.js';
import { type PageSetPattern } from './patterns/page-set/config.js';
export { type RichTextPattern } from './patterns/rich-text.js';
import { type SelectDropdownPattern } from './patterns/select-dropdown.js';
import { type SequencePattern } from './patterns/sequence.js';
import { FieldsetPattern } from './patterns/index.js';
export {
Expand Down
54 changes: 31 additions & 23 deletions packages/forms/src/patterns/select-dropdown/select-dropdown.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { describe, expect, it } from 'vitest';
import { createSchema, selectDropdownConfig, type SelectDropdownPattern } from './select-dropdown';
import {
createSchema,
selectDropdownConfig,
type SelectDropdownPattern,
} from './select-dropdown';

describe('SelectDropdownPattern tests', () => {
describe('createSchema', () => {
Expand All @@ -8,14 +12,14 @@ describe('SelectDropdownPattern tests', () => {
label: 'Test Label',
required: true,
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'value1', label: 'Option 1' },
{ value: 'value2', label: 'Option 2' },
],
};

const schema = createSchema(data);
expect(schema.safeParse('option1').success).toBe(true);
expect(schema.safeParse('option2').success).toBe(true);
expect(schema.safeParse('value1').success).toBe(true);
expect(schema.safeParse('value2').success).toBe(true);
expect(schema.safeParse('invalid').success).toBe(false);
expect(schema.safeParse('').success).toBe(false);
expect(() => schema.parse('')).toThrow();
Expand All @@ -26,14 +30,14 @@ describe('SelectDropdownPattern tests', () => {
label: 'Test Label',
required: false,
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'value1', label: 'Option 1' },
{ value: 'value2', label: 'Option 2' },
],
};

const schema = createSchema(data);
expect(schema.safeParse('option1').success).toBe(true);
expect(schema.safeParse('option2').success).toBe(true);
expect(schema.safeParse('value1').success).toBe(true);
expect(schema.safeParse('value2').success).toBe(true);
expect(schema.safeParse('invalid').success).toBe(false);
expect(schema.safeParse('').success).toBe(true);
});
Expand All @@ -45,7 +49,9 @@ describe('SelectDropdownPattern tests', () => {
options: [],
};

expect(() => createSchema(data)).toThrow('Options must have at least one value');
expect(() => createSchema(data)).toThrow(
'Options must have at least one value'
);
});
});

Expand All @@ -58,18 +64,18 @@ describe('SelectDropdownPattern tests', () => {
label: 'Test Dropdown',
required: true,
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'value1', label: 'Option 1' },
{ value: 'value2', label: 'Option 2' },
],
},
};

const inputObj = { value: 'option1' };
const inputValue = 'value1';
if (selectDropdownConfig.parseUserInput) {
const result = selectDropdownConfig.parseUserInput(pattern, inputObj);
const result = selectDropdownConfig.parseUserInput(pattern, inputValue);
console.log('Test parse result:', result);
if (result.success) {
expect(result.data).toBe('option1');
expect(result.data).toBe('value1');
} else {
throw new Error('Unexpected validation failure');
}
Expand All @@ -86,19 +92,21 @@ describe('SelectDropdownPattern tests', () => {
label: 'Test Dropdown',
required: true,
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'value1', label: 'Option 1' },
{ value: 'value2', label: 'Option 2' },
],
},
};

const inputObj = { value: 'invalid' };
const inputValue = 'invalid';
if (selectDropdownConfig.parseUserInput) {
const result = selectDropdownConfig.parseUserInput(pattern, inputObj);
const result = selectDropdownConfig.parseUserInput(pattern, inputValue);
console.log('Test parse result (error case):', result);
if (!result.success) {
expect(result.error).toBeDefined();
expect(result.error.message).toBe("Invalid enum value. Expected 'option1' | 'option2', received 'invalid'");
expect(result.error.message).toBe(
"Invalid enum value. Expected 'value1' | 'value2', received 'invalid'"
);
} else {
throw new Error('Unexpected validation success');
}
Expand All @@ -112,8 +120,8 @@ describe('SelectDropdownPattern tests', () => {
label: 'Test Dropdown',
required: true,
options: [
{ value: 'option1', label: 'Option 1' },
{ value: 'option2', label: 'Option 2' },
{ value: 'value1', label: 'Option 1' },
{ value: 'value2', label: 'Option 2' },
],
};

Expand Down Expand Up @@ -150,4 +158,4 @@ describe('SelectDropdownPattern tests', () => {
}
});
});
});
});
50 changes: 6 additions & 44 deletions packages/forms/src/patterns/select-dropdown/select-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
validatePattern,
} from '../../pattern.js';
import { getFormSessionValue } from '../../session.js';
import { safeZodParseFormErrors, safeZodParseToFormError } from '../../util/zod.js';
import {
safeZodParseFormErrors,
safeZodParseToFormError,
} from '../../util/zod.js';

const configSchema = z.object({
label: z.string().min(1),
Expand Down Expand Up @@ -59,51 +62,10 @@ export const selectDropdownConfig: PatternConfig<
],
},

parseUserInput: (pattern, inputObj) => {
const expectedInput = inputObj as { value: string };

const schema = createSchema(pattern.data);
try {
const parsedValue = schema.parse(expectedInput.value);
return parsedValue
? { success: true, data: parsedValue }
: {
success: false,
error: {
type: 'custom',
message: 'Parsed select dropdown value is undefined',
},
};
} catch (e) {
const zodError = e as z.ZodError;
return {
success: false,
error: {
type: 'custom',
message: zodError.errors
? zodError.errors[0].message
: zodError.message,
},
};
}
parseUserInput: (pattern, inputValue) => {
return safeZodParseToFormError(createSchema(pattern['data']), inputValue);
},

// parseUserInput: (pattern, inputObj) => {
// const expectedInput = inputObj as { value: string };
// console.log('TEST parseUserInput', pattern, expectedInput);

// const schema = createSchema(pattern.data);
// console.log('TEST schema', schema);
// const result = schema.parse(expectedInput.value);
// console.log('TEST result', result);

// if (result.success) {
// return { success: true, data: result.data };
// } else {
// return { success: false, error: safeZodParseToFormError(schema, obj) };
// }
// },

parseConfigData: obj => {
const result = safeZodParseFormErrors(configSchema, obj);
return result;
Expand Down

0 comments on commit d61938a

Please sign in to comment.