我正在尝试为复选框组件创建一些测试,但看起来我无法断言input[type="checkbox"]
何时被禁用。
在前两个块中,我正在测试复选框是否可以选中/取消选中元素是否被禁用,这里一切正常。
对于最后两个块,我正在尝试相同的方法来检查/取消选中,但现在我将属性设置disabled
为元素,但看起来库忽略了属性
这是我的测试
import { fireEvent, render, screen } from '@testing-library/vue';
it('should uncheck if is not disabled', async () => {
render({
template: '<input data-test-id="checkbox" type="checkbox" checked />',
});
const el = screen.getByTestId('checkbox');
await fireEvent.click(el);
expect(el).not.toBeDisabled();
expect(el).not.toBeChecked();
});
it('should check if is not disabled', async () => {
render({
template: '<input data-test-id="checkbox" type="checkbox" />',
});
const el = screen.getByTestId('checkbox');
await fireEvent.click(el);
expect(el).not.toBeDisabled();
expect(el).toBeChecked();
});
it('should not uncheck if is disabled', async () => {
render({
template: '<input data-test-id="checkbox" type="checkbox" checked disabled />',
});
const el = screen.getByTestId('checkbox');
await fireEvent.click(el);
expect(el).toBeDisabled();
expect(el).toBeChecked(); // ! FAIL
});
it('should not check if is disabled', async () => {
render({
template: '<input data-test-id="checkbox" type="checkbox" disabled />',
});
const el = screen.getByTestId('checkbox');
await fireEvent.click(el);
expect(el).toBeDisabled();
expect(el).not.toBeChecked(); // ! FAIL
});
和控制台输出
FAIL Checkbox.spec.js
✓ should uncheck if is not disabled (24ms)
✓ should check if is not disabled (3ms)
✕ should not uncheck if is disabled (5ms)
✕ should not check if is disabled (3ms)
● should not uncheck if is disabled
expect(element).toBeChecked()
Received element is not checked:
<input checked="checked" data-test-id="checkbox" disabled="disabled" type="checkbox" />
37 |
38 | expect(el).toBeDisabled();
> 39 | expect(el).toBeChecked(); // ! FAIL
| ^
40 | });
41 |
42 | it('should not check if is disabled', async () => {
at Object.<anonymous> (Checkbox.spec.js:39:14)
● should not check if is disabled
expect(element).not.toBeChecked()
Received element is checked:
<input data-test-id="checkbox" disabled="disabled" type="checkbox" />
50 |
51 | expect(el).toBeDisabled();
> 52 | expect(el).not.toBeChecked(); // ! FAIL
| ^
53 | });
54 |
at Object.<anonymous> (Checkbox.spec.js:52:18)
Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 passed, 4 total
Snapshots: 0 total
Time: 1.707s, estimated 3s