Skip to content

Commit

Permalink
test: update tests tckt-363
Browse files Browse the repository at this point in the history
  • Loading branch information
kalasgarov committed Nov 13, 2024
1 parent a89b9df commit 04d452a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 40 deletions.
10 changes: 4 additions & 6 deletions packages/forms/src/error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
type FormErrorType = 'required' | 'custom';

export type FormError =
| {
type?: FormErrorType;
message?: string;
}
| undefined;
export type FormError = {
type: FormErrorType;
message?: string;
};

export type FormErrors = Record<string, FormError>;
8 changes: 4 additions & 4 deletions packages/forms/src/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const aggregateValuesByPrefix = (
return aggregatedValues;
};

export const validatePatternAndChildren = (
export const aggregatePatternSessionValues = (
config: FormConfig,
form: Blueprint,
patternConfig: PatternConfig,
Expand All @@ -155,7 +155,7 @@ export const validatePatternAndChildren = (
result: {
values: Record<PatternId, PatternValue>;
errors: Record<PatternId, FormError>;
} = { values: {}, errors: {} }
}
) => {
const aggregatedValues = aggregateValuesByPrefix(values);

Expand All @@ -165,15 +165,15 @@ export const validatePatternAndChildren = (

if (parseResult.success) {
result.values[pattern.id] = parseResult.data;
result.errors[pattern.id] = undefined;
delete result.errors[pattern.id];
} else {
result.values[pattern.id] = values[pattern.id];
result.errors[pattern.id] = parseResult.error;
}
}
for (const child of patternConfig.getChildren(pattern, form.patterns)) {
const childPatternConfig = getPatternConfig(config, child.type);
validatePatternAndChildren(
aggregatePatternSessionValues(
config,
form,
childPatternConfig,
Expand Down
6 changes: 2 additions & 4 deletions packages/forms/src/patterns/page-set/submit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ describe('Page-set submission', () => {
session: {
...session,
data: {
errors: {
'input-1': undefined,
},
errors: {},
values: {
'input-1': 'test',
},
},
route: {
url: '#',
params: {
page: '0',
page: '1',
},
},
form: session.form,
Expand Down
24 changes: 9 additions & 15 deletions packages/forms/src/patterns/page-set/submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { failure, success } from '@atj/common';
import {
getPatternConfig,
getPatternSafely,
validatePatternAndChildren,
} from '../../pattern';
aggregatePatternSessionValues,
} from '../../pattern.js';
import { type FormSession } from '../../session';
import { type SubmitHandler } from '../../submission';
import { type PagePattern } from '../page/config';
Expand Down Expand Up @@ -35,12 +35,16 @@ export const submitPage: SubmitHandler<PageSetPattern> = async (
return failure(pagePattern.error);
}

const result = validatePatternAndChildren(
const result = aggregatePatternSessionValues(
config,
opts.session.form,
pagePatternConfig,
pagePattern.data,
opts.data
opts.data,
{
values: { ...opts.session.data.values },
errors: { ...opts.session.data.errors },
}
);

// Increment the page number if there are no errors and this isn't the last page.
Expand All @@ -53,17 +57,7 @@ export const submitPage: SubmitHandler<PageSetPattern> = async (
return success({
session: {
...opts.session,
data: {
...opts.session.data,
values: {
...opts.session.data.values,
...result.values,
},
errors: {
...opts.session.data.errors,
...result.errors,
},
},
data: result,
route: opts.session.route
? {
...opts.session.route,
Expand Down
22 changes: 20 additions & 2 deletions packages/forms/src/patterns/phone-number/phone-number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('PhoneNumberPattern tests', () => {
const invalidInput = '123456abc';

expect(schema.safeParse(validInput).success).toBe(true);
expect(schema.safeParse(invalidInput).success).toBe(false);
const invalidResult = schema.safeParse(invalidInput);
expect(invalidResult.success).toBe(false);
expect(invalidResult.error?.issues[0].message).toBe(
'Phone number may only contain digits, spaces, parentheses, hyphens, and periods.'
);
});

it('should create schema for optional phone input', () => {
Expand All @@ -30,9 +34,16 @@ describe('PhoneNumberPattern tests', () => {
const schema = createPhoneSchema(data);
const validInput = '+12223334444';
const emptyInput = '';
const invalidInput = '123456abc';

expect(schema.safeParse(validInput).success).toBe(true);
expect(schema.safeParse(emptyInput).success).toBe(true);

const invalidResult = schema.safeParse(invalidInput);
expect(invalidResult.success).toBe(false);
expect(invalidResult.error?.issues[0].message).toBe(
'Phone number may only contain digits, spaces, parentheses, hyphens, and periods.'
);
});

it('should fail with less than 10 digits', () => {
Expand All @@ -44,7 +55,11 @@ describe('PhoneNumberPattern tests', () => {
const schema = createPhoneSchema(data);
const shortInput = '123456789';

expect(schema.safeParse(shortInput).success).toBe(false);
const shortInputResult = schema.safeParse(shortInput);
expect(shortInputResult.success).toBe(false);
expect(shortInputResult.error?.issues[0].message).toBe(
'Phone number must contain at least 10 digits'
);
});
});

Expand Down Expand Up @@ -88,6 +103,9 @@ describe('PhoneNumberPattern tests', () => {
const result = phoneNumberConfig.parseUserInput(pattern, invalidInput);
if (!result.success) {
expect(result.error).toBeDefined();
expect(result.error?.message).toContain(
'Phone number may only contain digits, spaces, parentheses, hyphens, and periods.'
);
} else {
expect.fail('Unexpected validation success');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('SelectDropdownPattern tests', () => {
if (result.success) {
expect(result.data).toBe('value1');
} else {
throw new Error('Unexpected validation failure');
expect.fail('Unexpected validation failure');
}
});

Expand All @@ -105,11 +105,11 @@ describe('SelectDropdownPattern tests', () => {
console.log('Test parse result (error case):', result);
if (!result.success) {
expect(result.error).toBeDefined();
expect(result.error.message).toBe(
expect(result.error?.message).toBe(
"Invalid enum value. Expected 'value1' | 'value2', received 'invalid'"
);
} else {
throw new Error('Unexpected validation success');
expect.fail('Unexpected validation success');
}
});

Expand All @@ -132,7 +132,7 @@ describe('SelectDropdownPattern tests', () => {
expect(result.data.required).toBe(true);
expect(result.data.options.length).toBe(2);
} else {
throw new Error('Unexpected validation failure');
expect.fail('Unexpected validation failure');
}
});

Expand All @@ -150,7 +150,7 @@ describe('SelectDropdownPattern tests', () => {
if (!result.success) {
expect(result.error).toBeDefined();
} else {
throw new Error('Unexpected validation success');
expect.fail('Unexpected validation success');
}
});
});
Expand Down
6 changes: 2 additions & 4 deletions packages/forms/src/services/submit-form.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,14 @@ describe('multi-page form', () => {
...session,
data: {
attachments: undefined,
errors: {
'element-1': undefined,
},
errors: {},
values: {
'element-1': 'test',
},
},
route: {
params: {
page: '0',
page: '1',
},
url: '#',
},
Expand Down

0 comments on commit 04d452a

Please sign in to comment.