-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add jest mock for 'PushNotificationManager' #13410
Conversation
jest/setup.js
Outdated
getApplicationIconBadgeNumber: jest.fn(callback => process.nextTick(() => callback(0))), | ||
cancelLocalNotifications: jest.fn(), | ||
getScheduledLocalNotifications: jest.fn(callback => process.nextTick(() => callback())), | ||
requestPermissions: jest.fn(() => ({ alert: true, badge: true, sound: true })), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know this should return a promise, but I'm not also 100% certain that jest.fn()
isn't returning a promise. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thank you!
Fixed and rebased.
de533a7
to
5fc9f18
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
f61430b
to
0a56360
Compare
/cc @cpojer we briefly talked about this at the JSConf.eu where you asked me to ping you on this issue. |
jest/setup.js
Outdated
getApplicationIconBadgeNumber: jest.fn(callback => process.nextTick(() => callback(0))), | ||
cancelLocalNotifications: jest.fn(), | ||
getScheduledLocalNotifications: jest.fn(callback => process.nextTick(() => callback())), | ||
requestPermissions: jest.fn(() => Promise.resolve({ alert: true, badge: true, sound: true })), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code style: do not use spaces around curly braces.
jest/setup.js
Outdated
getScheduledLocalNotifications: jest.fn(callback => process.nextTick(() => callback())), | ||
requestPermissions: jest.fn(() => Promise.resolve({ alert: true, badge: true, sound: true })), | ||
abandonPermissions: jest.fn(), | ||
checkPermissions: jest.fn(callback => process.nextTick(() => callback({ alert: true, badge: true, sound: true }))), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here too
Yap, this makes sense. Seems like CI is currently broken. If you rebase and make it green, I'm happy to merge it. |
0a56360
to
36ec619
Compare
@cpojer thanks for the feedback, that was extremely fast! |
`PushNotificationIOS` is a library that makes use of the `PushNotificationManager` native module. This commit adds a jest mock for its native module so that code that uses `PushNotificationIOS` can be tested using jest. In order to enable behaviour in unit tests it is possible to replace or overwrite the mock implementation, see the [jest guide on mocks](https://facebook.github.io/jest/docs/mock-functions.html). By doing something similar to: ```javascript import { NativeModules } from 'react-native'; const { PushNotificationManager } = NativeModules; // mock 'checkPermissions' to return a different value than the default: PushNotificationManager.checkPermissions.mockImplementationOnce((callback) => { // execute callback in next tick to enable async behaviour process.nextTick(() => callback({ alert: false, badge: false, sound: false })); }); // execute unit tests for code that makes use of 'PushNotificationIOS' ```
36ec619
to
b1c2eb0
Compare
@cpojer looking at the commits in master (https://github.com/facebook/react-native/commits/master) and their CI status, I can make the following observations:
And by looking at the error message / stack trace from circleCI and the modifications of the aforementioned
|
@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator. |
Summary: `PushNotificationIOS` is a library that makes use of the `PushNotificationManager` native module. This commit adds a jest mock for its native module so that code that uses `PushNotificationIOS` can be tested using jest. In order to enable behaviour in unit tests it is possible to replace or overwrite the mock implementation, see the [jest guide on mocks](https://facebook.github.io/jest/docs/mock-functions.html). By doing something similar to: ```javascript import { NativeModules } from 'react-native'; const { PushNotificationManager } = NativeModules; // mock 'checkPermissions' to return a different value than the default: PushNotificationManager.checkPermissions.mockImplementationOnce((callback) => { // execute callback in next tick to enable async behaviour process.nextTick(() => callback({ alert: false, badge: false, sound: false })); }); // execute unit tests for code that makes use of 'PushNotificationIOS' ``` Closes facebook#13410 Differential Revision: D5043904 Pulled By: cpojer fbshipit-source-id: 11e73cd215ba6854d06f4ac7a5aea0ab4be26584
PushNotificationIOS
is a library that makes use of thePushNotificationManager
native module.This commit adds a jest mock for its native module so that code that
uses
PushNotificationIOS
can be tested using jest.In order to enable behaviour in unit tests it is possible to replace or
overwrite the mock implementation, see the jest guide on
mocks.
By doing something similar to: