2019-09-04 15:48:58 +03:00
|
|
|
/* global jest, expect */
|
2018-04-13 15:33:03 +01:00
|
|
|
|
|
|
|
import React from 'react';
|
2019-09-04 15:48:58 +03:00
|
|
|
import { render, fireEvent } from '@testing-library/react';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
2019-09-04 15:48:58 +03:00
|
|
|
import Form from './Form';
|
2018-04-13 15:33:03 +01:00
|
|
|
|
|
|
|
const defaultTestProps = {
|
|
|
|
buttonText: 'Submit',
|
2020-09-09 17:12:48 +01:00
|
|
|
formFields: [
|
|
|
|
{ name: 'name', label: 'name Label' },
|
|
|
|
{ name: 'website', label: 'WebSite label' }
|
|
|
|
],
|
2018-04-13 15:33:03 +01:00
|
|
|
id: 'my-test-form',
|
|
|
|
options: {
|
|
|
|
types: {
|
|
|
|
name: 'text',
|
|
|
|
website: 'url'
|
|
|
|
},
|
|
|
|
required: ['website']
|
|
|
|
},
|
|
|
|
submit: () => {}
|
|
|
|
};
|
|
|
|
|
2019-09-04 15:48:58 +03:00
|
|
|
test('should render', () => {
|
|
|
|
const { getByLabelText, getByText } = render(<Form {...defaultTestProps} />);
|
|
|
|
|
2020-09-09 17:12:48 +01:00
|
|
|
const nameInput = getByLabelText(/name Label/);
|
2019-09-04 15:48:58 +03:00
|
|
|
expect(nameInput).not.toBeRequired();
|
|
|
|
expect(nameInput).toHaveAttribute('type', 'text');
|
|
|
|
|
2020-09-09 17:12:48 +01:00
|
|
|
const websiteInput = getByLabelText(/WebSite label/);
|
2019-09-04 15:48:58 +03:00
|
|
|
expect(websiteInput).toBeRequired();
|
|
|
|
expect(websiteInput).toHaveAttribute('type', 'url');
|
|
|
|
|
|
|
|
const button = getByText(/submit/i);
|
|
|
|
expect(button).toHaveAttribute('type', 'submit');
|
|
|
|
expect(button).toBeDisabled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should render with default values', () => {
|
|
|
|
const websiteValue = 'http://mysite.com';
|
|
|
|
const nameValue = 'John';
|
|
|
|
|
|
|
|
const { getByLabelText, getByText } = render(
|
|
|
|
<Form
|
|
|
|
{...defaultTestProps}
|
|
|
|
enableSubmit={true}
|
|
|
|
initialValues={{ name: nameValue, website: websiteValue }}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2020-09-09 17:12:48 +01:00
|
|
|
const nameInput = getByLabelText(/name Label/);
|
2019-09-04 15:48:58 +03:00
|
|
|
expect(nameInput).toHaveValue(nameValue);
|
|
|
|
|
2020-09-09 17:12:48 +01:00
|
|
|
const websiteInput = getByLabelText(/WebSite label/);
|
2019-09-04 15:48:58 +03:00
|
|
|
expect(websiteInput).toHaveValue(websiteValue);
|
|
|
|
|
|
|
|
const button = getByText(/submit/i);
|
|
|
|
expect(button).toBeEnabled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should submit', () => {
|
|
|
|
const submit = jest.fn();
|
|
|
|
const props = {
|
|
|
|
...defaultTestProps,
|
|
|
|
submit
|
|
|
|
};
|
|
|
|
const websiteValue = 'http://mysite.com';
|
|
|
|
|
|
|
|
const { getByLabelText, getByText } = render(<Form {...props} />);
|
|
|
|
|
2020-09-09 17:12:48 +01:00
|
|
|
const websiteInput = getByLabelText(/WebSite label/);
|
2019-09-04 15:48:58 +03:00
|
|
|
fireEvent.change(websiteInput, { target: { value: websiteValue } });
|
|
|
|
expect(websiteInput).toHaveValue(websiteValue);
|
|
|
|
|
|
|
|
const button = getByText(/submit/i);
|
|
|
|
expect(button).toBeEnabled();
|
|
|
|
|
|
|
|
fireEvent.click(button);
|
|
|
|
expect(submit).toHaveBeenCalledTimes(1);
|
|
|
|
expect(submit.mock.calls[0][0]).toEqual({ website: websiteValue });
|
|
|
|
|
|
|
|
fireEvent.change(websiteInput, { target: { value: `${websiteValue}///` } });
|
|
|
|
expect(websiteInput).toHaveValue(`${websiteValue}///`);
|
|
|
|
|
|
|
|
fireEvent.click(button);
|
|
|
|
expect(submit).toHaveBeenCalledTimes(2);
|
|
|
|
expect(submit.mock.calls[1][0]).toEqual({ website: websiteValue });
|
2018-04-13 15:33:03 +01:00
|
|
|
});
|