Skip to content

Commit

Permalink
update expo content integration
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich committed Mar 27, 2024
1 parent f84031f commit 0c01575
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/js/integrations/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { isExpoGo, notWeb } from '../utils/environment';
import { debugSymbolicatorIntegration } from './debugsymbolicator';
import { deviceContextIntegration } from './devicecontext';
import { eventOriginIntegration } from './eventorigin';
import { ExpoContext } from './expocontext';
import { expoContextIntegration } from './expocontext';
import { modulesLoaderIntegration } from './modulesloader';
import { nativeLinkedErrorsIntegration } from './nativelinkederrors';
import { reactNativeErrorHandlersIntegration } from './reactnativeerrorhandlers';
Expand Down Expand Up @@ -101,7 +101,7 @@ export function getDefaultIntegrations(options: ReactNativeClientOptions): Integ
}

if (isExpoGo()) {
integrations.push(new ExpoContext());
integrations.push(expoContextIntegration());
}

if (options.enableSpotlight) {
Expand Down
55 changes: 21 additions & 34 deletions src/js/integrations/expocontext.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
import type { DeviceContext, Event, EventProcessor, Hub, Integration, OsContext } from '@sentry/types';
import type { DeviceContext, Event, Integration, OsContext } from '@sentry/types';

import { getExpoDevice } from '../utils/expomodules';

/** Load device context from expo modules. */
export class ExpoContext implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'ExpoContext';

/**
* @inheritDoc
*/
public name: string = ExpoContext.id;
export const expoContextIntegration = (): Integration => {
return {
name: 'ExpoContext',
setupOnce: () => {
// noop
},
processEvent,
};
};

/**
* @inheritDoc
* @deprecated
*/
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, _getCurrentHub: () => Hub): void {
// nothing to do here
function processEvent(event: Event): Event {
const expoDeviceContext = getExpoDeviceContext();
if (expoDeviceContext) {
event.contexts = event.contexts || {};
event.contexts.device = { ...expoDeviceContext, ...event.contexts.device };
}

/**
* @inheritDoc
*/
public processEvent(event: Event): Event {
const expoDeviceContext = getExpoDeviceContext();
if (expoDeviceContext) {
event.contexts = event.contexts || {};
event.contexts.device = { ...expoDeviceContext, ...event.contexts.device };
}

const expoOsContext = getExpoOsContext();
if (expoOsContext) {
event.contexts = event.contexts || {};
event.contexts.os = { ...expoOsContext, ...event.contexts.os };
}

return event;
const expoOsContext = getExpoOsContext();
if (expoOsContext) {
event.contexts = event.contexts || {};
event.contexts.os = { ...expoOsContext, ...event.contexts.os };
}

return event;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/js/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export { modulesLoaderIntegration } from './modulesloader';
export { HermesProfiling } from '../profiling/integration';
export { screenshotIntegration } from './screenshot';
export { viewHierarchyIntegration } from './viewhierarchy';
export { ExpoContext } from './expocontext';
export { expoContextIntegration } from './expocontext';
export { Spotlight } from './spotlight';
12 changes: 3 additions & 9 deletions test/integrations/expocontext.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type { Event } from '@sentry/types';
import type { Client, Event } from '@sentry/types';

import { ExpoContext } from '../../src/js/integrations/expocontext';
import { expoContextIntegration } from '../../src/js/integrations/expocontext';
import { getExpoDevice } from '../../src/js/utils/expomodules';

jest.mock('../../src/js/utils/expomodules');

describe('Expo Context Integration', () => {
let integration: ExpoContext;

beforeEach(() => {
integration = new ExpoContext();
});

it('does not add device context because expo device module is not available', async () => {
(getExpoDevice as jest.Mock).mockReturnValue(undefined);
const actualEvent = await executeIntegrationFor({});
Expand Down Expand Up @@ -107,6 +101,6 @@ describe('Expo Context Integration', () => {
});

function executeIntegrationFor(mockedEvent: Event): Event {
return integration.processEvent(mockedEvent);
return expoContextIntegration().processEvent!(mockedEvent, {}, {} as Client) as Event;
}
});

0 comments on commit 0c01575

Please sign in to comment.