Skip to content

Commit 7d1cdc4

Browse files
ahmed1hishamAliAbdelfattah
authored andcommitted
Add tests for Replies.showNotificationAndroid API
1 parent 28fa878 commit 7d1cdc4

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

__tests__/replies.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('Replies Module', () => {
2222
const setEnableInAppNotificationSound = sinon.spy(NativeModules.IBGReplies, 'setInAppNotificationSound');
2323
const setPushNotificationsEnabled = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationsEnabled');
2424
const setPushNotificationRegistrationToken = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationRegistrationToken');
25+
const showNotification = sinon.spy(NativeModules.IBGReplies, 'showNotification');
2526
const setNotificationIcon = sinon.spy(NativeModules.IBGReplies, 'setNotificationIcon');
2627
const setPushNotificationChannelId = sinon.spy(NativeModules.IBGReplies, 'setPushNotificationChannelId');
2728
const setSystemReplyNotificationSoundEnabled = sinon.spy(NativeModules.IBGReplies, 'setSystemReplyNotificationSoundEnabled');
@@ -32,6 +33,7 @@ describe('Replies Module', () => {
3233
IBGEventEmitter.removeAllListeners();
3334
setPushNotificationsEnabled.resetHistory();
3435
setPushNotificationRegistrationToken.resetHistory();
36+
showNotification.resetHistory();
3537
setNotificationIcon.resetHistory();
3638
setPushNotificationChannelId.resetHistory();
3739
setSystemReplyNotificationSoundEnabled.resetHistory();
@@ -140,6 +142,15 @@ describe('Replies Module', () => {
140142
expect(setPushNotificationRegistrationToken.calledOnceWithExactly('123')).toBe(true);
141143
});
142144

145+
146+
it('should call the native method showNotification on Android', () => {
147+
Platform.OS = 'android';
148+
Replies.showNotificationAndroid('test');
149+
150+
expect(showNotification.calledOnceWithExactly('test')).toBe(true);
151+
});
152+
153+
143154
it('should not call the native method setPushNotificationRegistrationToken on iOS', () => {
144155
Platform.OS = 'ios';
145156
Replies.setPushNotificationRegistrationTokenAndroid(true);

android/src/test/java/com/instabug/reactlibrary/RNInstabugRepliesModuleTest.java

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66

77
import com.facebook.react.bridge.Arguments;
88
import com.facebook.react.bridge.Callback;
9+
import com.facebook.react.bridge.JavaOnlyMap;
10+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
11+
import com.facebook.react.bridge.ReadableType;
12+
import com.facebook.react.bridge.WritableMap;
913
import com.facebook.react.bridge.WritableNativeArray;
14+
import com.facebook.react.bridge.WritableNativeMap;
1015
import com.instabug.bug.BugReporting;
1116
import com.instabug.chat.Replies;
1217
import com.instabug.library.Feature;
1318
import com.instabug.reactlibrary.utils.InstabugUtil;
19+
import com.instabug.reactlibrary.utils.MapUtil;
1420
import com.instabug.survey.Surveys;
1521
import com.instabug.reactlibrary.utils.MainThreadHandler;
1622

@@ -27,6 +33,8 @@
2733

2834
import java.util.concurrent.Executors;
2935
import java.util.concurrent.ScheduledExecutorService;
36+
import java.util.Map;
37+
import java.util.HashMap;
3038

3139
import static org.mockito.Matchers.any;
3240
import static org.mockito.Matchers.anyLong;
@@ -36,7 +44,7 @@
3644
import static org.powermock.api.mockito.PowerMockito.when;
3745

3846
@RunWith(PowerMockRunner.class)
39-
@PrepareForTest({Looper.class, android.os.Handler.class, BugReporting.class, Replies.class, Surveys.class, SystemClock.class, Runnable.class, WritableNativeArray.class, JSONObject.class, Arguments.class, InstabugUtil.class, RNInstabugRepliesModule.class, MainThreadHandler.class})
47+
@PrepareForTest({Looper.class, android.os.Handler.class, BugReporting.class, Replies.class, Surveys.class, SystemClock.class, Runnable.class, WritableNativeMap.class, WritableNativeArray.class, JSONObject.class, Arguments.class, InstabugUtil.class, RNInstabugRepliesModule.class, MainThreadHandler.class})
4048

4149
public class RNInstabugRepliesModuleTest {
4250
private RNInstabugRepliesModule rnModule = new RNInstabugRepliesModule(null);
@@ -170,6 +178,65 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable {
170178
Replies.setPushNotificationRegistrationToken("123");
171179
}
172180

181+
@Test
182+
public void givenBoolean$showNotification_whenQuery_thenShouldCallNativeApi() {
183+
// given
184+
PowerMockito.mockStatic(Replies.class);
185+
PowerMockito.mockStatic(SystemClock.class);
186+
PowerMockito.mockStatic(Arguments.class);
187+
PowerMockito.mock(JSONObject.class);
188+
189+
// when
190+
final long ts = SystemClock.uptimeMillis();
191+
PowerMockito.when(Arguments.createMap())
192+
.thenAnswer(
193+
new Answer<Object>() {
194+
@Override
195+
public Object answer(InvocationOnMock invocation) throws Throwable {
196+
return new JavaOnlyMap();
197+
}
198+
});
199+
PowerMockito.when(SystemClock.uptimeMillis())
200+
.thenAnswer(
201+
new Answer<Object>() {
202+
@Override
203+
public Object answer(InvocationOnMock invocation) throws Throwable {
204+
return ts;
205+
}
206+
});
207+
208+
HashMap<String, Object> hm = new HashMap<>();
209+
hm.put("message", "{\"aps\":{\"alert\":\"You have an unread message from RN-TestAnything team\"},\"IBGHost\":true}");
210+
WritableMap readableMap = MapUtil.toWritableMap(hm);
211+
212+
Map<String, String> map = new HashMap<>();
213+
ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
214+
while (iterator.hasNextKey()) {
215+
String key = iterator.nextKey();
216+
ReadableType type = readableMap.getType(key);
217+
218+
switch(type) {
219+
case String:
220+
String value = readableMap.getString(key);
221+
map.put(key, value);
222+
break;
223+
}
224+
}
225+
226+
PowerMockito.when(Replies.isInstabugNotification(map))
227+
.thenAnswer(
228+
new Answer<Object>() {
229+
@Override
230+
public Object answer(InvocationOnMock invocation) throws Throwable {
231+
return true;
232+
}
233+
});
234+
rnModule.showNotification(readableMap);
235+
236+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
237+
Replies.showNotification(map);
238+
}
239+
173240
@Test
174241
public void givenBoolean$setNotificationIcon_whenQuery_thenShouldCallNativeApi() {
175242
// given

jest/mockReplies.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jest.mock('NativeModules', () => {
1111
addListener: jest.fn(),
1212
setPushNotificationsEnabled: jest.fn(),
1313
setPushNotificationRegistrationToken: jest.fn(),
14+
showNotification: jest.fn(),
1415
setNotificationIcon: jest.fn(),
1516
setPushNotificationChannelId: jest.fn(),
1617
setSystemReplyNotificationSoundEnabled: jest.fn(),

0 commit comments

Comments
 (0)