Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Waiting For Appearance
2.1.
Using findBy Query
2.2.
Using waitFor Query
3.
Waiting For Disappearance
3.1.
waitForElementToBeRemoved
3.2.
waitFor
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Testing Conditional Rendering

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()
  })
})

Waiting For Disappearance

To achieve waiting for disappearance, we can use the following two functions,

  • waitForElementToBeRemoved
  • waitFor

waitForElementToBeRemoved

test('Disappearance Example', async () => {
  // element is removed
  await waitForElementToBeRemoved(() => queryByText('Item to Disappear'))
})

waitFor

test('Appearance Example', async () => {
  // element is initially present in the application
  await waitFor(() => {
    expect(getByText('Item to Disappear')).not.toBeInTheDocument()
  })
})

FAQs

  1. What are Testing Libraries?
    Testing Libraries are a collection of libraries for testing applications. These libraries provide assurance to the developer that the application will work as expected in a real-case scenario.
  2. What is Jest?
    Jest is a framework in JavaScript that is used for the testing purposes of large web applications. It is maintained by Facebook. Since Jest doesn’t require a lot of configuration, it is an excellent option to be used as a testing framework among new developers.
  3. What is the disadvantage of using Jest?
    Although Jest is a compelling library, it is comparatively prolonged compared to other testing libraries.
  4. What is DOM?
    DOM stands for Document Object Model. It is a cross-platform and language-independent interface for treating an XML or HTML document.
  5. What is Dynamic Testing?
    A Dynamic Test is one of the most commonly used tests, and it is generated at the run time. We use the TestFactory method to generate Dynamic Tests in JUnit5. TestFactory method is a non-static and non-private method.

Key Takeaways

This Blog covered all the necessary points about the Appearance and Disappearance of elements in Testing. We further looked at different methods to achieve Appearance and Disappearance Testing using the Testing Libraries. 
Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass