0
Jestを使用してWindowオブジェクトまたはDocumentオブジェクトをどのように模擬しますか?特に、テストしているファイルでまだ定義されていない関数がネストされているとします。Jest - Mock WindowまたはDocumentオブジェクト
const foo = (payload) => {
window.webkit.messageHandlers.execute.postMessage(payload)
return true
}
// Jest: TypeError: Cannot read property 'messageHandlers' of undefined'
同様に、あなたはどのようにしてdocument.location
を模倣しますか?例えば。私はの方向に行っていた
const bar = (nonce, payload) => {
document.location = 'native://somefeature/send?nonce=' + nonce + '&payload=' + payload
return true
}
:
describe('foo',() => {
it('posts a message',() => {
const payload = 'payload'
window.webkit.messageHandlers.execute.postMessage = jest.fn()
let result = foo(payload)
expect(window.webkit.messageHandlers.execute.postMessage).toHaveBeenCalledWith(payload)
})
})
を私はあなたがdocument.location =
のために類似した何かをしたい推測しています。もちろん、それは簡単すぎるでしょうし、うまくいきません。