skip to Main Content

I’m trying to test a React component that conditionally renders null using Cypress component testing. Here’s a simplified version of my component:

const MyComponent = () => {
  if (someCondition) {
    return null;
  }

  return (
    <div>
      {/* UI content */}
    </div>
  );
};

I want to write a test that checks if the component renders nothing when the condition is true. Here’s what I have so far:

it("should render nothing if condition is true", () => {
  cy.pageMount(<MyComponent />);
  
  // How can I assert that the component rendered null?
});

I’m not sure how to assert that the component actually rendered null. Is there a way to check for the absence of the component in the DOM using Cypress?

I’ve tried looking for documentation on testing null renders with Cypress, but haven’t found a clear solution. Any help or guidance would be greatly appreciated!

Note: I’m using Cypress component testing, not end-to-end testing.

2

Answers


  1. Well I think I understood the problem. To test if your React component is returning null with Cypress, you can simply check that the DOM doesn’t contain any content when the component renders nothing. Since you’re conditionally returning null, no elements should be present in the DOM.

    Here’s a friendly suggestion for how to handle this:

    Check for the absence of elements: You can assert that no elements from your component exist in the DOM.

    Use .should(‘not.exist’): This is a great way to verify that nothing is rendered.

    it("should render nothing if condition is true", () => {
      cy.pageMount(<MyComponent />);
      
      cy.get('div').should('not.exist');
    });

    lets see what it is doing:

    cy.pageMount() mounts your component in the test.
    cy.get(‘div’).should(‘not.exist’) checks if no div element (or any root element you specify) is present.

    If you’re unsure what the root element is, you could also check that the body contains no child elements like this:

    cy.get('body').children().should('have.length', 0);

    This way, you’re ensuring the component rendered absolutely nothing when someCondition is true. I hope that helps!

    Login or Signup to reply.
  2. In the absence of something to identify the component, you can check the inner HTML of the element <div data-cy-root> which Cypress adds to the DOM as a mounting point.

    Obviously, the component is (almost?) never actually totally devoid of distinguishing features, you could use any of the text or attribute selection methods Cypress provides, then simply assert it does not exist.

    But let’s say it’s a code-only component such as a data-context provider, purely there to pass state to React, so there is nothing to select it by except it’s top-level <div> tag.

    For example, if I set up the provided example component so that it doesn’t mount

    import React from 'react'
    
    const someCondition = true
    
    export const MyComponent = () => {
      if (someCondition) {
        return null;
      }
    
      return (
        <div>
          I'm mounting now
        </div>
      );
    }
    

    I can test it with

    import React from 'react'
    import {MyComponent} from './MyComponent.js'
    
    console.clear()
    
    it("should render nothing if condition is true", () => {
      cy.mount(<MyComponent data-test-id='my-component' />)
    
      cy.get('div[data-cy-root]')
        .find('div')
        .should('not.exist')
    })
    

    and the test passes.

    enter image description here

    To make sure I’m not spoofing myself, I flip someCondition to false

    enter image description here

    and the test now fails.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search