Jed Dayo
The enzyme is a javascript testing utility for React that makes it easier to assert, manipulate and traverse the components. It was created by Airbnb and later transferred to an independent Organization. It was developed around December 2015. It provides manageable methods to find elements, render components and interact with the elements.
describe('Login Test Suite', () => {
it('should render the form', () => {
const wrapper = shallow(<Login />);
expect(wrapper.find('form.login').exists()).toBe(true);
expect(wrapper.find('#email').length).toEqual(1);
expect(wrapper.find('#password').length).toEqual(1);
})
})
describe("The events are working", () => {
it("The form is submitted when the click event is fired", () => {
const mockCallBack = jest.fn();
const wrapper = shallow(<Form onSubmit={mockCallBack()} />);
wrapper.find("#submit-button").simulate("click");
expect(mockCallBack).toHaveBeenCalledTimes(1);
});
});
describe("Snapshot", () => {
it("matches App the snapshot", () => {
const wrapper = mount(<App />);
expect(toJson(wrapper)).toMatchSnapshot();
});
});
React Testing Library
Enzyme
https://redux.js.org/usage/writing-tests
Used to find and get elements in the DOM
https://testing-library.com/docs/queries/about
This can be used to query every element that is exposed in the accessibility tree
Example: getByRole('button', {name: /submit/i})
This method is really good for form fields. When navigating through a website form, users find elements using label text. This method emulates that behaviour, so it should be your top preference
Example: getByLabelText('username')
A placeholder is not a substitute for a label. But if that's all you have, then it's better than alternatives.
Example: getByPlaceholderText('enter username here')
Outside of forms, text content is the main way users find elements. This method can be used to find non-interactive elements (like divs, spans, and paragraphs)
Example:
getByText('Contact Us'),
getByText(/Contact Us/i)
The current value of a form element can be useful when navigating a page with filled-in values
Example:
getByDisplayValue('100%')
HTML5 and ARIA compliant selectors. Note that the user experience of interacting with these attributes varies greatly across browsers and assistive technology.
If your element is one which supports alt
text (img
, area
, input
, and any custom element), then you can use this to find that element
Example:
getByAltText('this is an image')
The title attribute is not consistently read by screenreaders, and is not visible by default for sighted users
Example:
getByTitle('I get shown on hover')
The user cannot see (or hear) these, so this is only recommended for cases where you can't match by role or text or it doesn't make sense (e.g. the text is dynamic)
Example:
getByTestId('test-submit-button-id')
Convenience methods for firing DOM events
see for full list -> https://github.com/testing-library/dom-testing-library/blob/main/src/event-map.js
Commonly used
describe('payment form validation',()=>{
it('should show minimum amount error',()=>{
render(<PaymentForm/>);
const amountInput = screen.getByLabelText('Amount');
fireEvent.change(amountInput, {target: {value: 0}});
fireEvent.blur(amountInput);
expect(screen.getByText(/amount should be greater than 0/i));
})
})
user-event
is a companion library for Testing Library that simulates user interactions by dispatching the events that would happen if the interaction took place in a browser.
describe('payment form validation',()=>{
it('should show minimum amount error',()=>{
render(<PaymentForm/>);
const amountInput = screen.getByLabelText('Amount');
userEvent.type(amountInput, '0{enter}');
expect(screen.getByText(/amount should be greater than 0/i));
})
})
https://kentcdodds.com/blog/common-mistakes-with-react-testing-library
https://kentcdodds.com/blog/write-fewer-longer-tests
https://testingjavascript.com