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

feat: jest tests #140

Merged
merged 11 commits into from
Nov 19, 2021
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,24 @@ yarn example:android
Note: You'll probably want to change the Facebook App ID to your own, else the example app won't be able to login. To change it, edit your local copy of `refresh-example.sh`, update the `FacebookAppId` variable, then re-run `refresh-example.sh` to regenerate the example directory.


## Testing with Jest

We have a example mock inside `jest/setup.js` but you just add the following line to your setup file:

```js
jest.mock('react-native-fbsdk-next', () => require('react-native-fbsdk-next/jest/mocks').default);
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
```

You also can spyOn one of this mock to return whatever you want inside your test:

```js
import { LoginManager } from 'react-native-fbsdk-next'

jest.spyOn(LoginManager, 'logInWithPermissions').mockImplementation(() => Promise.resolve({ isCancelled: false }))

```


## Join the React Native community

- Website: https://facebook.github.io/react-native
Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'react-native',
setupFiles: ['./jest/setup.js'],
};
63 changes: 63 additions & 0 deletions jest/mocks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const nonMocked = require('../../lib/commonjs');

export const mockAppEvents = {
AchievedLevel: 'fb_mobile_level_achieved',
AdClick: 'AdClick',
AdImpression: 'AdImpression',
AddedPaymentInfo: 'fb_mobile_add_payment_info',
AddedToCart: 'fb_mobile_add_to_cart',
AddedToWishlist: 'fb_mobile_add_to_wishlist',
CompletedRegistration: 'fb_mobile_complete_registration',
CompletedTutorial: 'fb_mobile_tutorial_completion',
Contact: 'Contact',
CustomizeProduct: 'CustomizeProduct',
Donate: 'Donate',
FindLocation: 'FindLocation',
InitiatedCheckout: 'fb_mobile_initiated_checkout',
Purchased: 'fb_mobile_purchase',
Rated: 'fb_mobile_rate',
Schedule: 'Schedule',
Searched: 'fb_mobile_search',
SpentCredits: 'fb_mobile_spent_credits',
StartTrial: 'StartTrial',
SubmitApplication: 'SubmitApplication',
Subscribe: 'Subscribe',
UnlockedAchievement: 'fb_mobile_achievement_unlocked',
ViewedContent: 'fb_mobile_content_view',
};

export const mockAppEventParams = {
AddType: 'ad_type',
Content: 'fb_content',
ContentID: 'fb_content_id',
ContentType: 'fb_content_type',
Currency: 'fb_currency',
Description: 'fb_description',
Level: 'fb_level',
MaxRatingValue: 'fb_max_rating_value',
NumItems: 'fb_num_items',
OrderId: 'fb_order_id',
PaymentInfoAvailable: 'fb_payment_info_available',
RegistrationMethod: 'fb_registration_method',
SearchString: 'fb_search_string',
Success: 'fb_success',
ValueNo: '0',
ValueYes: '1',
};

export default {
AppEventsLogger: {
logEvent: jest.fn(),
logPurchase: jest.fn(),
setUserID: jest.fn(),
AppEventParams: mockAppEventParams,
AppEvents: mockAppEvents,
},
LoginManager: {
logInWithPermissions: jest.fn(),
},
Settings: {
initializeSDK: jest.fn(),
},
LoginButton: nonMocked.LoginButton,
};
3 changes: 3 additions & 0 deletions jest/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-env jest */

jest.mock('../src', () => require('./mocks').default);
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"prepare": "bob build",
"flow": "flow",
"lint": "eslint ./src",
"test": "yarn validate:prettier && yarn validate:eslint",
"test": "yarn validate:prettier && yarn validate:eslint && yarn jest",
"validate:eslint": "eslint \"src/**/*\"",
"validate:flow": "flow",
"validate:prettier": "prettier \"src/**/*.js\" --check",
Expand All @@ -62,7 +62,8 @@
"example:clean": "cd RNFBSDKExample && \\rm -fr yarn.lock node_modules ios/Podfile.lock && cd ..",
"example:install": "cd RNFBSDKExample && yarn && cd ios && (pod install || true) && cd ../..",
"example:devcopy": "yarn prepare && cp -rv android ios lib src types *.podspec RNFBSDKExample/node_modules/react-native-fbsdk-next/",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"jest": "jest"
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
},
"files": [
"android/src/main/AndroidManifest.xml",
Expand All @@ -74,7 +75,8 @@
"LICENSE.txt",
"README.md",
"react-native-fbsdk-next.podspec",
"types"
"types",
"jest"
],
"dependencies": {},
"peerDependencies": {
Expand Down