I've recently inherited a codebase with a requirement to use RTL (currently using Enzyme). A lot of the Enzyme tests are fairly simple like so and pass with no issues:
const render = ({ firstName, surname } = {}) => {
const props = { firstName, surname };
return shallow(<MyComponent {...props} />);
};
test('renders firstName and surname', () => {
expect(
render({
firstName: 'My first name',
surname: 'My surname',
})
).toMatchSnapshot();
});
I'm having issues however migrating this test to React Testing Library - I have the below as a very rough starting point, but know that it's not quite right. Can anyone suggest helpful docs for basics like this or suggest any code improvements? TIA
test('renders firstName and surname', () => {
props = { firstName, surname };
render(<MyComponent {...props} />);
expect(screen.getByText(props.firstName)).toBe('My first name');
expect(screen.getByText(props.surname)).toBe('My surname');
});
renderline just doexpect(screen.getByText(firstName)).toBeInTheDocument()(same for surname) and remove theconstdeclarations