[英]Check that button is disabled in react-testing-library
我有一个生成按钮的 React 组件,其中的内容包含<span>
元素,如下所示:
function Click(props) {
return (
<button disable={props.disable}>
<span>Click me</span>
</button>
);
}
我想使用react-testing-library
和mocha
+ chai
来测试这个组件的逻辑。
我目前getByText("Click me")
的问题是getByText("Click me")
选择器返回<span>
DOM 节点,但对于测试,我需要检查<button>
节点的disable
属性。 处理此类测试用例的最佳实践是什么? 我看到了几个解决方案,但所有这些听起来都有点不对劲:
<button>
元素使用data-test-id
<Click />
组件的祖先之一,然后选择within(...)
此范围within(...)
的按钮fireEvent
的选定元素并检查是否没有发生任何事情你能提出更好的方法吗?
断言按钮是否被禁用
您可以使用toHaveAttribute
和closest
来测试它。
import { render } from '@testing-library/react';
const { getByText } = render(Click);
expect(getByText(/Click me/i).closest('button')).toHaveAttribute('disabled');
或toBeDisabled
expect(getByText(/Click me/i).closest('button')).toBeDisabled();
断言按钮是否启用
要检查按钮是否启用,请使用not
如下
expect(getByText(/Click me/i).closest('button')).not.toBeDisabled();
您可以使用@testing-library/jest-dom
toBeDisabled()
,它是一个自定义的笑话匹配器来测试 DOM 的状态:
https://github.com/testing-library/jest-dom
例子:
<button>Submit</button>
expect(getByText(/submit/i)).toBeDisabled()
对于寻找测试的人来说,按钮没有被禁用。
import { render } from '@testing-library/react';
const { getByText } = render(Click);
expect(getByText(/Click me/i).getAttribute("disabled")).toBe(null)
您可以使用@testing-library/react
按钮的禁用属性,如下所示。
例子:
import { render } from '@testing-library/react';
const {getByText} = render(<Click/>)
expect(getByText('Click me').closest('button').disabled).toBeTruthy()
我会礼貌地争辩说您正在测试一个实现细节,而 react-testing-library 不鼓励这样做。
您的测试与您的软件使用方式越相似,它们就可以给您越多的信心。
如果按钮被禁用,用户不会看到禁用的道具,而是看不到任何事情发生。 如果按钮被启用,用户不会看到被禁用的道具的遗漏,而是会看到一些事情发生。
我相信您应该对此进行测试:
const Button = (props) => (
<button
type="submit"
onClick={props.onClick}
disabled={props.disabled}
>
Click me
</button>
);
describe('Button', () => {
it('will call onClick when enabled', () => {
const onClick = jest.fn();
render(<Button onClick={onClick} disabled={false} />);
userEvent.click(getByRole('button', /click me/i));
expect(onClick).toHaveBeenCalledTimes(1);
});
it('will not call onClick when disabled', () => {
const onClick = jest.fn();
render(<Button onClick={onClick} disabled={true} />);
userEvent.click(getByRole('button', /click me/i));
expect(onClick).not.toHaveBeenCalled();
});
})
解决此问题的另一种方法是按角色抓取并检查innerHTML,例如,
const { getByRole } = render(<Click />)
const button = getByRole('button')
// will make sure the 'Click me' text is in there somewhere
expect(button.innerHTML).toMatch(/Click me/))
这不是针对您的特定情况的最佳解决方案,但如果您必须处理不是实际按钮的按钮组件,则可以将其放在后袋中,例如,
<div role="button"><span>Click Me</span></div>
toHaveAttribute是使用属性的好选择。
<button data-testid="ok-button" type="submit" disabled>ok</button>
const button = getByTestId('ok-button')
//const button = getByRole('button');
expect(button).toHaveAttribute('disabled')
expect(button).toHaveAttribute('type', 'submit')
expect(button).not.toHaveAttribute('type', 'button')
expect(button).toHaveAttribute('type', expect.stringContaining('sub'))
expect(button).toHaveAttribute('type', expect.not.stringContaining('but'))
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.