代码之家  ›  专栏  ›  技术社区  ›  user762579

虚拟用户.js试验:w单元测试实用程序和笑话:[vue test utils]:包装器.销毁()只能在Vue实例上调用

  •  0
  • user762579  · 技术社区  · 7 年前

     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测试实用程序]:包装器.销毁()只能在Vue实例上调用

      177 |     expect(wrapper.contains('#playPauseBtn')).toBe(true); // OK
      178 |     // when
    > 179 |     player.destroy()
    

    我错在哪里?

    感谢您的反馈

    1 回复  |  直到 7 年前
        1
  •  2
  •   Danijel    7 年前

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

    destroy()

    不能对“HTML元素”调用destroy函数。我相信你想写信 wrapper.destroy()