Skip to content

Commit

Permalink
add promise when calling func requestAppReview (#27)
Browse files Browse the repository at this point in the history
Add Promise While requesting InAppReview flow for analytic purpose..
  • Loading branch information
MinaSamir11 committed Feb 16, 2021
1 parent 18a138f commit a5a13db
Show file tree
Hide file tree
Showing 11 changed files with 15,792 additions and 149 deletions.
82 changes: 47 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
# react-native-in-app-review

<details>
<summary>Pre-Release(v3.1.0-beta), 2-2-2021</summary>

1- Add promise if flow has finished, to be like

```javascript
InAppReview.RequestInAppReview()
.then((hasFlowFinishedSuccessfully) => {
console.log('InAppReview ', hasFlowFinishedSuccessfully);

// 1- do something ex: (navigate Home page).

// 2- another option:
if (hasFlowFinishedSuccessfully) {
// do something
} else {
// do something else.
}

// The flow has finished. The API does not indicate whether the user
// reviewed or not, or even whether the review dialog was shown. Thus, no
// matter the result, we continue our app flow.
})
.catch((error) => {
//we continue our app flow.

console.log(error);
});
```

</details>

![npm](https://img.shields.io/npm/dw/react-native-in-app-review?logo=npm)

![npm](https://img.shields.io/npm/v/react-native-in-app-review?logo=npm)

![Travis (.com) branch](https://img.shields.io/travis/com/MinaSamir11/react-native-in-app-review/master)

[![Coverage Status](https://coveralls.io/repos/github/MinaSamir11/react-native-in-app-review/badge.svg)](https://coveralls.io/github/MinaSamir11/react-native-in-app-review)
![Coveralls github branch](https://img.shields.io/coveralls/github/MinaSamir11/react-native-in-app-review/master)

The Google Play In-App Review API, App store rating API lets you prompt users to submit Play Store or App store ratings and reviews without the inconvenience of leaving your app or game.

Expand Down Expand Up @@ -98,7 +66,7 @@ after following the instructions for your platform to link react-native-in-app-r

<details>
<summary>iOS details</summary>

### Using [CocoaPods](https://cocoapods.org/)

Add the following to your `Podfile` and run `pod install`:
Expand Down Expand Up @@ -176,9 +144,53 @@ import InAppReview from 'react-native-in-app-review';
InAppReview.isAvailable();

// trigger UI InAppreview
InAppReview.RequestInAppReview();
InAppReview.RequestInAppReview()
.then((hasFlowFinishedSuccessfully) => {
// when return true in android it means user finished or close review flow
console.log('InAppReview in android', hasFlowFinishedSuccessfully);

// when return true in ios it means review flow lanuched to user.
console.log(
'InAppReview in ios has lanuched successfully',
hasFlowFinishedSuccessfully,
);

// 1- you have option to do something ex: (navigate Home page) (in android).
// 2- you have option to do something,
// ex: (save date today to lanuch InAppReview after 15 days) (in android and ios).

// 3- another option:
if (hasFlowFinishedSuccessfully) {
// do something for ios
// do something for android
}

// for android:
// The flow has finished. The API does not indicate whether the user
// reviewed or not, or even whether the review dialog was shown. Thus, no
// matter the result, we continue our app flow.

// for ios
// the flow lanuched successfully, The API does not indicate whether the user
// reviewed or not, or he/she closed flow yet as android, Thus, no
// matter the result, we continue our app flow.
})
.catch((error) => {
//we continue our app flow.
// we have some error could happen while lanuching InAppReview,
// Check table for errors and code number that can return in catch.
console.log(error);
});
```

# Error could happen and code number

| Error Name | Code Number | Description | iOS | Android |
| ----------------------------- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | --- | ------- |
| ERROR_DEVICE_VERSION | 21 | This Device not supported to lanuch InAppReview |||
| GOOGLE_SERVICES_NOT_AVAILABLE | 22 | This Device doesn't support google play services |||
| [DYNAMIC ERROR NAME] | 23 | Unexpected error occur may return different error from different user and device check code number to get discovered errors messages that could be happen. |||

# + Android Notes:

# When to request an in-app review
Expand Down
134 changes: 124 additions & 10 deletions __tests__/InAppReview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,147 @@ jest.mock('react-native/Libraries/Utilities/Platform', () => {
return platform;
});

describe('react-native-in-app-review', () => {
it('should show Review Dialog in iOS if native module exist', () => {
Platform.OS = 'ios';
jest.mock('react-native/Libraries/BatchedBridge/NativeModules', () => {
let _NativeModules = {};

_NativeModules = {
InAppReviewModule: {show: jest.fn()},
RNInAppReviewIOS: {requestReview: jest.fn(), isAvailable: jest.fn()},
};

InAppReview.RequestInAppReview();
return _NativeModules;
});

describe('test-react-native-in-app-review-when-NativeModules-exist', () => {
it('should Not Call any Native method when paltform is not ios or android', async () => {
Platform.OS = 'windows';

await InAppReview.RequestInAppReview();

expect(
NativeModules.RNInAppReviewIOS.requestReview.mock.calls,
).toHaveLength(1);
).toHaveLength(0);

expect(NativeModules.InAppReviewModule.show.mock.calls).toHaveLength(0);
});

it('should lanuch in App review in android if native module exist and android api >= 21', () => {
it('should lanuch in App review in android if native module exist and android api >= 21', async () => {
Platform.OS = 'android';
Platform.Version = 21;

InAppReview.RequestInAppReview();
await InAppReview.RequestInAppReview();

expect(NativeModules.InAppReviewModule.show.mock.calls).toHaveLength(1);
});

it('should not lanuch in App review in android if native module exist and android api < 21', () => {
it('should show Review Dialog in iOS if native module exist', async () => {
Platform.OS = 'ios';

await InAppReview.RequestInAppReview();

expect(
NativeModules.RNInAppReviewIOS.requestReview.mock.calls,
).toHaveLength(1);
});

it('should return and throw error (ERROR_DEVICE_VERSION) if android api < 21', async () => {
Platform.OS = 'android';
Platform.Version = 19;

InAppReview.RequestInAppReview();
const rejectDeviceVersion = () => {
throw new Error('ERROR_DEVICE_VERSION');
};

expect(NativeModules.InAppReviewModule.show.mock.calls).toHaveLength(0);
jest
.spyOn(NativeModules.InAppReviewModule, 'show')
.mockReturnValueOnce(rejectDeviceVersion);

await InAppReview.RequestInAppReview();

expect(NativeModules.InAppReviewModule.show.mock.results[0].value).toThrow(
'ERROR_DEVICE_VERSION',
);
});

it('should return and throw error in iOS Platform (ERROR_DEVICE_VERSION) if iOS < 10.3', async () => {
Platform.OS = 'ios';

const rejectDeviceVersion = () => {
throw new Error('ERROR_DEVICE_VERSION');
};

jest
.spyOn(NativeModules.RNInAppReviewIOS, 'requestReview')
.mockReturnValueOnce(rejectDeviceVersion);

await InAppReview.RequestInAppReview();

expect(
NativeModules.RNInAppReviewIOS.requestReview.mock.results[0].value,
).toThrow('ERROR_DEVICE_VERSION');
});

it('should return and throw error (GOOGLE_SERVICES_NOT_AVAILABLE) if android device does not support GOOGLE_SERVICES', async () => {
Platform.OS = 'android';

const rejectGooglePlaySevices = () => {
throw new Error('GOOGLE_SERVICES_NOT_AVAILABLE');
};

jest
.spyOn(NativeModules.InAppReviewModule, 'show')
.mockReturnValueOnce(rejectGooglePlaySevices);

await InAppReview.RequestInAppReview();

expect(NativeModules.InAppReviewModule.show.mock.results[0].value).toThrow(
'GOOGLE_SERVICES_NOT_AVAILABLE',
);
});
it('should return and throw error With error code "23" in android env this refer that unexpected error happen', async () => {
Platform.OS = 'android';

const rejectGooglePlaySevices = () => {
throw new Error('23');
};

jest
.spyOn(NativeModules.InAppReviewModule, 'show')
.mockReturnValueOnce(rejectGooglePlaySevices);

await InAppReview.RequestInAppReview();

expect(NativeModules.InAppReviewModule.show.mock.results[0].value).toThrow(
'23',
);
});

it('should return true in android envirmoment when review flow finished', async () => {
Platform.OS = 'android';

jest
.spyOn(NativeModules.InAppReviewModule, 'show')
.mockResolvedValueOnce('true');

await InAppReview.RequestInAppReview();

expect(
NativeModules.InAppReviewModule.show.mock.results[0].value,
).resolves.toEqual('true');
});

it('should return true in iOS Platform when review lanuch successuffly', async () => {
Platform.OS = 'ios';

jest
.spyOn(NativeModules.RNInAppReviewIOS, 'requestReview')
.mockResolvedValueOnce('true');

await InAppReview.RequestInAppReview();

expect(
NativeModules.RNInAppReviewIOS.requestReview.mock.results[0].value,
).resolves.toEqual('true');
});

it('should return isAvailable true in android if android api >= 21', () => {
Expand Down
56 changes: 56 additions & 0 deletions __tests__/InAppReviewWithoutNativeModules-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

import {Platform} from 'react-native';
import InAppReview from '../';

jest.mock('react-native/Libraries/BatchedBridge/NativeModules', () => {
let _NativeModules = {};

return _NativeModules;
});

jest.mock('react-native/Libraries/Utilities/Platform', () => {
let platform = {
OS: 'ios',
};

const select = jest.fn().mockImplementation((obj) => {
const value = obj[platform.OS];
return !value ? obj.default : value;
});

platform.select = select;

return platform;
});

describe('test-react-native-in-app-review-without-NativeModules-exist', () => {
it('should throw error in iOS Module if module not exist', async () => {
Platform.OS = 'ios';

const errorMessageExpected =
'InAppReview native module not available, did you forget to link the library?';

expect(() => {
InAppReview.RequestInAppReview();
}).toThrow(errorMessageExpected);
});

it('should throw error in android Module if module not exist', async () => {
Platform.OS = 'android';

const errorMessageExpected =
'InAppReview native module not available, did you forget to link the library?';

expect(() => {
InAppReview.RequestInAppReview();
}).toThrow(errorMessageExpected);
});
});
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ android {

dependencies {
implementation "com.facebook.react:react-native:+" // From node_modules
implementation 'com.google.android.play:core:1.8.0'
implementation 'com.google.android.gms:play-services-base:16.0.1'
implementation 'com.google.android.play:core:1.9.0'
implementation 'com.google.android.gms:play-services-base:17.5.0'
}
Loading

0 comments on commit a5a13db

Please sign in to comment.