Skip to content

Instantly share code, notes, and snippets.

@mattste
Created November 4, 2016 04:39
Show Gist options
  • Select an option

  • Save mattste/e034f609878512071bcce17303130b86 to your computer and use it in GitHub Desktop.

Select an option

Save mattste/e034f609878512071bcce17303130b86 to your computer and use it in GitHub Desktop.
Jest Mock Help
// Is there a way for me to mock the fetchNotifications call?
export function fetchNotifications() {
return dispatch => {
dispatch(fetchNotificationsRequest())
return get('/notifications/self')
.then(json => dispatch(fetchNotificationsSuccess(json)))
.catch(err => dispatch(fetchNotificationsFailure(err)))
}
}
export function updateCreatedNotification(notification) {
return dispatch => {
dispatch(updateCreatedNotificationRequest(notification))
return put(`/notification/${notification.id}`, notification)
.then((json) => {
// HOW TO MOCK THIS FETCHNOTIFICATIONS CALL?
return dispatch(fetchNotifications()).then(() => {
return dispatch(updateCreatedNotificationSuccess(json))
})
}).catch((err) => {throw dispatch(updateCreatedNotificationFailure(err))})
}
}
import * as actions from './index'
test('created UPDATE_CREATED_NOTIFICATION_SUCCESS when updating notification request has returned successfully', () => {
actions.fetchNotifications = jest.fn(() => {
console.log('called mock')
return Promise.resolve([])
})
actions.updateCreatedNotification(mockNotification)
actions.fetchNotifications.mock.calledOnce()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment