我有一个'notifications.js'模块,看起来像这样:
import { Notifications, Permissions } from 'expo'
export function setLocalNotification(storage = AsyncStorage) {
return storage
.getItem(NOTIFICATION_KEY)
.then(JSON.parse)
.then(data => {
if (data === null) {
return Permissions.askAsync(
Permissions.NOTIFICATIONS
).then(({ status }) => {
if (status === 'granted') {
Notifications.cancelAllScheduledNotificationsAsync()
...etc.
在我的测试中,我想模拟“权限”和“通知”,以便可以在notifications.spec.js中执行以下操作:
import { setLocalNotification } from './notifications'
import mockAsyncStorage from '../mock/AsyncStorage'
it('correctly cancels pending notifications', done => {
setLocalNotification(mockAsyncStorage).then(done())
expect(Permissions.askAsync).toBeCalled()
expect(Notifications.cancelAllScheduledNotificationsAsync)
.toBeCalled()
})
我已经尝试过使用各种方法jest.mock
,jest.setMock
但是似乎无法正常工作。如何以所需的方式模拟这些命名的导入?例如,我已经尝试过了:
jest.setMock('Permissions', () => ({
askAsync: jest
.fn()
.mockImplementationOnce(() => ({ status: 'granted' }))
}))
但这是行不通的。它抛出
'module Permissions cannot be found from notifications.spec.js'
而且,如果我尝试模拟整个expo模块,则模拟的函数expect().toBeCalled()
将返回false。
您必须模拟模块 'expo'
jest.mock('expo', ()=>({
Permissions: {
askAsync: jest.fn()
}
}))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句