Skip to content

Commit

Permalink
feat(test): add jest tests and mock file for library users (#140)
Browse files Browse the repository at this point in the history
* feat: setup jest script
* feat: create jest.config file
* feat: create mocks to appevents and params
* feat: added jest to eslint
* feat: added test example inside readme
* feat: added spyOn example to Readme
* feat: added jest folder to package/json
  • Loading branch information
diegotsi committed Nov 19, 2021
1 parent e25a711 commit cc904e7
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
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);
```
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
@@ -0,0 +1,4 @@
module.exports = {
preset: 'react-native',
setupFiles: ['./jest/setup.js'],
};
63 changes: 63 additions & 0 deletions jest/mocks/index.js
@@ -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
@@ -0,0 +1,3 @@
/* eslint-env jest */

jest.mock('../src', () => require('./mocks').default);
8 changes: 5 additions & 3 deletions package.json
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"
},
"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

0 comments on commit cc904e7

Please sign in to comment.