2019-01-03 16:00:55 +03:00
|
|
|
import React from 'react';
|
2021-10-22 07:18:03 +03:00
|
|
|
import { render, screen } from '@testing-library/react';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2019-01-03 16:00:55 +03:00
|
|
|
import { Form } from '../src';
|
2020-09-02 20:30:47 +03:00
|
|
|
import { schema, uiSchema, formData } from '../src/Form/schema';
|
2019-01-03 16:00:55 +03:00
|
|
|
|
2020-08-08 23:26:39 +03:00
|
|
|
describe('Form', function () {
|
2021-10-22 07:18:03 +03:00
|
|
|
let random: () => number;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
random = Math.random;
|
|
|
|
Math.random = jest.fn(() => 0.25546350798039463);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
Math.random = random;
|
|
|
|
console.log(Math.random());
|
|
|
|
});
|
|
|
|
|
2019-01-03 16:00:55 +03:00
|
|
|
it('renders correctly', () => {
|
2021-10-22 07:18:03 +03:00
|
|
|
const { container } = render(
|
2023-07-12 21:03:20 +03:00
|
|
|
<Form formData={formData} schema={schema} uiSchema={uiSchema} />,
|
2019-01-10 21:51:14 +03:00
|
|
|
);
|
2021-10-22 07:18:03 +03:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2019-01-03 16:00:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders with primary button', () => {
|
2021-10-22 07:18:03 +03:00
|
|
|
const { container } = render(
|
2019-01-03 16:00:55 +03:00
|
|
|
<Form
|
|
|
|
primaryButton
|
|
|
|
submitText="Custom button"
|
|
|
|
formData={formData}
|
|
|
|
schema={schema}
|
|
|
|
uiSchema={uiSchema}
|
2023-07-12 21:03:20 +03:00
|
|
|
/>,
|
2019-01-10 21:51:14 +03:00
|
|
|
);
|
2021-10-22 07:18:03 +03:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2019-01-03 16:00:55 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders with no button', () => {
|
2021-10-22 07:18:03 +03:00
|
|
|
const { container } = render(
|
2023-07-12 21:03:20 +03:00
|
|
|
<Form formData={formData} schema={schema} uiSchema={uiSchema} noSubmit />,
|
2019-01-10 21:51:14 +03:00
|
|
|
);
|
2021-10-22 07:18:03 +03:00
|
|
|
expect(container.firstChild).toMatchSnapshot();
|
2019-01-03 16:00:55 +03:00
|
|
|
});
|
|
|
|
|
2022-06-06 19:22:05 +03:00
|
|
|
it('should handle the submit event', async () => {
|
2019-01-03 16:00:55 +03:00
|
|
|
const onSubmit = jest.fn();
|
2021-10-22 07:18:03 +03:00
|
|
|
render(
|
2019-01-03 16:00:55 +03:00
|
|
|
<Form
|
|
|
|
formData={formData}
|
|
|
|
schema={schema}
|
|
|
|
uiSchema={uiSchema}
|
|
|
|
onSubmit={onSubmit}
|
2023-07-12 21:03:20 +03:00
|
|
|
/>,
|
2019-01-03 16:00:55 +03:00
|
|
|
);
|
|
|
|
|
2022-06-06 19:22:05 +03:00
|
|
|
await userEvent.click(screen.getByRole('button', { name: 'Submit' }));
|
2021-10-22 07:18:03 +03:00
|
|
|
expect(onSubmit).toHaveBeenCalled();
|
2019-01-03 16:00:55 +03:00
|
|
|
});
|
|
|
|
});
|