Модуль vue.js: test w test-utils и Jest: Как я могу проверить - window.open () в методе? - PullRequest
0 голосов
/ 05 сентября 2018

В моем компоненте AudioPlayer у меня есть метод download ():

download() {
  this.audio.pause();
  window.open(this.file, "download");
},

Я могу проверить первую строку:

this.audio.pause();

Но как мне проверить (следует ли мне? Вторая строка:

window.open(this.file, "download");

Вот мой текущий тест спецификации файла

  it("should open a window from downloadBtn", async () => {
    // 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.pause = () => { /* do nothing */ };
    // given
    const wrapper = mount(AudioPlayer, {
      attachToDocument: true,
      propsData: {
        autoPlay: false,
        file: file,
        ended,
        canPlay
      }
    });
    const downloadBtn = wrapper.find("#downloadBtn");
    wrapper.vm.loaded = true; // enable downloadBtn
    // when
    downloadBtn.trigger("click");
    await wrapper.vm.$nextTick();
    // then
    expect(wrapper.vm.paused).toBe(true);
  });

спасибо за отзыв

Ответы [ 2 ]

0 голосов
/ 06 сентября 2018

вы можете проверить, был ли window.open вызван

const spy = jest.spyOn(window, 'open');
expect(spy).toHaveBeenCalledTimes(1)
0 голосов
/ 06 сентября 2018

Вы можете заменить window.open на шутку, а затем протестировать ложные вызовы, как обычно.

window.open = jest.fn();
window.open('foo');
expect(window.open).toHaveBeenCalledWith('foo');
...