Skip to content

Commit 1564bd2

Browse files
fixed js tests
1 parent f187c8b commit 1564bd2

File tree

4 files changed

+141
-131
lines changed

4 files changed

+141
-131
lines changed

InstabugSample/__tests__/instabugUtils.spec.js

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,74 @@
33
* @lint-ignore-every XPLATJSCOPYRIGHT1
44
*/
55

6-
import { NativeModules, Platform } from 'react-native';
6+
import {NativeModules, Platform} from 'react-native';
77
import '../../jest/mockInstabug';
88
import Instabug from 'instabug-reactnative';
99
import IBGEventEmitter from 'instabug-reactnative/utils/IBGEventEmitter';
1010
import IBGConstants from 'instabug-reactnative/utils/InstabugConstants';
1111
// import console = require('console');
1212

13-
jest.mock('../node_modules/instabug-reactnative/utils/XhrNetworkInterceptor', () => {
13+
jest.mock(
14+
'../node_modules/instabug-reactnative/utils/XhrNetworkInterceptor',
15+
() => {
1416
return {
15-
enableInterception: jest.fn(),
16-
setOnDoneCallback: jest.fn()
17-
}
18-
})
17+
enableInterception: jest.fn(),
18+
setOnDoneCallback: jest.fn(),
19+
};
20+
},
21+
);
1922

2023
describe('Test global error handler', () => {
21-
22-
const sendJSCrash = jest.spyOn(NativeModules.Instabug, 'sendJSCrash');
23-
24-
beforeEach(() => {
25-
// var InstabugUtils = require('../utils/InstabugUtils');
26-
});
27-
28-
it('should call sendJSCrash when platform is ios', () => {
29-
30-
Platform.OS = 'ios';
31-
const handler = global.ErrorUtils.getGlobalHandler();
32-
handler({ name: 'TypeError', message: 'This is a type error.' }, false);
33-
const expected = {
34-
message: 'TypeError - This is a type error.',
35-
os: 'ios',
36-
platform: 'react_native',
37-
exception: []
38-
}
39-
expect(sendJSCrash).toHaveBeenCalledWith(expected);
40-
41-
});
42-
43-
it('should call sendJSCrash when platform is android', () => {
44-
45-
Platform.OS = 'android';
46-
const handler = global.ErrorUtils.getGlobalHandler();
47-
handler({ name: 'TypeError', message: 'This is a type error.' }, false);
48-
const expected = {
49-
message: 'TypeError - This is a type error.',
50-
os: 'android',
51-
platform: 'react_native',
52-
exception: []
53-
}
54-
expect(sendJSCrash).toHaveBeenCalledWith(JSON.stringify(expected));
55-
56-
});
57-
58-
// it('should emit event IBGSendUnhandledJSCrash when platform is android and onReportSubmitHandler is set', (done) => {
59-
60-
// Platform.OS = 'android';
61-
// Instabug._isOnReportHandlerSet = jest.fn(() => true);
62-
// const handler = global.ErrorUtils.getGlobalHandler();
63-
// IBGEventEmitter.addListener(Instabug, IBGConstants.SEND_UNHANDLED_CRASH, (actual) => {
64-
// const expected = {
65-
// message: 'TypeError - This is a type error.',
66-
// os: 'android',
67-
// platform: 'react_native',
68-
// exception: []
69-
// };
70-
// expect(actual).toEqual(expected);
71-
// done();
72-
// });
73-
// handler({ name: 'TypeError', message: 'This is a type error.' }, false);
74-
75-
76-
// });
77-
78-
24+
const sendJSCrash = jest.spyOn(NativeModules.Instabug, 'sendJSCrash');
25+
beforeEach(() => {
26+
// var InstabugUtils = require('../utils/InstabugUtils');
27+
});
28+
29+
it('should call sendJSCrash when platform is ios', () => {
30+
Platform.OS = 'ios';
31+
Platform.constants['reactNativeVersion'] = {minor: 64};
32+
var handler = global.ErrorUtils.getGlobalHandler();
33+
handler({name: 'TypeError', message: 'This is a type error.'}, false);
34+
const expected = {
35+
message: 'TypeError - This is a type error.',
36+
os: 'ios',
37+
platform: 'react_native',
38+
exception: [],
39+
};
40+
41+
expect(sendJSCrash).toHaveBeenCalledWith(expected);
42+
});
43+
44+
it('should call sendJSCrash when platform is android', () => {
45+
Platform.OS = 'android';
46+
Platform.constants['reactNativeVersion'] = {minor: 64};
47+
const handler = global.ErrorUtils.getGlobalHandler();
48+
handler({name: 'TypeError', message: 'This is a type error.'}, false);
49+
const expected = {
50+
message: 'TypeError - This is a type error.',
51+
os: 'android',
52+
platform: 'react_native',
53+
exception: [],
54+
};
55+
expect(sendJSCrash).toHaveBeenCalledWith(JSON.stringify(expected));
56+
});
57+
58+
// it('should emit event IBGSendUnhandledJSCrash when platform is android and onReportSubmitHandler is set', (done) => {
59+
60+
// Platform.OS = 'android';
61+
// Instabug._isOnReportHandlerSet = jest.fn(() => true);
62+
// const handler = global.ErrorUtils.getGlobalHandler();
63+
// IBGEventEmitter.addListener(Instabug, IBGConstants.SEND_UNHANDLED_CRASH, (actual) => {
64+
// const expected = {
65+
// message: 'TypeError - This is a type error.',
66+
// os: 'android',
67+
// platform: 'react_native',
68+
// exception: []
69+
// };
70+
// expect(actual).toEqual(expected);
71+
// done();
72+
// });
73+
// handler({ name: 'TypeError', message: 'This is a type error.' }, false);
74+
75+
// });
7976
});

