techStackGuru

Jest Checkbox Testing

Table of Content

  • Test Checkbox Checked
  • Test Checkbox Un-checked

Here we will see how to test Checkbox using Jest.


Test Checkbox Checked

App.js file


function App() {

    return (
      <div className="App">
        <input type="checkbox" checked={true}/>
      </div>
    );
  }
  
export default App;

test('Check Initial Conditions', () => {
    render(<App />)
  
    const checkbox = screen.getByRole('checkbox')
    expect(checkbox).toBeChecked()
  })

Running test


npm test
jest-test

Test Checkbox Un-checked


function App() {

    return (
      <div className="App">
        <input type="checkbox"/>
      </div>
    );
  }
  
  export default App;

test('Check Initial Conditions', () => {
    render(<App />)
  
    const checkbox = screen.getByRole('checkbox')
    expect(checkbox).not.toBeChecked()
  })

To Run test we use below command


npm test
jest-test

previous-button
next-button