Skip to content

Commit 45521f6

Browse files
committed
add TDD for the package, add some devTool (husky,eslint,..)/ update .gitIgnore, add jest
1 parent 51eece7 commit 45521f6

8 files changed

+13006
-4
lines changed

.eslintrc.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
root: true,
3+
plugins: ['jest', 'unused-imports'],
4+
extends: [
5+
'@react-native-community',
6+
'plugin:react-hooks/recommended',
7+
'plugin:jest/recommended',
8+
'plugin:jest/style',
9+
],
10+
rules: {
11+
'unused-imports/no-unused-imports': 'warn',
12+
},
13+
env: {
14+
'jest/globals': true,
15+
},
16+
};

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
.gradle
44
local.properties
55
.project
6-
gradle
6+
gradle
7+
node_modules/
8+
test-results/
9+
coverage

.prettierrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
bracketSpacing: false,
3+
jsxBracketSameLine: true,
4+
singleQuote: true,
5+
trailingComma: 'all',
6+
};

__tests__/InAppReview-test.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
import {NativeModules, Platform} from 'react-native';
11+
import InAppReview from '../';
12+
13+
jest.mock('react-native/Libraries/Utilities/Platform', () => {
14+
let platform = {
15+
OS: 'ios',
16+
};
17+
18+
const select = jest.fn().mockImplementation((obj) => {
19+
const value = obj[platform.OS];
20+
return !value ? obj.default : value;
21+
});
22+
23+
platform.select = select;
24+
25+
return platform;
26+
});
27+
28+
describe('react-native-in-app-review', () => {
29+
it('should show Review Dialog in iOS if native module exist', () => {
30+
Platform.OS = 'ios';
31+
32+
InAppReview.RequestInAppReview();
33+
34+
expect(
35+
NativeModules.RNInAppReviewIOS.requestReview.mock.calls,
36+
).toHaveLength(1);
37+
});
38+
39+
it('should lanuch in App review in android if native module exist and android api >= 21', () => {
40+
Platform.OS = 'android';
41+
Platform.Version = 21;
42+
43+
InAppReview.RequestInAppReview();
44+
45+
expect(NativeModules.InAppReviewModule.show.mock.calls).toHaveLength(1);
46+
});
47+
48+
it('should not lanuch in App review in android if native module exist and android api < 21', () => {
49+
Platform.OS = 'android';
50+
Platform.Version = 19;
51+
52+
InAppReview.RequestInAppReview();
53+
54+
expect(NativeModules.InAppReviewModule.show.mock.calls).toHaveLength(0);
55+
});
56+
57+
it('should return isAvailable true in android if android api >= 21', () => {
58+
Platform.OS = 'android';
59+
Platform.Version = 22;
60+
61+
expect(InAppReview.isAvailable()).toBeTruthy();
62+
});
63+
it('should return isAvailable false in android if android api < 21', () => {
64+
Platform.OS = 'android';
65+
Platform.Version = 19;
66+
expect(InAppReview.isAvailable()).toBeFalsy();
67+
});
68+
});

babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['module:metro-react-native-babel-preset'],
3+
};

jest.setup.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow
9+
*/
10+
/* eslint-env jest */
11+
12+
import {NativeModules} from 'react-native';
13+
14+
// Mock the RNInAppReviewIOS native module to allow us to unit test the JavaScript code
15+
NativeModules.RNInAppReviewIOS = {
16+
requestReview: jest.fn(),
17+
isAvailable: jest.fn(),
18+
};
19+
20+
NativeModules.InAppReviewModule = {
21+
show: jest.fn(),
22+
};
23+
24+
// Reset the mocks before each test
25+
global.beforeEach(() => {
26+
jest.resetAllMocks();
27+
});

0 commit comments

Comments
 (0)