最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Testing library React - expect multiple elements to pass a specified condition - Stack Overflow

programmeradmin4浏览0评论

I have multiple buttons in my ponent 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?

I have multiple buttons in my ponent 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?

Share Improve this question edited Sep 19, 2023 at 20:16 Deotyma 9483 gold badges14 silver badges30 bronze badges asked Oct 23, 2021 at 3:26 dannyxndadannyxnda 1,0141 gold badge11 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Iterate the buttons array and run the expect for each one

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

I used this code in my test:

buttons.forEach((button) =>{
    expect(button).toBeDisabled()
})

or you can write this:

 expect(buttons).toBeDisabled().toHaveLength(2)//here you put number of your buttons
发布评论

评论列表(0)

  1. 暂无评论