0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

window.openでpdfを別タブで開く方法

Last updated at Posted at 2021-05-23
// 同じタブ内でpdf開く
window.location.href = pdfDownloadUrl;

// 別タブでpdf開く
window.open(pdfDownloadUrl, '_blank')

###蛇足:vue-test-utilsの記述例

it('同じタブ内でpdf開くときのテスト', async () => {
    // window.location.hrefを書き換える
    global.window = Object.create(window);
    Object.defineProperty(window, 'location', {
        value: {
            href: "",
        },
    });

    // axiosでAPIレスポンス設定、mountなど色々

    // 検証
    expect(window.location.href).toBe(pdfDownloadUrl)
})

it('別タブでpdfを開くときのテスト。'', async () => {
    // windowオブジェクトをモック化
    const mockedOpen = jest.fn();
    const originalOpen = window.open;
    window.open = mockedOpen;

    // axiosでAPIレスポンス設定、mountなど色々

    // 検証
    expect(mockedOpen).toBeCalled();
    expect(mockedOpen).toHaveBeenCalledWith(pdfDownloadUrl, "_blank")
    // windowオブジェクトを復元
    window.open = originalOpen;
})
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?