【发布时间】:2019-11-28 14:21:53
【问题描述】:
我已经为此苦苦挣扎了一段时间。它基本上归结为检查渲染的组件是否具有特定的类。我已经记录了它,只有当我在我正在搜索的特定类上调用 .find() 时它才会返回 true。这是我的输出:
test('Preview icon to be disabled', () => {
console.warn(component
.find('.preview-icon.is-disabled')
.at(0)
.html(),
);
console.warn(component
.find('.preview-icon.is-disabled')
.at(0)
.hasClass('is-disabled'),
);
console.warn(component
.find('.preview-icon')
.at(0)
.html(),
);
console.warn(component
.find('.preview-icon')
.at(0)
.hasClass('is-disabled'),
);
expect(component
.find('.preview-icon.is-disabled')
.at(0)
.exists(),
).toEqual(true);
});
这个输出:
console.warn client/src/components/Preview.test.js:69
<span class="toolbar-icon preview-icon is-disabled">Test Component</span>
console.warn client/src/components/Preview.test.js:75
true
console.warn client/src/components/Preview.test.js:81
<span class="toolbar-icon preview-icon is-disabled">Test Component</span>
console.warn client/src/components/Preview.test.js:87
false
我的问题是,为什么我的第一组日志记录为真(当我搜索 .preview-icon.is-disabled 时)而不是当我只搜索 .preview-icon 时? HTML 匹配,并且在第二组日志记录 HTML 中,它显然具有 is-disabled 类。
我的实际期望语句是我目前的解决方法,但我觉得这个测试缺少一些东西。任何帮助是极大的赞赏。谢谢你。
【问题讨论】:
-
你不会在寻找 component.find('.preview-icon').props('disabled') 。 ?
-
不太确定会做什么,它返回一个 HTML 元素属性的对象,但不包含
disabled的道具
标签: javascript reactjs jestjs enzyme