Skip to content

Commit 283b89f

Browse files
committed
add new test, init new promise in android related to InAppcomment func (#114)
1 parent 039ff2d commit 283b89f

File tree

4 files changed

+27919
-27897
lines changed

4 files changed

+27919
-27897
lines changed

__tests__/InAppReview-test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jest.mock('react-native/Libraries/BatchedBridge/NativeModules', () => {
2929
let _NativeModules = {};
3030

3131
_NativeModules = {
32-
InAppReviewModule: {show: jest.fn()},
32+
InAppReviewModule: {show: jest.fn(), showInAppCommentHMS: jest.fn()},
3333
RNInAppReviewIOS: {requestReview: jest.fn(), isAvailable: jest.fn()},
3434
};
3535

@@ -64,7 +64,9 @@ describe('test-react-native-in-app-review-when-NativeModules-exist', () => {
6464

6565
await InAppReview.requestInAppCommentAppGallery();
6666

67-
expect(NativeModules.InAppReviewModule.show.mock.calls).toHaveLength(1);
67+
expect(
68+
NativeModules.InAppReviewModule.showInAppCommentHMS.mock.calls,
69+
).toHaveLength(1);
6870
});
6971

7072
it('should show Review Dialog in iOS if native module exist', async () => {

__tests__/InAppReviewWithoutNativeModules-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,14 @@ describe('test-react-native-in-app-review-without-NativeModules-exist', () => {
5353
InAppReview.RequestInAppReview();
5454
}).toThrow(errorMessageExpected);
5555
});
56+
57+
it('should throw error in calling in app comment Module if module not exist', async () => {
58+
Platform.OS = 'android';
59+
60+
const errorMessageExpected =
61+
'InAppReview native module not available, did you forget to link the library?';
62+
expect(() => {
63+
InAppReview.requestInAppCommentAppGallery();
64+
}).toThrow(errorMessageExpected);
65+
});
5666
});

android/src/main/java/com/ibits/react_native_in_app_review/AppReviewModule.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class AppReviewModule extends ReactContextBaseJavaModule implements Activ
2525

2626
private final ReactApplicationContext mContext;
2727
private Promise pendingPromise;
28+
private Promise pendingInAppCommentPromise;
2829

2930
public AppReviewModule(ReactApplicationContext reactContext) {
3031
super(reactContext);
@@ -87,15 +88,16 @@ public void show(final Promise promise) {
8788
}
8889

8990
@ReactMethod
90-
public void showInAppCommentHMS () {
91+
public void showInAppCommentHMS (final Promise promise) {
92+
this.pendingInAppCommentPromise = promise;
9193
Activity currentActivity = getCurrentActivity();
9294

9395
Intent intent = new Intent("com.huawei.appmarket.intent.action.guidecomment");
9496
intent.setPackage("com.huawei.appmarket");
9597
if (currentActivity != null) {
9698
currentActivity.startActivityForResult(intent, 1001);
9799
}else {
98-
rejectPromise("24", new Error("ACTIVITY_DOESN'T_EXIST"));
100+
rejectPromiseHMS("24", new Error("ACTIVITY_DOESN'T_EXIST"));
99101
}
100102

101103
}
@@ -107,6 +109,13 @@ private void rejectPromise(String code, Error err) {
107109
}
108110
}
109111

112+
private void rejectPromiseHMS(String code, Error err) {
113+
if (this.pendingInAppCommentPromise != null) {
114+
this.pendingInAppCommentPromise.reject(code, err);
115+
this.pendingInAppCommentPromise = null;
116+
}
117+
}
118+
110119
private void resolvePromise(boolean hasFlowFinishedSuccessfully) {
111120
if (this.pendingPromise != null) {
112121
this.pendingPromise.resolve(hasFlowFinishedSuccessfully);
@@ -115,12 +124,12 @@ private void resolvePromise(boolean hasFlowFinishedSuccessfully) {
115124
}
116125

117126
private void resolvePromiseHMS(int resultCode) {
118-
if (this.pendingPromise != null) {
119-
this.pendingPromise.resolve(resultCode);
120-
this.pendingPromise = null;
127+
if (this.pendingInAppCommentPromise != null) {
128+
this.pendingInAppCommentPromise.resolve(resultCode);
121129
}
122130
}
123131

132+
124133
private boolean isGooglePlayServicesAvailable() {
125134
GoogleApiAvailability GMS = GoogleApiAvailability.getInstance();
126135
int isGMS = GMS.isGooglePlayServicesAvailable(mContext);
@@ -133,10 +142,10 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
133142
if (resultCode == 101) {
134143
// Ensure that your app has been correctly released on AppGallery.
135144
Log.e("huawei_errorrr1 Ensure that your app has been correctly released on AppGallery", isGooglePlayServicesAvailable() + "");
136-
rejectPromise("101", new Error("Ensure that your app has been correctly released on AppGallery"));
145+
rejectPromiseHMS("101", new Error("Ensure that your app has been correctly released on AppGallery"));
137146
} else if (resultCode == 0){
138147
Log.e("huawei_errorrr1 in app comment Unknown error.", "Unknown error");
139-
rejectPromise("0", new Error("in app comment Unknown error"));
148+
rejectPromiseHMS("0", new Error("in app comment Unknown error"));
140149
}
141150
else if (resultCode == 102){
142151
Log.e("huawei_errorrr1 rating submitted", "rating done");
@@ -145,20 +154,20 @@ else if (resultCode == 102){
145154
Log.e("huawei_errorrr1 Comment submitted", "rating done");
146155
resolvePromiseHMS(103);
147156
} else if (resultCode == 104) {
148-
rejectPromise("104", new Error("check the HUAWEI ID sign-in status"));
157+
rejectPromiseHMS("104", new Error("check the HUAWEI ID sign-in status"));
149158
// Prompt the user to check the HUAWEI ID sign-in status.
150159
Log.e("huawei_errorrr1 check the HUAWEI ID sign-in status", "");
151160
} else if (resultCode == 105) {
152-
rejectPromise("105", new Error("The user does not meet the conditions for displaying the comment pop-up"));
161+
rejectPromiseHMS("105", new Error("The user does not meet the conditions for displaying the comment pop-up"));
153162
Log.e("huawei_errorrr1 The user does not meet the conditions for displaying the comment pop-up", "");
154163
} else if (resultCode == 106){
155-
rejectPromise("106", new Error("The commenting function is disabled"));
164+
rejectPromiseHMS("106", new Error("The commenting function is disabled"));
156165
Log.e(" huawei_errorrr1 The commenting function is disabled", "disabled");
157166
} else if (resultCode == 107){
158-
rejectPromise("107", new Error("The in-app commenting service is not supported. (Apps released in the Chinese mainland do not support this service.)"));
167+
rejectPromiseHMS("107", new Error("The in-app commenting service is not supported. (Apps released in the Chinese mainland do not support this service.)"));
159168
Log.e("huawei_errorrr1 The in-app commenting service is not supported. (Apps released in the Chinese mainland do not support this service.)", "in-app commenting service is not supported");
160169
} else if (resultCode == 108){
161-
rejectPromise("108", new Error("The user canceled the comment"));
170+
rejectPromiseHMS("108", new Error("The user canceled the comment"));
162171
Log.e("huawei_errorrr1 The user canceled the comment.", "user canceled");
163172
}
164173
}

0 commit comments

Comments
 (0)