vue.js unit:test w test-utils 和 Jest:如何在方法中测试 - window.open()?
vue.js unit:test w test-utils and Jest : How can I test - window.open() in a method?
在我的组件 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);
});
感谢反馈
您可以将 window.open
替换为一个有趣的模拟函数,然后像往常一样测试模拟调用。
window.open = jest.fn();
window.open('foo');
expect(window.open).toHaveBeenCalledWith('foo');
您可以检查 window.open
是否被
调用
const spy = jest.spyOn(window, 'open');
expect(spy).toHaveBeenCalledTimes(1)
在我的组件 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);
});
感谢反馈
您可以将 window.open
替换为一个有趣的模拟函数,然后像往常一样测试模拟调用。
window.open = jest.fn();
window.open('foo');
expect(window.open).toHaveBeenCalledWith('foo');
您可以检查 window.open
是否被
const spy = jest.spyOn(window, 'open');
expect(spy).toHaveBeenCalledTimes(1)