Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
How to test that a react component renders both its states correctly?

Before rendering all the UI elements I check that the relevant state parameters aren't null.

render() {
        if(this.state.one === null 
            || this.state.two === null 
            || this.state.three === null){
            return <div/>
        }
        return(
            // divs for actual UI 
        )
}

Then in my test I check that component matches the snapshot, the initial state has null parameters, so the snapshot will only return the one empty div. So, that works, I can check that the component isn't rendered when it shouldn't be, but how can I add another snapshot, or something else, to see that everything is rendered correctly when the state no longer contains null?

import React from 'react';
import { shallow } from 'enzyme';
import TheUIComponent from './TheUIClass';

let component;

beforeEach(() => {
    component = shallow(<TheUIComponent/>);
});

describe('UIComponent', () => {
    it('should render correctly', () => {
        expect(component).toMatchSnapshot();
        
    });
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!