Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpospiech committed Apr 22, 2024
1 parent 410034b commit 824e93f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/uniforms-bridge-zod/__tests__/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,23 +131,26 @@ describe('ZodBridge', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
const error = bridge.getValidator()({});
const messages = error?.issues?.map(issue => issue.message);
const messages = ['A: Required', 'B: Required'];
expect(bridge.getErrorMessages(error)).toEqual(messages);
});

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const messages = error?.issues?.map(issue => issue.message);
const messages = [
'A (0, 2): Expected string, received number',
'A (1, 0): Expected string, received number',
];
expect(bridge.getErrorMessages(error)).toEqual(messages);
});

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const messages = error?.issues?.map(issue => issue.message);
const messages = ['C: Expected string, received number'];
expect(bridge.getErrorMessages(error)).toEqual(messages);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bridge-zod/src/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function getLabel(value: unknown) {
return upperFirst(lowerCase(joinName(null, value).slice(-1)[0]));
}

function getFullLabel(path: ZodIssue['path'], indexes: number[]): string {
function getFullLabel(path: ZodIssue['path'], indexes: number[] = []): string {
const lastElement = path[path.length - 1];

if (typeof lastElement === 'number') {
Expand Down Expand Up @@ -92,7 +92,7 @@ export default class ZodBridge<T extends ZodRawShape> extends Bridge {
getErrorMessages(error: unknown) {
if (error instanceof ZodError) {
return error.issues.map(issue => {
const name = getFullLabel(issue.path, []);
const name = getFullLabel(issue.path);
return `${name}: ${issue.message}`;
});
}
Expand Down
4 changes: 3 additions & 1 deletion packages/uniforms/__suites__/ErrorsField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export function testErrorsField(ErrorsField: ComponentType<any>) {
]),
schema: z.object({ x: z.boolean(), y: z.number(), z: z.string() }),
});
expect(screen.getAllByText(errorMessage)).toHaveLength(3);
expect(screen.getByText(`X: ${errorMessage}`)).toBeInTheDocument();
expect(screen.getByText(`Y: ${errorMessage}`)).toBeInTheDocument();
expect(screen.getByText(`Z: ${errorMessage}`)).toBeInTheDocument();
});

test('<ErrorsField> - renders error from props', () => {
Expand Down

0 comments on commit 824e93f

Please sign in to comment.