-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e82768
commit 3babc0b
Showing
2 changed files
with
79 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<any>) { | ||
const schema = new ZodBridge({ schema: z.object({}) }); | ||
|
||
const onSubmit = jest.fn(); | ||
afterEach(() => { | ||
onSubmit.mockClear(); | ||
}); | ||
|
||
test('<BaseForm> - renders', () => { | ||
const screen = render(<BaseForm data-testid="form" schema={schema} />); | ||
expect(screen.getByTestId('form')).toBeInTheDocument(); | ||
}); | ||
|
||
test('<BaseForm> - when rendered, is <form>', () => { | ||
const { container } = render(<BaseForm schema={schema} />); | ||
expect(container.getElementsByTagName('form')).toHaveLength(1); | ||
}); | ||
|
||
test('<BaseForm> - when rendered, have correct children', () => { | ||
const { container } = render( | ||
<BaseForm schema={schema}> | ||
<div /> | ||
<div /> | ||
<div /> | ||
</BaseForm>, | ||
); | ||
|
||
expect(container.getElementsByTagName('div')).toHaveLength(3); | ||
}); | ||
|
||
test('<BaseForm> - when submitted, calls `onSubmit` once', () => { | ||
render(<BaseForm schema={schema} onSubmit={onSubmit} data-testid="form" />); | ||
|
||
fireEvent.submit(screen.getByTestId('form')); | ||
expect(onSubmit).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('<BaseForm> - when submitted, calls `onSubmit` with correct model', () => { | ||
render( | ||
<BaseForm | ||
model={model} | ||
schema={schema} | ||
onSubmit={onSubmit} | ||
data-testid="form" | ||
/>, | ||
); | ||
|
||
fireEvent.submit(screen.getByTestId('form')); | ||
expect(onSubmit).toHaveBeenLastCalledWith(model); | ||
}); | ||
|
||
test('<BaseForm> - when submitted, calls `onSubmit` with the correctly `modelTransform`ed model', () => { | ||
render( | ||
<BaseForm | ||
model={model} | ||
schema={schema} | ||
onSubmit={onSubmit} | ||
data-testid="form" | ||
modelTransform={(mode, model) => | ||
mode === 'submit' ? { submit: 1 } : model | ||
} | ||
/>, | ||
); | ||
|
||
fireEvent.submit(screen.getByTestId('form')); | ||
expect(onSubmit).toHaveBeenLastCalledWith({ submit: 1 }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <form>', () => { | ||
const { container } = render(<BaseForm schema={schema} />); | ||
expect(container.getElementsByTagName('form')).toHaveLength(1); | ||
}); | ||
|
||
it('have correct children', () => { | ||
const { container } = render( | ||
<BaseForm schema={schema}> | ||
<div /> | ||
<div /> | ||
<div /> | ||
</BaseForm>, | ||
); | ||
|
||
expect(container.getElementsByTagName('div')).toHaveLength(3); | ||
}); | ||
}); | ||
|
||
describe('when submitted', () => { | ||
it('calls `onSubmit` once', () => { | ||
render( | ||
<BaseForm schema={schema} onSubmit={onSubmit} data-testid="form" />, | ||
); | ||
|
||
fireEvent.submit(screen.getByTestId('form')); | ||
expect(onSubmit).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls `onSubmit` with correct model', () => { | ||
render( | ||
<BaseForm | ||
model={model} | ||
schema={schema} | ||
onSubmit={onSubmit} | ||
data-testid="form" | ||
/>, | ||
); | ||
|
||
fireEvent.submit(screen.getByTestId('form')); | ||
expect(onSubmit).toHaveBeenLastCalledWith(model); | ||
}); | ||
|
||
it('calls `onSubmit` with the correctly `modelTransform`ed model', () => { | ||
render( | ||
<BaseForm | ||
model={model} | ||
schema={schema} | ||
onSubmit={onSubmit} | ||
data-testid="form" | ||
modelTransform={(mode, model) => | ||
mode === 'submit' ? { submit: 1 } : model | ||
} | ||
/>, | ||
); | ||
|
||
fireEvent.submit(screen.getByTestId('form')); | ||
expect(onSubmit).toHaveBeenLastCalledWith({ submit: 1 }); | ||
}); | ||
}); | ||
}); |