From 824e93f82432e60c18f150cf4c8587a706837053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Po=C5=9Bpiech?= Date: Mon, 22 Apr 2024 19:22:22 +0200 Subject: [PATCH] updated tests --- packages/uniforms-bridge-zod/__tests__/ZodBridge.ts | 9 ++++++--- packages/uniforms-bridge-zod/src/ZodBridge.ts | 4 ++-- packages/uniforms/__suites__/ErrorsField.tsx | 4 +++- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts b/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts index 9dd1dc797..2456ee5aa 100644 --- a/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts +++ b/packages/uniforms-bridge-zod/__tests__/ZodBridge.ts @@ -131,7 +131,7 @@ 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); }); @@ -139,7 +139,10 @@ describe('ZodBridge', () => { 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); }); @@ -147,7 +150,7 @@ describe('ZodBridge', () => { 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); }); }); diff --git a/packages/uniforms-bridge-zod/src/ZodBridge.ts b/packages/uniforms-bridge-zod/src/ZodBridge.ts index 6012031cd..762dd74e7 100644 --- a/packages/uniforms-bridge-zod/src/ZodBridge.ts +++ b/packages/uniforms-bridge-zod/src/ZodBridge.ts @@ -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') { @@ -92,7 +92,7 @@ export default class ZodBridge 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}`; }); } diff --git a/packages/uniforms/__suites__/ErrorsField.tsx b/packages/uniforms/__suites__/ErrorsField.tsx index ef333fc4e..5aa31e884 100644 --- a/packages/uniforms/__suites__/ErrorsField.tsx +++ b/packages/uniforms/__suites__/ErrorsField.tsx @@ -15,7 +15,9 @@ export function testErrorsField(ErrorsField: ComponentType) { ]), 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(' - renders error from props', () => {