admin管理员组

文章数量:1432155

I am trying to write a test to make sure that, when and when i pass a valid URL arg to a function it runs windows.open(args) to open it. then to make sure that i focus on it.

Test link validity:

export function isValidURL(url: string): boolean {
  try {
    new URL(url)
    return true
  } catch (e) {
    console.warn(`Invalid URL: ${url}`)
    return false
  }
}

Open link:

export function openURL(url: string): void {
  if (isValidURL(url)) {
    const exTab = window.open(url, "_blank")
    if (exTab) exTab.focus()
  }
}

I thinked that i should mock some function or maybe to fake it's code, then wait for it's number of call or something like that. but i'm new with jest and testing and i feel so confused of how that can be done.

My issay:

describe("Test tools.openURL()", () => {
  test("it should open link if valid.", () => {
    const { open } = window
    delete window.open
    window.open = jest.fn()
    openURL("htts//url2.de9v")
    expect(window.open).not.toHaveBeenCalled()
    openURL("")
    expect(window.open).toHaveBeenCalled()
    window.open = open
    // test focus here
  })
})

With this code i have succeeded to test the open, now i just need to test focus.

I am trying to write a test to make sure that, when and when i pass a valid URL arg to a function it runs windows.open(args) to open it. then to make sure that i focus on it.

Test link validity:

export function isValidURL(url: string): boolean {
  try {
    new URL(url)
    return true
  } catch (e) {
    console.warn(`Invalid URL: ${url}`)
    return false
  }
}

Open link:

export function openURL(url: string): void {
  if (isValidURL(url)) {
    const exTab = window.open(url, "_blank")
    if (exTab) exTab.focus()
  }
}

I thinked that i should mock some function or maybe to fake it's code, then wait for it's number of call or something like that. but i'm new with jest and testing and i feel so confused of how that can be done.

My issay:

describe("Test tools.openURL()", () => {
  test("it should open link if valid.", () => {
    const { open } = window
    delete window.open
    window.open = jest.fn()
    openURL("htts//url2.de9v")
    expect(window.open).not.toHaveBeenCalled()
    openURL("https://www.url1.dev")
    expect(window.open).toHaveBeenCalled()
    window.open = open
    // test focus here
  })
})

With this code i have succeeded to test the open, now i just need to test focus.

Share Improve this question edited Oct 1, 2019 at 21:11 Malek Boubakri asked Oct 1, 2019 at 17:42 Malek BoubakriMalek Boubakri 8662 gold badges18 silver badges37 bronze badges 2
  • 1 window.open('htts//url2.de9v') doesn't error – r3wt Commented Oct 1, 2019 at 17:47
  • @r3wt I edited my essay based on another idea (a workaround), now i successed testing open. I still need to test focus to have 100% coverage. please take a look.. – Malek Boubakri Commented Oct 1, 2019 at 21:21
Add a ment  | 

1 Answer 1

Reset to default 4

'open' is readonly property. Instead of jest.spyOn(window, "open") Try:

Object.defineProperty(window, 'open', { value: <your mock> });

in place of <your mock> locate mock object or object that should be returned.

本文标签: javascriptmocking a conditional windowopen function call with jestStack Overflow