测试库 React - 期望多个元素通过指定条件
Testing library React - expect multiple elements to pass a specified condition
我的组件中有多个按钮,所有按钮都应该被禁用。
const buttons = screen.getAllByRole('button');
expect(ALL BUTTONS).toHaveAttribute('disabled'); // I want something like this.
怎样写才最方便?
迭代按钮数组和运行每个按钮的期望值
buttons.forEach((button) => {
expect(button).toHaveAttribute('disabled');
})
我在测试中使用了这段代码:
buttons.forEach((button) =>{
expect(button).toBeDisabled()
})
或者你可以这样写:
expect(buttons).toBeDisabled().toHaveLength(2)//here you put number of your buttons
我的组件中有多个按钮,所有按钮都应该被禁用。
const buttons = screen.getAllByRole('button');
expect(ALL BUTTONS).toHaveAttribute('disabled'); // I want something like this.
怎样写才最方便?
迭代按钮数组和运行每个按钮的期望值
buttons.forEach((button) => {
expect(button).toHaveAttribute('disabled');
})
我在测试中使用了这段代码:
buttons.forEach((button) =>{
expect(button).toBeDisabled()
})
或者你可以这样写:
expect(buttons).toBeDisabled().toHaveLength(2)//here you put number of your buttons