3

为了在我的组件中测试 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()

我哪里错了?

感谢您的反馈

4

1 回答 1

4

const player = wrapper.find("#player");返回一个 DOM 元素的包装器,所以基本上是 HTML。

destroy()销毁一个 Vue 组件实例。

您不能在“HTML 元素”上调用销毁函数。我相信你想写wrapper.destroy()

于 2018-09-05T21:01:53.327 回答