From cdfc432921202984904e1a8d52bada9ab98a097f Mon Sep 17 00:00:00 2001 From: Ben Gourley Date: Tue, 29 Jun 2021 16:57:55 +0100 Subject: [PATCH 1/3] feat(plugin-expo-device): Use device id as default user id --- packages/plugin-expo-device/device.js | 10 ++++ .../plugin-expo-device/test/device.test.ts | 54 +++++++++++++++++++ .../features/fixtures/test-app/app/user.js | 8 +++ test/expo/features/user.feature | 9 ++++ 4 files changed, 81 insertions(+) diff --git a/packages/plugin-expo-device/device.js b/packages/plugin-expo-device/device.js index 55eedd01ba..b211b87aad 100644 --- a/packages/plugin-expo-device/device.js +++ b/packages/plugin-expo-device/device.js @@ -45,6 +45,7 @@ module.exports = { client.addOnSession(session => { session.device = { ...session.device, ...device } addDefaultAppType(session) + addDefaultUserId(session) }) client.addOnError(event => { @@ -54,6 +55,7 @@ module.exports = { appOwnership: Constants.appOwnership }) addDefaultAppType(event) + addDefaultUserId(event) }, true) } } @@ -68,3 +70,11 @@ function addDefaultAppType (eventOrSession) { } } } + +function addDefaultUserId (eventOrSession) { + // device id is also used to populate the user id field, if it's not already set + const user = eventOrSession.getUser() + if (!user || !user.id) { + eventOrSession.setUser(eventOrSession.device.id) + } +} diff --git a/packages/plugin-expo-device/test/device.test.ts b/packages/plugin-expo-device/test/device.test.ts index 3dea691b18..85c7da750d 100644 --- a/packages/plugin-expo-device/test/device.test.ts +++ b/packages/plugin-expo-device/test/device.test.ts @@ -15,6 +15,7 @@ describe('plugin: expo device', () => { jest.doMock('expo-constants', () => ({ default: { + installationId: '123', platform: { android: {} }, manifest: { sdkVersion: SDK_VERSION }, expoVersion: EXPO_VERSION, @@ -64,6 +65,8 @@ describe('plugin: expo device', () => { }) expect(r.events[0].metaData.device.isDevice).toBe(true) expect(r.events[0].metaData.device.appOwnership).toBe('standalone') + expect(r.events[0].device.id).toBe('123') + expect(r.events[0].user.id).toBe('123') done() }, sendSession: () => {} @@ -184,6 +187,57 @@ describe('plugin: expo device', () => { c.notify(new Error('device testing')) }) + it('does not overwrite user.id when one is already set', done => { + const REACT_NATIVE_VERSION = '0.57.1' + const SDK_VERSION = '32.3.0' + const EXPO_VERSION = '2.10.4' + const IOS_MODEL = 'iPhone 7 Plus' + const IOS_PLATFORM = 'iPhone1,1' + const IOS_VERSION = '11.2' + + jest.doMock('expo-constants', () => ({ + default: { + installationId: '123', + platform: { ios: { model: IOS_MODEL, platform: IOS_PLATFORM, systemVersion: IOS_VERSION } }, + manifest: { sdkVersion: SDK_VERSION }, + expoVersion: EXPO_VERSION, + isDevice: false, + appOwnership: 'expo' + } + })) + jest.doMock('expo-device', () => ({ + manufacturer: 'Apple' + })) + jest.doMock('react-native', () => ({ + Dimensions: { + addEventListener: function () {}, + get: function () { + return { width: 1024, height: 768 } + } + }, + Platform: { OS: 'ios' } + })) + jest.doMock('react-native/package.json', () => ({ version: REACT_NATIVE_VERSION })) + + const plugin = require('..') + + const c = new Client({ apiKey: 'api_key', plugins: [plugin] }) + c.setUser('345') + c._setDelivery(client => ({ + sendEvent: (payload) => { + const r = JSON.parse(JSON.stringify(payload)) + expect(r).toBeTruthy() + expect(r.events[0].device).toBeTruthy() + expect(r.events[0].device.osName).toBe('ios') + expect(r.events[0].user.id).toBe('345') + done() + }, + sendSession: () => {} + + })) + c.notify(new Error('device testing')) + }) + it('updates orientation when the screen dimensions change', done => { const REACT_NATIVE_VERSION = '0.57.1' const SDK_VERSION = '32.3.0' diff --git a/test/expo/features/fixtures/test-app/app/user.js b/test/expo/features/fixtures/test-app/app/user.js index 8472a0534f..c3db8c3ce2 100644 --- a/test/expo/features/fixtures/test-app/app/user.js +++ b/test/expo/features/fixtures/test-app/app/user.js @@ -14,6 +14,10 @@ export default class User extends Component { }) } + userDefault = () => { + bugsnagClient.notify(new Error('UserDefaultError')) + } + render() { return ( @@ -25,6 +29,10 @@ export default class User extends Component { title="userCallback" onPress={this.userCallback} /> +