Skip to content

Commit

Permalink
moved test from suites to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpospiech committed Feb 16, 2024
1 parent 0e82768 commit 3babc0b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 65 deletions.
68 changes: 3 additions & 65 deletions packages/uniforms/__suites__/BaseForm.tsx
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 });
});
}
76 changes: 76 additions & 0 deletions packages/uniforms/__tests__/BaseForm.tsx
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 });
});
});
});

0 comments on commit 3babc0b

Please sign in to comment.