Skip to content
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

Closed

Conversation

ZauberNerd
Copy link
Contributor

@ZauberNerd ZauberNerd commented Apr 9, 2017

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
.
By doing something similar to:

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'

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Apr 9, 2017
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 })),
Copy link
Contributor

@peterp peterp Apr 10, 2017

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. :)

Copy link
Contributor Author

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.

@ZauberNerd ZauberNerd force-pushed the mock-push-notification-ios branch 2 times, most recently from de533a7 to 5fc9f18 Compare April 10, 2017 12:05
Copy link
Contributor

@DanielMSchmidt DanielMSchmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ZauberNerd ZauberNerd force-pushed the mock-push-notification-ios branch 2 times, most recently from f61430b to 0a56360 Compare May 10, 2017 13:51
@ZauberNerd
Copy link
Contributor Author

/cc @cpojer we briefly talked about this at the JSConf.eu where you asked me to ping you on this issue.
Would love to get some feedback on this if you have the time.

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 })),
Copy link
Contributor

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 }))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

@cpojer
Copy link
Contributor

cpojer commented May 10, 2017

Yap, this makes sense. Seems like CI is currently broken. If you rebase and make it green, I'm happy to merge it.

@ZauberNerd
Copy link
Contributor Author

ZauberNerd commented May 10, 2017

@cpojer thanks for the feedback, that was extremely fast!
I fixed the issues mentioned above and rebased against master but the CI is still failing.
I'll try again tomorrow.

`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'
```
@ZauberNerd
Copy link
Contributor Author

@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 BUCK file, it seems like these changes might be related (although now I'm in an area that I have no expertise in - I have never before used BUCK).

BUILD FAILED: Couldn't get dependency '//ReactCommon/jschelpers:jschelpers' of target '//ReactAndroid/src/main/jni/packagerconnection:jni':
Parse error for build file /home/ubuntu/react-native/ReactCommon/jschelpers/BUCK:
NameError: name 'THIS_IS_FBANDROID' is not defined
Call stack:
  File "/home/ubuntu/buck/build/classes/buck_parser/buck.py", line 1216, in process_with_diagnostics
    diagnostics=diagnostics)
  File "/home/ubuntu/buck/build/classes/buck_parser/buck.py", line 1105, in process
    os.path.join(self._project_root, path))
  File "/home/ubuntu/buck/build/classes/buck_parser/buck.py", line 1041, in _process_build_file
    return self._process(build_env, path, is_implicit_include=False)
  File "/home/ubuntu/buck/build/classes/buck_parser/buck.py", line 975, in _process
    exec(code, module.__dict__)
  File "/home/ubuntu/react-native/ReactCommon/jschelpers/BUCK", line 16
    if THIS_IS_FBANDROID:

@facebook-github-bot facebook-github-bot added the Import Started This pull request has been imported. This does not imply the PR has been approved. label May 11, 2017
@facebook-github-bot
Copy link
Contributor

@cpojer has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@ZauberNerd ZauberNerd deleted the mock-push-notification-ios branch May 11, 2017 15:22
Maftalion pushed a commit to csatf/react-native that referenced this pull request Jun 9, 2017
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Import Started This pull request has been imported. This does not imply the PR has been approved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants