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
.
-
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 testfocus
to have 100% coverage. please take a look.. – Malek Boubakri Commented Oct 1, 2019 at 21:21
1 Answer
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
版权声明:本文标题:javascript - mocking a conditional window.open function call with jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745586567a2664941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论