Skip to content

Commit

Permalink
new core data was added
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Lopez committed Apr 4, 2024
1 parent 9b6bab3 commit 16b68f2
Show file tree
Hide file tree
Showing 8 changed files with 568 additions and 1,992 deletions.
1 change: 1 addition & 0 deletions __mocks__/@react-native-community/netinfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from '@react-native-community/netinfo/jest/netinfo-mock';
2 changes: 2 additions & 0 deletions lib/constant/coreData.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export default [
'client',
'language',
'currency',
'deviceId',
'connection',
];
12 changes: 11 additions & 1 deletion lib/crashlytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
formatUserData,
} from './utils';
import {validateCoreData} from './utils/helpers';
import {getNetworkState, getUniqueId} from '@janiscommerce/app-device-info';

const setLogAttributes = async (attributes, key) => {
try {
Expand Down Expand Up @@ -43,11 +44,20 @@ class Crashlytics {
*/
async initialize() {
try {
const {networkType} = await getNetworkState();

this.userData.connection = networkType;

/* istanbul ignore next */
if (validateCoreData(this.userData)) return null;

const userInfo = await getUserInfo();
this.userData = formatUserData(userInfo);

this.userData = {
...this.userData,
...formatUserData(userInfo),
deviceId: getUniqueId(),
};
} catch (error) {
/* istanbul ignore next */
if (isDevEnv()) {
Expand Down
2,487 changes: 507 additions & 1,980 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,40 @@
"author": "Janis",
"license": "ISC",
"dependencies": {
"@janiscommerce/app-device-info": "^1.1.0",
"@react-native-firebase/app": "^18.3.0",
"@react-native-firebase/crashlytics": "^18.3.2"
"@react-native-firebase/crashlytics": "^18.3.2",
"react-native-device-info": "^10.12.0"
},
"peerDependencies": {
"@janiscommerce/oauth-native": "^1.3.1",
"react": ">=17.0.2 <=18.2.0",
"react-native": ">=0.67.5 <=0.72.0",
"@janiscommerce/oauth-native": "^1.3.1"
"react-native": ">=0.67.5 <=0.72.0"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/eslint-parser": "^7.23.3",
"@babel/eslint-plugin": "^7.23.5",
"@babel/eslint-parser": "^7.23.3",
"@babel/eslint-plugin": "^7.23.5",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/preset-env": "^7.22.10",
"@babel/runtime": "^7.12.5",
"@janiscommerce/oauth-native": "^1.3.1",
"@react-native-community/netinfo": "^11.3.1",
"@react-native-firebase/app": "^18.3.0",
"@react-native-firebase/crashlytics": "^18.3.2",
"@janiscommerce/oauth-native": "^1.3.1",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.3",
"babel-loader": "8.2.4",
"eslint": "^8.56.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^8.0.0",
"jest": "^29.7.0",
"jest": "^29.7.0",
"jsdoc-babel": "^0.5.0",
"jsdoc-to-markdown": "^8.0.0",
"lint-staged": "^10.5.4",
Expand Down
6 changes: 6 additions & 0 deletions setupTest/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ jest.mock('@react-native-firebase/crashlytics', () =>
jest.mock('@janiscommerce/oauth-native', () => ({
getUserInfo: jest.fn(),
}));

jest.mock('@janiscommerce/app-device-info', () => ({
__esModule: true,
getNetworkState: jest.fn(),
getUniqueId: jest.fn(),
}));
25 changes: 25 additions & 0 deletions test/crashlytics.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Crashlytics from '../lib/crashlytics';
import * as deviceInfo from '@janiscommerce/app-device-info';
import * as isDevEnv from '../lib/utils/isDevEnv';
import * as setError from '../lib/utils/setError';
import * as setLogAttibute from '../lib/utils/setAttribute';
import * as setLogAttibutes from '../lib/utils/setAttributes';

describe('Crashlytics class', () => {
const spyGetNetworkState = jest.spyOn(deviceInfo, 'getNetworkState');
describe('log function', () => {
const mockedDevEnv = jest.spyOn(isDevEnv, 'default');
const mockSetAttribute = jest.spyOn(setLogAttibute, 'default');
Expand All @@ -17,20 +19,25 @@ describe('Crashlytics class', () => {
});

it('message is required and isEnvDev is false', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});

await crash.log(undefined);
});

it('message is required and isEnvDev is true', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(true);
await crash.log();
});

it('when setAttribute returns an error', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockSetAttribute.mockRejectedValueOnce({message: 'error'});
await crash.log('error', {name: 'peter'});
});

it('when setAttributes returns an error', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockSetAttributes.mockRejectedValueOnce({message: 'error'});
await crash.log('error', {name: 'peter'});
});
Expand All @@ -43,22 +50,27 @@ describe('Crashlytics class', () => {
});

it('when has message', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log(15);
});

it('when has message and attribute', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {name: 'peter'});
});

it('when has message and attributes', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {info: {name: 'peter'}});
});

it('when has message and AttributesCrash', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {userId: '13231'});
});

it('when has error instance', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {
userId: '13231',
error: new Error('Error'),
Expand All @@ -72,22 +84,27 @@ describe('Crashlytics class', () => {
});

it('when has message', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log(15);
});

it('when has message and attribute', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {name: 'peter'});
});

it('when has message and attributes', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {info: {name: 'peter'}});
});

it('when has message and AttributesCrash', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {userId: '13231'});
});

it('when has error instance', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
await crash.log('error', {
userId: '13231',
error: new Error('Error'),
Expand All @@ -99,10 +116,12 @@ describe('Crashlytics class', () => {

describe('crash function', () => {
beforeEach(() => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
crash = new Crashlytics();
});

it('it works correctly', () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
crash.crashThisApp();
});
});
Expand All @@ -115,33 +134,39 @@ describe('Crashlytics class', () => {

describe('throws an error when', () => {
it('crashlytics is not enabled', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(true);
await crash.recordError(undefined, 'Error name');
});

it('error is not correct', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(false);
await crash.recordError(undefined, 'Error name');
});

it('jsErrorName is not string', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(false);
await crash.recordError(new Error('error'), 15);
});

it('attributes is not object', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(false);
await crash.recordError(new Error('error'), 'Error name', 15);
});
});

describe('it works correctly', () => {
it(' but no error message', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(false);
await crash.recordError(new Error('error'));
});

it(' but has error message', async () => {
spyGetNetworkState.mockResolvedValue({networkType: 'wifi'});
mockedDevEnv.mockReturnValueOnce(false);
await crash.recordError(new Error('error'), 'Error name');
});
Expand Down
2 changes: 2 additions & 0 deletions test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ describe('helpers', () => {
client: 'janis',
language: 'lang',
currency: 'currency',
deviceId: '12345',
connection: 'wifi',
};
it('returns true when the data contain all required coreData properties', () => {
expect(validateCoreData(validObject)).toBe(true);
Expand Down

0 comments on commit 16b68f2

Please sign in to comment.