redux-devtools/packages/redux-devtools-ui/test/Form.test.tsx

62 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-01-03 16:00:55 +03:00
import React from 'react';
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';
import { schema, uiSchema, formData } from '../src/Form/schema';
2019-01-03 16:00:55 +03:00
describe('Form', function () {
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', () => {
const { container } = render(
<Form formData={formData} schema={schema} uiSchema={uiSchema} />,
2019-01-10 21:51:14 +03:00
);
expect(container.firstChild).toMatchSnapshot();
2019-01-03 16:00:55 +03:00
});
it('renders with primary button', () => {
const { container } = render(
2019-01-03 16:00:55 +03:00
<Form
primaryButton
submitText="Custom button"
formData={formData}
schema={schema}
uiSchema={uiSchema}
/>,
2019-01-10 21:51:14 +03:00
);
expect(container.firstChild).toMatchSnapshot();
2019-01-03 16:00:55 +03:00
});
it('renders with no button', () => {
const { container } = render(
<Form formData={formData} schema={schema} uiSchema={uiSchema} noSubmit />,
2019-01-10 21:51:14 +03:00
);
expect(container.firstChild).toMatchSnapshot();
2019-01-03 16:00:55 +03:00
});
it('should handle the submit event', async () => {
2019-01-03 16:00:55 +03:00
const onSubmit = jest.fn();
render(
2019-01-03 16:00:55 +03:00
<Form
formData={formData}
schema={schema}
uiSchema={uiSchema}
onSubmit={onSubmit}
/>,
2019-01-03 16:00:55 +03:00
);
await userEvent.click(screen.getByRole('button', { name: 'Submit' }));
expect(onSubmit).toHaveBeenCalled();
2019-01-03 16:00:55 +03:00
});
});