Introduction
The Testing Library is the most convenient and lightweight testing solution for web pages. This library provides assurance to the developer that the application will work as expected in a real-case scenario.
Testing Conditional Rendering includes testing the Appearance and Disappearance of an element. In simple words, it checks whether an element is present and then disappears and vice-versa.
Waiting For Appearance
We can use the async wait utilities in the case when we have to wait for an element to appear in our application. The async wait utilities allow the system to wait for an assertion before proceeding forward. The async wait utility will keep retrying until the condition is satisfied or time runs out.
We can use two methods to wait for appearances,
- Using findBy Query
- Using waitFor Query
Using findBy Query
test('Appearance Example', async () => {
// element is initially not present in the application
// function will wait for the appearance and return the element
const appearance = await findByText('New Appear')
})
Using waitFor Query
test('Appearance Example', async () => {
// element is initially not present in the application
// function will wait for the appearance and return the element
await waitFor(() => {
expect(getByText('New Appear')).toBeInTheDocument()
})
})