Home > Enterprise >  Testing library React - expect multiple elements to pass specified a condition
Testing library React - expect multiple elements to pass specified a condition

Time:10-23

I have multiple buttons in my component and all of them should be disabled.

const buttons = screen.getAllByRole('button');

expect(ALL BUTTONS).toHaveAttribute('disabled'); // I want something like this.

How can we write this in the most convenient way?

CodePudding user response:

Iterate the buttons array and run the expect for each one

buttons.forEach((button) => {
   expect(button).toHaveAttribute('disabled');
})
  • Related