Convert old tests to use "react-testing-library"

This commit is contained in:
Justin
2022-04-04 11:53:47 -04:00
parent 9a099b3fa7
commit ed47cf5f09
71 changed files with 411 additions and 1515 deletions

View File

@ -1,40 +0,0 @@
import React from 'react';
import { createShallowComponent } from 'soapbox/test_helpers';
import Form from '../form';
describe('<Form />', () => {
it('renders children', () => {
const onSubmitMock = jest.fn();
const component = createShallowComponent(
<Form onSubmit={onSubmitMock}>children</Form>,
);
expect(component.text()).toContain('children');
});
it('handles onSubmit prop', () => {
const onSubmitMock = jest.fn();
const component = createShallowComponent(
<Form onSubmit={onSubmitMock}>children</Form>,
);
component.find('form').at(0).simulate('submit', {
preventDefault: () => {},
});
expect(onSubmitMock).toHaveBeenCalled();
});
it('handles disabled prop', () => {
const onSubmitMock = jest.fn();
const component = createShallowComponent(
<Form onSubmit={onSubmitMock} disabled>
<button type='submit'>Submit</button>
</Form>,
);
component.find('button').at(0).simulate('click');
expect(onSubmitMock).not.toHaveBeenCalled();
});
});

View File

@ -0,0 +1,29 @@
import React from 'react';
import { fireEvent, render, screen } from '../../../../jest/test-helpers';
import Form from '../form';
describe('<Form />', () => {
it('renders children', () => {
const onSubmitMock = jest.fn();
render(
<Form onSubmit={onSubmitMock}>children</Form>,
);
expect(screen.getByTestId('form')).toHaveTextContent('children');
});
it('handles onSubmit prop', () => {
const onSubmitMock = jest.fn();
render(
<Form onSubmit={onSubmitMock}>children</Form>,
);
fireEvent.submit(
screen.getByTestId('form'), {
preventDefault: () => {},
},
);
expect(onSubmitMock).toHaveBeenCalled();
});
});

View File

@ -1,7 +1,6 @@
import * as React from 'react';
interface IForm {
disabled?: boolean,
onSubmit?: (event: React.FormEvent) => void,
}
@ -15,7 +14,7 @@ const Form: React.FC<IForm> = ({ onSubmit, children, ...filteredProps }) => {
}, [onSubmit]);
return (
<form onSubmit={handleSubmit} className='space-y-4' {...filteredProps}>
<form data-testid='form' onSubmit={handleSubmit} className='space-y-4' {...filteredProps}>
{children}
</form>
);