The Complete React Testing Course 1
Testing Components
Summary
• When testing React components, there are two major concerns we should test:
rendering and responding to user actions.
• Aim to write tests that are maintainable, robust, and trustworthy.
• To ensure your tests are robust, test the behavior (what the application does), not the
implementation (how it is done). This approach ensures that tests remain valid even
when the implementation changes, as long as the behavior stays consistent.
• Don't test styles! Visual tests can be fragile. A tiny style change can fail a test. Plus,
passing visual tests don't guarantee that our application looks good.
• Setup les are loaded before each test le. They are useful for setting up the testing
environment.
• user-event is a companion library to React Testing Library that simulates user
interactions.
Copyright 2024 Code with Mosh [Link]
fi
fi
The Complete React Testing Course 2
Useful Functions
Types of Queries
getBy
queryBy
findBy
Query Variations
ByRole
ByText
ByPlaceholderText
ByTestId
Useful Matchers
toBeChecked()
toBeDisabled()
toBeEmptyDOMElement()
toBeInTheDocument()
toHaveAttribute()
toHaveTextContent()
Simulating User Interactions
const user = [Link]()
await [Link](element)
await [Link](element, ‘a’)
Copyright 2024 Code with Mosh [Link]