diff --git a/packages/uniforms/__suites__/BaseForm.tsx b/packages/uniforms/__suites__/BaseForm.tsx index b6baee8bf..89c887748 100644 --- a/packages/uniforms/__suites__/BaseForm.tsx +++ b/packages/uniforms/__suites__/BaseForm.tsx @@ -1,75 +1,13 @@ -import { fireEvent, render, screen } from '@testing-library/react'; -import React from 'react'; -import { BaseForm as UniformsBaseForm } from 'uniforms'; +import { render } from '@testing-library/react'; +import React, { ComponentType } from 'react'; import { ZodBridge } from 'uniforms-bridge-zod'; import z from 'zod'; -export function testBaseForm(BaseForm: typeof UniformsBaseForm) { - const model = { $: [1], _: 1 }; +export function testBaseForm(BaseForm: ComponentType) { const schema = new ZodBridge({ schema: z.object({}) }); - const onSubmit = jest.fn(); - afterEach(() => { - onSubmit.mockClear(); - }); - test(' - renders', () => { - const screen = render(); - expect(screen.getByTestId('form')).toBeInTheDocument(); - }); - - test(' - when rendered, is
', () => { const { container } = render(); expect(container.getElementsByTagName('form')).toHaveLength(1); }); - - test(' - when rendered, have correct children', () => { - const { container } = render( - -
-
-
- , - ); - - expect(container.getElementsByTagName('div')).toHaveLength(3); - }); - - test(' - when submitted, calls `onSubmit` once', () => { - render(); - - fireEvent.submit(screen.getByTestId('form')); - expect(onSubmit).toHaveBeenCalledTimes(1); - }); - - test(' - when submitted, calls `onSubmit` with correct model', () => { - render( - , - ); - - fireEvent.submit(screen.getByTestId('form')); - expect(onSubmit).toHaveBeenLastCalledWith(model); - }); - - test(' - when submitted, calls `onSubmit` with the correctly `modelTransform`ed model', () => { - render( - - mode === 'submit' ? { submit: 1 } : model - } - />, - ); - - fireEvent.submit(screen.getByTestId('form')); - expect(onSubmit).toHaveBeenLastCalledWith({ submit: 1 }); - }); } diff --git a/packages/uniforms/__tests__/BaseForm.tsx b/packages/uniforms/__tests__/BaseForm.tsx new file mode 100644 index 000000000..9e2212e87 --- /dev/null +++ b/packages/uniforms/__tests__/BaseForm.tsx @@ -0,0 +1,76 @@ +import { fireEvent, render, screen } from '@testing-library/react'; +import React from 'react'; +import { BaseForm } from 'uniforms'; +import { ZodBridge } from 'uniforms-bridge-zod'; +import z from 'zod'; + +describe('BaseForm', () => { + const model = { $: [1], _: 1 }; + const schema = new ZodBridge({ schema: z.object({}) }); + + const onSubmit = jest.fn(); + afterEach(() => { + onSubmit.mockClear(); + }); + + describe('when rendered', () => { + it('is ', () => { + const { container } = render(); + expect(container.getElementsByTagName('form')).toHaveLength(1); + }); + + it('have correct children', () => { + const { container } = render( + +
+
+
+ , + ); + + expect(container.getElementsByTagName('div')).toHaveLength(3); + }); + }); + + describe('when submitted', () => { + it('calls `onSubmit` once', () => { + render( + , + ); + + fireEvent.submit(screen.getByTestId('form')); + expect(onSubmit).toHaveBeenCalledTimes(1); + }); + + it('calls `onSubmit` with correct model', () => { + render( + , + ); + + fireEvent.submit(screen.getByTestId('form')); + expect(onSubmit).toHaveBeenLastCalledWith(model); + }); + + it('calls `onSubmit` with the correctly `modelTransform`ed model', () => { + render( + + mode === 'submit' ? { submit: 1 } : model + } + />, + ); + + fireEvent.submit(screen.getByTestId('form')); + expect(onSubmit).toHaveBeenLastCalledWith({ submit: 1 }); + }); + }); +});