InstabugSample/jest.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
preset: 'react-native',
3+
moduleNameMapper: {
4+
'^react-native$': require.resolve('react-native'),
5+
},
6+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
7+
transformIgnorePatterns: [
8+
'node_modules/(?!(react-native' +
9+
'|@react-native' +
10+
'|react-navigation-tabs' +
11+
'|react-native-splash-screen' +
12+
'|react-native-screens' +
13+
'|react-native-reanimated' +
14+
'|instabug-reactnative' +
15+
')/)',
16+
],
17+
};

InstabugSample/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@
2525
"metro-react-native-babel-preset": "^0.66.2",
2626
"react-test-renderer": "17.0.2"
2727
},
28-
"jest": {
29-
"preset": "react-native",
30-
"transformIgnorePatterns": [
31-
"node_modules/(?!react-native|react-navigation)/"
32-
]
33-
},
28+
3429
"detox": {
3530
"test-runner": "jest",
3631
"runner-config": "__e2e__/config.json",

jest/mockInstabug.js

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,62 @@
1-
jest.mock('NativeModules', () => {
2-
return {
3-
Instabug: {
4-
startWithToken: jest.fn(),
5-
setUserData: jest.fn(),
6-
setTrackUserSteps: jest.fn(),
7-
setIBGLogPrintsToConsole: jest.fn(),
8-
didSelectPromptOptionHandler: jest.fn(),
9-
setSessionProfilerEnabled: jest.fn(),
10-
setPushNotificationsEnabled: jest.fn(),
11-
setLocale: jest.fn(),
12-
setColorTheme: jest.fn(),
13-
setPrimaryColor: jest.fn(),
14-
appendTags: jest.fn(),
15-
resetTags: jest.fn(),
16-
getTags: jest.fn(cb => cb(['tags1', 'tags2'])),
17-
setString: jest.fn(),
18-
identifyUserWithEmail: jest.fn(),
19-
logOut: jest.fn(),
20-
logUserEventWithName: jest.fn(),
21-
log: jest.fn(),
22-
logVerbose: jest.fn(),
23-
logInfo: jest.fn(),
24-
logWarn: jest.fn(),
25-
logError: jest.fn(),
26-
logDebug: jest.fn(),
27-
clearLogs: jest.fn(),
28-
setReproStepsMode: jest.fn(),
29-
setSdkDebugLogsLevel: jest.fn(),
30-
setUserAttribute: jest.fn(),
31-
getUserAttribute: jest.fn((_, cb) => cb('24')),
32-
removeUserAttribute: jest.fn(),
33-
getAllUserAttributes: jest.fn(cb => cb([{ age: '24' }])),
34-
clearAllUserAttributes: jest.fn(),
35-
setDebugEnabled: jest.fn(),
36-
enable: jest.fn(),
37-
disable: jest.fn(),
38-
isRunningLive: jest.fn(cb => cb(true)),
39-
setVideoRecordingFloatingButtonPosition: jest.fn(),
40-
showWelcomeMessageWithMode: jest.fn(),
41-
setWelcomeMessageMode: jest.fn(),
42-
setFileAttachment: jest.fn(),
43-
hideView: jest.fn(),
44-
show: jest.fn(),
45-
setPreSendingHandler: jest.fn(),
46-
callPrivateApi: jest.fn(),
47-
addListener: jest.fn(),
48-
getReport: jest.fn(),
49-
sendHandledJSCrash: jest.fn(),
50-
sendJSCrash: jest.fn(),
51-
reportScreenChange: jest.fn(),
52-
},
53-
IBGBugReporting: {
54-
setFloatingButtonEdge: jest.fn(),
55-
setEnabledAttachmentTypes: jest.fn(),
56-
},
57-
IBGReplies: {
58-
setPushNotificationsEnabled: jest.fn(),
59-
},
60-
};
61-
});
1+
jest.mock("react-native", () => {
2+
const RN = jest.requireActual("react-native"); // use original implementation, which comes with mocks out of the box
3+
RN.NativeModules.Instabug = {
4+
startWithToken: jest.fn(),
5+
setUserData: jest.fn(),
6+
setTrackUserSteps: jest.fn(),
7+
setIBGLogPrintsToConsole: jest.fn(),
8+
didSelectPromptOptionHandler: jest.fn(),
9+
setSessionProfilerEnabled: jest.fn(),
10+
setPushNotificationsEnabled: jest.fn(),
11+
setLocale: jest.fn(),
12+
setColorTheme: jest.fn(),
13+
setPrimaryColor: jest.fn(),
14+
appendTags: jest.fn(),
15+
resetTags: jest.fn(),
16+
getTags: jest.fn((cb) => cb(["tags1", "tags2"])),
17+
setString: jest.fn(),
18+
identifyUserWithEmail: jest.fn(),
19+
logOut: jest.fn(),
20+
logUserEventWithName: jest.fn(),
21+
log: jest.fn(),
22+
logVerbose: jest.fn(),
23+
logInfo: jest.fn(),
24+
logWarn: jest.fn(),
25+
logError: jest.fn(),
26+
logDebug: jest.fn(),
27+
clearLogs: jest.fn(),
28+
setReproStepsMode: jest.fn(),
29+
setSdkDebugLogsLevel: jest.fn(),
30+
setUserAttribute: jest.fn(),
31+
getUserAttribute: jest.fn((_, cb) => cb("24")),
32+
removeUserAttribute: jest.fn(),
33+
getAllUserAttributes: jest.fn((cb) => cb([{ age: "24" }])),
34+
clearAllUserAttributes: jest.fn(),
35+
setDebugEnabled: jest.fn(),
36+
enable: jest.fn(),
37+
disable: jest.fn(),
38+
isRunningLive: jest.fn((cb) => cb(true)),
39+
setVideoRecordingFloatingButtonPosition: jest.fn(),
40+
showWelcomeMessageWithMode: jest.fn(),
41+
setWelcomeMessageMode: jest.fn(),
42+
setFileAttachment: jest.fn(),
43+
hideView: jest.fn(),
44+
show: jest.fn(),
45+
setPreSendingHandler: jest.fn(),
46+
callPrivateApi: jest.fn(),
47+
addListener: jest.fn(),
48+
getReport: jest.fn(),
49+
sendHandledJSCrash: jest.fn(),
50+
sendJSCrash: jest.fn(),
51+
reportScreenChange: jest.fn(),
52+
};
53+
RN.NativeModules.IBGBugReporting = {
54+
setFloatingButtonEdge: jest.fn(),
55+
setEnabledAttachmentTypes: jest.fn(),
56+
};
57+
RN.NativeModules.IBGReplies = {
58+
setPushNotificationsEnabled: jest.fn(),
59+
};
60+
61+
return RN;
62+
});

0 commit comments

Comments
 (0)