为了在我的组件中测试 beforeDestroy() 钩子,我编写了以下规范:
it("should test lifecycle when audio tag is destroyed", () => {
// jsdom doesn't support any loading or playback media operations.
// As a workaround you can add a few stubs in your test setup:
window.HTMLMediaElement.prototype.removeEventListener = () => { /* do nothing */ };
// given
const wrapper = mount(AudioPlayer, {
// attachToDocument: true,
propsData: {
autoPlay: false,
file: file,
ended,
canPlay
}
});
wrapper.vm.loaded = true; // enable buttons
const player = wrapper.find("#player");
expect(wrapper.contains('#playPauseBtn')).toBe(true);
// when
player.destroy()
// then
expect(wrapper.contains('#playPauseBtn')).toBe(false);
});
但我收到一个错误,即使在文档中使用了 destroy() ...
[vue-test-utils]: wrapper.destroy() 只能在 Vue 实例上调用
177 | expect(wrapper.contains('#playPauseBtn')).toBe(true); // OK
178 | // when
> 179 | player.destroy()
我哪里错了?
感谢您的反馈