Skip to content

Commit 728a2e1

Browse files
authored
🔨 refactor js code (#346)
* 🔨 refactor js code * 🐛 fix failing tests and add test to check that setDidSelectPromptOptionHandler does nothing when platform is android
1 parent 75f9ca9 commit 728a2e1

File tree

5 files changed

+36
-100
lines changed

5 files changed

+36
-100
lines changed

__tests__/index.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ describe('Instabug Module', () => {
156156

157157
it('should call the native method didSelectPromptOptionHandler with a function', () => {
158158

159+
Platform.OS = 'ios';
159160
const callback = jest.fn()
160161
Instabug.setDidSelectPromptOptionHandler(callback);
161162

@@ -165,6 +166,7 @@ describe('Instabug Module', () => {
165166

166167
it('should invoke callback on emitting the event IBGDidSelectPromptOptionHandler', (done) => {
167168

169+
Platform.OS = 'ios';
168170
const payload = { promptOption: Instabug.promptOption.bug };
169171
const callback = (promptOption) => {
170172
expect(promptOption).toBe(payload.promptOption);
@@ -176,6 +178,17 @@ describe('Instabug Module', () => {
176178
expect(IBGEventEmitter.getListeners(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER).length).toEqual(1);
177179
});
178180

181+
it('should return on calling setDidSelectPromptOptionHandler when Platform is android', () => {
182+
183+
Platform.OS = 'android';
184+
185+
Instabug.setDidSelectPromptOptionHandler(jest.fn());
186+
IBGEventEmitter.emit(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER, {});
187+
188+
expect(didSelectPromptOptionHandler.notCalled).toBe(true);
189+
expect(IBGEventEmitter.getListeners(IBGConstants.DID_SELECT_PROMPT_OPTION_HANDLER).length).toEqual(0);
190+
});
191+
179192
it('should call the native method setSessionProfilerEnabled', () => {
180193

181194
Instabug.setSessionProfilerEnabled(true);

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export namespace BugReporting {
1212
invocationMode: invocationMode,
1313
invocationOptions: invocationOptions[]
1414
): void;
15-
function onInvokeHandler(preInvocationHandler: () => void): void;
15+
function onInvokeHandler(handler: () => void): void;
1616
function onReportSubmitHandler(preSendingHandler: () => void): void;
17-
function onSDKDismissedHandler(postInvocationHandler: (dismiss: dismissType, report: reportType) => void): void;
17+
function onSDKDismissedHandler(handler: (dismiss: dismissType, report: reportType) => void): void;
1818
function setPromptOptionsEnabled(
1919
chat: boolean,
2020
bug: boolean,

index.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {
22
NativeModules,
3-
NativeAppEventEmitter,
4-
DeviceEventEmitter,
53
Platform,
64
findNodeHandle,
75
processColor
@@ -129,23 +127,10 @@ const InstabugModule = {
129127
* gets executed when a prompt option is selected.
130128
*/
131129
setDidSelectPromptOptionHandler: function(didSelectPromptOptionHandler) {
132-
if (Platform.OS === 'ios') {
133-
Instabug.addListener('IBGDidSelectPromptOptionHandler');
134-
NativeAppEventEmitter.addListener(
135-
'IBGDidSelectPromptOptionHandler',
136-
function(payload) {
137-
didSelectPromptOptionHandler(payload['promptOption']);
138-
}
139-
);
140-
} else {
141-
DeviceEventEmitter.addListener(
142-
'IBGDidSelectPromptOptionHandler',
143-
function(payload) {
144-
didSelectPromptOptionHandler(payload.promptOption);
145-
}
146-
);
147-
}
148-
130+
if (Platform.OS === 'android') return;
131+
IBGEventEmitter.addListener(InstabugConstants.DID_SELECT_PROMPT_OPTION_HANDLER, (payload) => {
132+
didSelectPromptOptionHandler(payload.promptOption);
133+
});
149134
Instabug.didSelectPromptOptionHandler(didSelectPromptOptionHandler);
150135
},
151136

@@ -562,20 +547,7 @@ const InstabugModule = {
562547
* executed when a new message is received.
563548
*/
564549
setOnNewMessageHandler: function(onNewMessageHandler) {
565-
if (Platform.OS === 'ios') {
566-
Instabug.addListener('IBGonNewMessageHandler');
567-
NativeAppEventEmitter.addListener(
568-
'IBGonNewMessageHandler',
569-
onNewMessageHandler
570-
);
571-
} else {
572-
DeviceEventEmitter.addListener(
573-
'IBGonNewMessageHandler',
574-
onNewMessageHandler
575-
);
576-
}
577-
578-
Instabug.setOnNewMessageHandler(onNewMessageHandler);
550+
Replies.setOnNewReplyReceivedHandler(onNewMessageHandler);
579551
},
580552

581553
/**

modules/BugReporting.js

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
NativeModules,
3-
NativeAppEventEmitter,
4-
DeviceEventEmitter,
53
Platform
64
} from 'react-native';
75
let { Instabug } = NativeModules;
86
import InstabugModule from '../index';
7+
import IBGEventEmitter from '../utils/IBGEventEmitter';
8+
import InstabugConstants from '../utils/InstabugConstants';
99

1010
/**
1111
* BugReporting
@@ -76,23 +76,11 @@ export default {
7676
* Sets a block of code to be executed just before the SDK's UI is presented.
7777
* This block is executed on the UI thread. Could be used for performing any
7878
* UI changes before the SDK's UI is shown.
79-
* @param {function} preInvocationHandler - A callback that gets executed before invoking the SDK
79+
* @param {function} handler - A callback that gets executed before invoking the SDK
8080
*/
81-
onInvokeHandler: function(preInvocationHandler) {
82-
if (Platform.OS === 'ios') {
83-
Instabug.addListener('IBGpreInvocationHandler');
84-
NativeAppEventEmitter.addListener(
85-
'IBGpreInvocationHandler',
86-
preInvocationHandler
87-
);
88-
} else {
89-
DeviceEventEmitter.addListener(
90-
'IBGpreInvocationHandler',
91-
preInvocationHandler
92-
);
93-
}
94-
95-
Instabug.setPreInvocationHandler(preInvocationHandler);
81+
onInvokeHandler: function(handler) {
82+
IBGEventEmitter.addListener(InstabugConstants.ON_INVOKE_HANDLER, handler);
83+
Instabug.setPreInvocationHandler(handler);
9684
},
9785

9886
/**
@@ -111,26 +99,14 @@ export default {
11199
* Sets a block of code to be executed right after the SDK's UI is dismissed.
112100
* This block is executed on the UI thread. Could be used for performing any
113101
* UI changes after the SDK's UI is dismissed.
114-
* @param {function} postInvocationHandler - A callback to get executed after
102+
* @param {function} handler - A callback to get executed after
115103
* dismissing the SDK.
116104
*/
117-
onSDKDismissedHandler: function(postInvocationHandler) {
118-
if (Platform.OS === 'ios') {
119-
Instabug.addListener('IBGpostInvocationHandler');
120-
NativeAppEventEmitter.addListener('IBGpostInvocationHandler', function(
121-
payload
122-
) {
123-
postInvocationHandler(payload['dismissType'], payload['reportType']);
124-
});
125-
} else {
126-
DeviceEventEmitter.addListener('IBGpostInvocationHandler', function(
127-
payload
128-
) {
129-
postInvocationHandler(payload.dismissType, payload.reportType);
130-
});
131-
}
132-
133-
Instabug.setPostInvocationHandler(postInvocationHandler);
105+
onSDKDismissedHandler: function(handler) {
106+
IBGEventEmitter.addListener(InstabugConstants.ON_SDK_DISMISSED_HANDLER, (payload) => {
107+
handler(payload.dismissType, payload.reportType);
108+
});
109+
Instabug.setPostInvocationHandler(handler);
134110
},
135111

136112
/**

modules/Surveys.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {
22
NativeModules,
3-
NativeAppEventEmitter,
4-
DeviceEventEmitter,
5-
Platform
63
} from 'react-native';
4+
import IBGEventEmitter from '../utils/IBGEventEmitter';
5+
import InstabugConstants from '../utils/InstabugConstants';
76
let { Instabug } = NativeModules;
87

98
/**
@@ -92,19 +91,7 @@ export default {
9291
* presenting the survey's UI.
9392
*/
9493
setOnShowHandler: function(onShowHandler) {
95-
if (Platform.OS === 'ios') {
96-
Instabug.addListener('IBGWillShowSurvey');
97-
NativeAppEventEmitter.addListener(
98-
'IBGWillShowSurvey',
99-
onShowHandler
100-
);
101-
} else {
102-
DeviceEventEmitter.addListener(
103-
'IBGWillShowSurvey',
104-
onShowHandler
105-
);
106-
}
107-
94+
IBGEventEmitter.addListener(InstabugConstants.WILL_SHOW_SURVEY_HANDLER, onShowHandler);
10895
Instabug.setWillShowSurveyHandler(onShowHandler);
10996
},
11097

@@ -128,19 +115,7 @@ export default {
128115
* the survey's UI is dismissed.
129116
*/
130117
setOnDismissHandler: function(onDismissHandler) {
131-
if (Platform.OS === 'ios') {
132-
Instabug.addListener('IBGDidDismissSurvey');
133-
NativeAppEventEmitter.addListener(
134-
'IBGDidDismissSurvey',
135-
onDismissHandler
136-
);
137-
} else {
138-
DeviceEventEmitter.addListener(
139-
'IBGDidDismissSurvey',
140-
onDismissHandler
141-
);
142-
}
143-
118+
IBGEventEmitter.addListener(InstabugConstants.DID_DISMISS_SURVEY_HANDLER, onDismissHandler);
144119
Instabug.setDidDismissSurveyHandler(onDismissHandler);
145120
},
146121

0 commit comments

Comments
 (0)