diff --git a/CHANGELOG.md b/CHANGELOG.md index 84bb5cef16..4d05f0da0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ - Tracks update checks, downloads, errors, rollbacks, and restarts as `expo.updates` breadcrumbs - Enabled by default in Expo apps (requires `expo-updates` to be installed) - feat(android): Expose `enableAnrFingerprinting` option ([#5838](https://github.com/getsentry/sentry-react-native/issues/5838)) +- Show feedback widget on device shake ([#5754](https://github.com/getsentry/sentry-react-native/pull/5754)) + - Use `Sentry.enableFeedbackOnShake()` / `Sentry.disableFeedbackOnShake()` or set `feedbackIntegration({ enableShakeToReport: true })` ### Fixes diff --git a/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java b/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java index b239852cbf..024f0d0ed1 100644 --- a/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java +++ b/packages/core/android/src/main/java/io/sentry/react/RNSentryModuleImpl.java @@ -45,6 +45,7 @@ import io.sentry.android.core.InternalSentrySdk; import io.sentry.android.core.SentryAndroidDateProvider; import io.sentry.android.core.SentryAndroidOptions; +import io.sentry.android.core.SentryShakeDetector; import io.sentry.android.core.ViewHierarchyEventProcessor; import io.sentry.android.core.internal.debugmeta.AssetsDebugMetaLoader; import io.sentry.android.core.internal.util.SentryFrameMetricsCollector; @@ -122,6 +123,9 @@ public class RNSentryModuleImpl { private final @NotNull Runnable emitNewFrameEvent; + private static final String ON_SHAKE_EVENT = "rn_sentry_on_shake"; + private @Nullable SentryShakeDetector shakeDetector; + /** Max trace file size in bytes. */ private long maxTraceFileSize = 5 * 1024 * 1024; @@ -208,10 +212,58 @@ public void addListener(String eventType) { } public void removeListeners(double id) { - // Is must be defined otherwise the generated interface from TS won't be - // fulfilled - logger.log( - SentryLevel.ERROR, "removeListeners of NativeEventEmitter can't be used on Android!"); + // removeListeners does not carry event-type information, so it cannot be used + // to track shake listeners selectively. Shake detection is managed exclusively + // via enableShakeDetection / disableShakeDetection. + } + + private void startShakeDetection() { + if (shakeDetector != null) { + return; + } + + try { // NOPMD - We don't want to crash in any case + final ReactApplicationContext context = getReactApplicationContext(); + shakeDetector = new SentryShakeDetector(logger); + shakeDetector.start( + context, + () -> { + try { // NOPMD - We don't want to crash in any case + final ReactApplicationContext ctx = getReactApplicationContext(); + if (ctx.hasActiveReactInstance()) { + ctx.getJSModule( + com.facebook.react.modules.core.DeviceEventManagerModule + .RCTDeviceEventEmitter.class) + .emit(ON_SHAKE_EVENT, null); + } + } catch (Throwable e) { // NOPMD - We don't want to crash in any case + logger.log(SentryLevel.WARNING, "Failed to emit shake event.", e); + } + }); + } catch (Throwable e) { // NOPMD - We don't want to crash in any case + logger.log(SentryLevel.WARNING, "Failed to start shake detection.", e); + shakeDetector = null; + } + } + + private void stopShakeDetection() { + try { // NOPMD - We don't want to crash in any case + if (shakeDetector != null) { + shakeDetector.stop(); + shakeDetector = null; + } + } catch (Throwable e) { // NOPMD - We don't want to crash in any case + logger.log(SentryLevel.WARNING, "Failed to stop shake detection.", e); + shakeDetector = null; + } + } + + public void enableShakeDetection() { + startShakeDetection(); + } + + public void disableShakeDetection() { + stopShakeDetection(); } public void fetchModules(Promise promise) { diff --git a/packages/core/android/src/newarch/java/io/sentry/react/RNSentryModule.java b/packages/core/android/src/newarch/java/io/sentry/react/RNSentryModule.java index b928d2d9c4..fe2a341844 100644 --- a/packages/core/android/src/newarch/java/io/sentry/react/RNSentryModule.java +++ b/packages/core/android/src/newarch/java/io/sentry/react/RNSentryModule.java @@ -212,4 +212,14 @@ public void popTimeToDisplayFor(String key, Promise promise) { public boolean setActiveSpanId(String spanId) { return this.impl.setActiveSpanId(spanId); } + + @Override + public void enableShakeDetection() { + this.impl.enableShakeDetection(); + } + + @Override + public void disableShakeDetection() { + this.impl.disableShakeDetection(); + } } diff --git a/packages/core/android/src/oldarch/java/io/sentry/react/RNSentryModule.java b/packages/core/android/src/oldarch/java/io/sentry/react/RNSentryModule.java index 0488e143c9..499ef37f39 100644 --- a/packages/core/android/src/oldarch/java/io/sentry/react/RNSentryModule.java +++ b/packages/core/android/src/oldarch/java/io/sentry/react/RNSentryModule.java @@ -212,4 +212,14 @@ public void popTimeToDisplayFor(String key, Promise promise) { public boolean setActiveSpanId(String spanId) { return this.impl.setActiveSpanId(spanId); } + + @ReactMethod + public void enableShakeDetection() { + this.impl.enableShakeDetection(); + } + + @ReactMethod + public void disableShakeDetection() { + this.impl.disableShakeDetection(); + } } diff --git a/packages/core/ios/RNSentry.mm b/packages/core/ios/RNSentry.mm index 9260876452..6cb2c522fb 100644 --- a/packages/core/ios/RNSentry.mm +++ b/packages/core/ios/RNSentry.mm @@ -60,6 +60,7 @@ @implementation RNSentry { bool hasListeners; + bool _shakeDetectionEnabled; RNSentryTimeToDisplay *_timeToDisplay; NSArray *_ignoreErrorPatternsStr; NSArray *_ignoreErrorPatternsRegex; @@ -295,9 +296,54 @@ - (void)stopObserving [[RNSentryNativeLogsForwarder shared] stopForwarding]; } +- (void)handleShakeDetected +{ + if (_shakeDetectionEnabled) { + [self sendEventWithName:RNSentryOnShakeEvent body:@{}]; + } +} + +// SentryShakeDetector is a Swift class; its notification name and methods are accessed +// via the raw string / NSClassFromString to avoid requiring @import Sentry in this .mm file. +static NSNotificationName const RNSentryShakeNotification = @"SentryShakeDetected"; + +RCT_EXPORT_METHOD(enableShakeDetection) +{ + [[NSNotificationCenter defaultCenter] removeObserver:self + name:RNSentryShakeNotification + object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handleShakeDetected) + name:RNSentryShakeNotification + object:nil]; + Class shakeDetector = NSClassFromString(@"SentryShakeDetector"); + if (shakeDetector) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + [shakeDetector performSelector:@selector(enable)]; +#pragma clang diagnostic pop + } + _shakeDetectionEnabled = YES; +} + +RCT_EXPORT_METHOD(disableShakeDetection) +{ + _shakeDetectionEnabled = NO; + Class shakeDetector = NSClassFromString(@"SentryShakeDetector"); + if (shakeDetector) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + [shakeDetector performSelector:@selector(disable)]; +#pragma clang diagnostic pop + } + [[NSNotificationCenter defaultCenter] removeObserver:self + name:RNSentryShakeNotification + object:nil]; +} + - (NSArray *)supportedEvents { - return @[ RNSentryNewFrameEvent, RNSentryNativeLogEvent ]; + return @[ RNSentryNewFrameEvent, RNSentryNativeLogEvent, RNSentryOnShakeEvent ]; } RCT_EXPORT_METHOD( diff --git a/packages/core/ios/RNSentryEvents.h b/packages/core/ios/RNSentryEvents.h index 6f1a5f0540..13884dc4eb 100644 --- a/packages/core/ios/RNSentryEvents.h +++ b/packages/core/ios/RNSentryEvents.h @@ -1,4 +1,5 @@ #import extern NSString *const RNSentryNewFrameEvent; +extern NSString *const RNSentryOnShakeEvent; extern NSString *const RNSentryNativeLogEvent; diff --git a/packages/core/ios/RNSentryEvents.m b/packages/core/ios/RNSentryEvents.m index bb3e842d73..c4bf3ab350 100644 --- a/packages/core/ios/RNSentryEvents.m +++ b/packages/core/ios/RNSentryEvents.m @@ -1,4 +1,5 @@ #import "RNSentryEvents.h" NSString *const RNSentryNewFrameEvent = @"rn_sentry_new_frame"; +NSString *const RNSentryOnShakeEvent = @"rn_sentry_on_shake"; NSString *const RNSentryNativeLogEvent = @"SentryNativeLog"; diff --git a/packages/core/src/js/NativeRNSentry.ts b/packages/core/src/js/NativeRNSentry.ts index afd8fba03d..ee753e71f0 100644 --- a/packages/core/src/js/NativeRNSentry.ts +++ b/packages/core/src/js/NativeRNSentry.ts @@ -54,6 +54,8 @@ export interface Spec extends TurboModule { popTimeToDisplayFor(key: string): Promise; setActiveSpanId(spanId: string): boolean; encodeToBase64(data: number[]): Promise; + enableShakeDetection(): void; + disableShakeDetection(): void; } export type NativeStackFrame = { diff --git a/packages/core/src/js/feedback/FeedbackWidgetManager.tsx b/packages/core/src/js/feedback/FeedbackWidgetManager.tsx index 505bf5e6da..ba3cef2601 100644 --- a/packages/core/src/js/feedback/FeedbackWidgetManager.tsx +++ b/packages/core/src/js/feedback/FeedbackWidgetManager.tsx @@ -1,6 +1,7 @@ import { debug } from '@sentry/core'; import { isWeb } from '../utils/environment'; import { lazyLoadAutoInjectFeedbackButtonIntegration,lazyLoadAutoInjectFeedbackIntegration, lazyLoadAutoInjectScreenshotButtonIntegration } from './lazy'; +import { startShakeListener, stopShakeListener } from './ShakeToReportBug'; export const PULL_DOWN_CLOSE_THRESHOLD = 200; export const SLIDE_ANIMATION_DURATION = 200; @@ -132,4 +133,20 @@ const resetScreenshotButtonManager = (): void => { ScreenshotButtonManager.reset(); }; -export { showFeedbackButton, hideFeedbackButton, showFeedbackWidget, showScreenshotButton, hideScreenshotButton, resetFeedbackButtonManager, resetFeedbackWidgetManager, resetScreenshotButtonManager }; +let _imperativeShakeListenerStarted = false; + +const enableFeedbackOnShake = (): void => { + lazyLoadAutoInjectFeedbackIntegration(); + if (!_imperativeShakeListenerStarted) { + _imperativeShakeListenerStarted = startShakeListener(showFeedbackWidget); + } +}; + +const disableFeedbackOnShake = (): void => { + if (_imperativeShakeListenerStarted) { + stopShakeListener(); + _imperativeShakeListenerStarted = false; + } +}; + +export { showFeedbackButton, hideFeedbackButton, showFeedbackWidget, enableFeedbackOnShake, disableFeedbackOnShake, showScreenshotButton, hideScreenshotButton, resetFeedbackButtonManager, resetFeedbackWidgetManager, resetScreenshotButtonManager }; diff --git a/packages/core/src/js/feedback/FeedbackWidgetProvider.tsx b/packages/core/src/js/feedback/FeedbackWidgetProvider.tsx index 426affd998..d71c13411d 100644 --- a/packages/core/src/js/feedback/FeedbackWidgetProvider.tsx +++ b/packages/core/src/js/feedback/FeedbackWidgetProvider.tsx @@ -13,10 +13,12 @@ import { FeedbackWidgetManager, PULL_DOWN_CLOSE_THRESHOLD, ScreenshotButtonManager, + showFeedbackWidget, SLIDE_ANIMATION_DURATION, } from './FeedbackWidgetManager'; -import { getFeedbackButtonOptions, getFeedbackOptions, getScreenshotButtonOptions } from './integration'; +import { getFeedbackButtonOptions, getFeedbackOptions, getScreenshotButtonOptions, isShakeToReportEnabled } from './integration'; import { ScreenshotButton } from './ScreenshotButton'; +import { startShakeListener, stopShakeListener } from './ShakeToReportBug'; import { isModalSupported, isNativeDriverSupportedForColorAnimations } from './utils'; const useNativeDriverForColorAnimations = isNativeDriverSupportedForColorAnimations(); @@ -51,6 +53,7 @@ export class FeedbackWidgetProvider extends React.Component { @@ -92,21 +95,29 @@ export class FeedbackWidgetProvider extends React.Component { this.forceUpdate(); }); + + if (isShakeToReportEnabled()) { + this._startedShakeListener = startShakeListener(showFeedbackWidget); + } } /** - * Clean up the theme listener. + * Clean up the theme listener and stop shake detection. */ public componentWillUnmount(): void { if (this._themeListener) { this._themeListener.remove(); } + + if (this._startedShakeListener) { + stopShakeListener(); + } } /** diff --git a/packages/core/src/js/feedback/ShakeToReportBug.ts b/packages/core/src/js/feedback/ShakeToReportBug.ts new file mode 100644 index 0000000000..d58fba9da0 --- /dev/null +++ b/packages/core/src/js/feedback/ShakeToReportBug.ts @@ -0,0 +1,98 @@ +import { debug } from '@sentry/core'; +import type { EmitterSubscription, NativeModule } from 'react-native'; +import { NativeEventEmitter } from 'react-native'; +import { isWeb } from '../utils/environment'; +import { getRNSentryModule } from '../wrapper'; + +export const OnShakeEventName = 'rn_sentry_on_shake'; + +let _shakeSubscription: EmitterSubscription | null = null; + +/** + * Creates a NativeEventEmitter for the given module. + * Can be overridden in tests via the `createEmitter` parameter. + */ +type EmitterFactory = (nativeModule: NativeModule) => NativeEventEmitter; + +const defaultEmitterFactory: EmitterFactory = nativeModule => new NativeEventEmitter(nativeModule); + +/** + * Starts listening for device shake events and invokes the provided callback when a shake is detected. + * + * This starts native shake detection: + * - iOS: Uses UIKit's motion event detection (no permissions required) + * - Android: Uses the accelerometer sensor (no permissions required) + */ +export function startShakeListener( + onShake: () => void, + createEmitter: EmitterFactory = defaultEmitterFactory, +): boolean { + if (_shakeSubscription) { + return false; + } + + if (isWeb()) { + debug.warn('Shake detection is not supported on Web.'); + return false; + } + + const nativeModule = getRNSentryModule() as NativeModule | undefined; + if (!nativeModule) { + debug.warn('Native module is not available. Shake detection will not work.'); + return false; + } + + try { + const emitter = createEmitter(nativeModule); + _shakeSubscription = emitter.addListener(OnShakeEventName, () => { + onShake(); + }); + + // Explicitly enable native shake detection. On iOS with New Architecture (TurboModules), + // NativeEventEmitter.addListener does not dispatch to native addListener:, so the + // native shake listener would never start without this explicit call. + const module = nativeModule as { enableShakeDetection?: () => void }; + if (module.enableShakeDetection) { + module.enableShakeDetection(); + } else { + debug.warn('enableShakeDetection is not available on the native module.'); + } + return true; + } catch (e) { + debug.warn('Failed to start shake listener:', e); + if (_shakeSubscription) { + _shakeSubscription.remove(); + _shakeSubscription = null; + } + return false; + } +} + +/** + * Stops listening for device shake events. + */ +export function stopShakeListener(): void { + if (_shakeSubscription) { + try { + _shakeSubscription.remove(); + } catch (e) { + debug.warn('Failed to remove shake subscription:', e); + } + _shakeSubscription = null; + + try { + const nativeModule = getRNSentryModule() as { disableShakeDetection?: () => void } | undefined; + nativeModule?.disableShakeDetection?.(); + } catch (e) { + debug.warn('Failed to disable native shake detection:', e); + } + } +} + +/** + * Returns whether the shake listener is currently active. + * Exported for testing purposes. + */ +export function isShakeListenerActive(): boolean { + return _shakeSubscription !== null; +} diff --git a/packages/core/src/js/feedback/integration.ts b/packages/core/src/js/feedback/integration.ts index 895568f57d..ace02554e2 100644 --- a/packages/core/src/js/feedback/integration.ts +++ b/packages/core/src/js/feedback/integration.ts @@ -11,6 +11,7 @@ type FeedbackIntegration = Integration & { colorScheme?: 'system' | 'light' | 'dark'; themeLight: Partial; themeDark: Partial; + enableShakeToReport: boolean; }; export const feedbackIntegration = ( @@ -20,6 +21,15 @@ export const feedbackIntegration = ( colorScheme?: 'system' | 'light' | 'dark'; themeLight?: Partial; themeDark?: Partial; + /** + * Enable showing the feedback widget when the user shakes the device. + * + * - iOS: Uses UIKit's motion event detection (no permissions required) + * - Android: Uses the accelerometer sensor (no permissions required) + * + * @default false + */ + enableShakeToReport?: boolean; } = {}, ): FeedbackIntegration => { const { @@ -28,6 +38,7 @@ export const feedbackIntegration = ( colorScheme, themeLight: lightTheme, themeDark: darkTheme, + enableShakeToReport: shakeToReport, ...widgetOptions } = initOptions; @@ -39,6 +50,7 @@ export const feedbackIntegration = ( colorScheme: colorScheme || 'system', themeLight: lightTheme || {}, themeDark: darkTheme || {}, + enableShakeToReport: shakeToReport || false, }; }; @@ -99,3 +111,8 @@ export const getFeedbackDarkTheme = (): Partial => { return integration.themeDark; }; + +export const isShakeToReportEnabled = (): boolean => { + const integration = _getClientIntegration(); + return integration?.enableShakeToReport ?? false; +}; diff --git a/packages/core/src/js/index.ts b/packages/core/src/js/index.ts index 0ef9632203..e83e8d6d54 100644 --- a/packages/core/src/js/index.ts +++ b/packages/core/src/js/index.ts @@ -103,6 +103,12 @@ export { Mask, Unmask } from './replay/CustomMask'; export { FeedbackButton } from './feedback/FeedbackButton'; export { FeedbackWidget } from './feedback/FeedbackWidget'; -export { showFeedbackWidget, showFeedbackButton, hideFeedbackButton } from './feedback/FeedbackWidgetManager'; +export { + showFeedbackWidget, + showFeedbackButton, + hideFeedbackButton, + enableFeedbackOnShake, + disableFeedbackOnShake, +} from './feedback/FeedbackWidgetManager'; export { getDataFromUri } from './wrapper'; diff --git a/packages/core/test/feedback/ShakeToReportBug.test.tsx b/packages/core/test/feedback/ShakeToReportBug.test.tsx new file mode 100644 index 0000000000..3b85c17960 --- /dev/null +++ b/packages/core/test/feedback/ShakeToReportBug.test.tsx @@ -0,0 +1,213 @@ +import { debug, setCurrentClient } from '@sentry/core'; +import { render } from '@testing-library/react-native'; +import * as React from 'react'; +import { Text } from 'react-native'; +import { + resetFeedbackWidgetManager, +} from '../../src/js/feedback/FeedbackWidgetManager'; +import { FeedbackWidgetProvider } from '../../src/js/feedback/FeedbackWidgetProvider'; +import { feedbackIntegration } from '../../src/js/feedback/integration'; +import { isShakeListenerActive, startShakeListener, stopShakeListener } from '../../src/js/feedback/ShakeToReportBug'; +import { isModalSupported } from '../../src/js/feedback/utils'; +import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; + +jest.mock('../../src/js/feedback/utils', () => ({ + isModalSupported: jest.fn(), + isNativeDriverSupportedForColorAnimations: jest.fn().mockReturnValue(true), +})); + +const mockedIsModalSupported = isModalSupported as jest.MockedFunction; + +jest.mock('../../src/js/wrapper', () => ({ + getRNSentryModule: jest.fn(() => ({ + addListener: jest.fn(), + removeListeners: jest.fn(), + })), +})); + +let mockShakeCallback: (() => void) | undefined; +const mockRemove = jest.fn(); + +const createMockEmitter = () => { + return jest.fn().mockReturnValue({ + addListener: jest.fn().mockImplementation((_eventType: string, listener: () => void) => { + mockShakeCallback = listener; + return { remove: mockRemove }; + }), + }); +}; + +let mockEmitterFactory: ReturnType; + +// Also mock the module-level NativeEventEmitter used by FeedbackWidgetProvider's auto-start +jest.mock('../../src/js/feedback/ShakeToReportBug', () => { + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + return { + ...actual, + startShakeListener: jest.fn(actual.startShakeListener), + stopShakeListener: jest.fn(actual.stopShakeListener), + isShakeListenerActive: jest.fn(actual.isShakeListenerActive), + }; +}); + +beforeEach(() => { + debug.error = jest.fn(); + debug.log = jest.fn() as typeof debug.log; + debug.warn = jest.fn() as typeof debug.warn; +}); + +describe('ShakeToReportBug', () => { + beforeEach(() => { + const client = new TestClient(getDefaultTestClientOptions()); + setCurrentClient(client); + client.init(); + resetFeedbackWidgetManager(); + + // Get the actual functions (unmocked) + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + actual.stopShakeListener(); + + mockShakeCallback = undefined; + mockRemove.mockClear(); + mockEmitterFactory = createMockEmitter(); + + (startShakeListener as jest.Mock).mockClear(); + (stopShakeListener as jest.Mock).mockClear(); + (isShakeListenerActive as jest.Mock).mockClear(); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + describe('startShakeListener / stopShakeListener', () => { + it('starts listening for shake events', () => { + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + actual.startShakeListener(jest.fn(), mockEmitterFactory); + + expect(actual.isShakeListenerActive()).toBe(true); + expect(mockEmitterFactory).toHaveBeenCalledTimes(1); + }); + + it('does not start a second listener if already active', () => { + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + actual.startShakeListener(jest.fn(), mockEmitterFactory); + actual.startShakeListener(jest.fn(), mockEmitterFactory); + + expect(actual.isShakeListenerActive()).toBe(true); + expect(mockEmitterFactory).toHaveBeenCalledTimes(1); + }); + + it('stops listening for shake events', () => { + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + actual.startShakeListener(jest.fn(), mockEmitterFactory); + actual.stopShakeListener(); + + expect(actual.isShakeListenerActive()).toBe(false); + expect(mockRemove).toHaveBeenCalledTimes(1); + }); + + it('does not throw when stopping without starting', () => { + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + expect(() => actual.stopShakeListener()).not.toThrow(); + }); + + it('invokes onShake callback when shake event is received', () => { + const actual = jest.requireActual('../../src/js/feedback/ShakeToReportBug'); + const onShake = jest.fn(); + actual.startShakeListener(onShake, mockEmitterFactory); + + mockShakeCallback?.(); + + expect(onShake).toHaveBeenCalledTimes(1); + }); + }); + + describe('feedbackIntegration with enableShakeToReport', () => { + it('auto-starts shake listener when enableShakeToReport is true', () => { + mockedIsModalSupported.mockReturnValue(true); + + const integration = feedbackIntegration({ + enableShakeToReport: true, + }); + + const client = new TestClient(getDefaultTestClientOptions()); + setCurrentClient(client); + client.init(); + client.addIntegration(integration); + + render( + + App Components + , + ); + + expect(startShakeListener).toHaveBeenCalled(); + }); + + it('does not auto-start shake listener when enableShakeToReport is false', () => { + mockedIsModalSupported.mockReturnValue(true); + + const integration = feedbackIntegration({ + enableShakeToReport: false, + }); + + const client = new TestClient(getDefaultTestClientOptions()); + setCurrentClient(client); + client.init(); + client.addIntegration(integration); + + render( + + App Components + , + ); + + expect(startShakeListener).not.toHaveBeenCalled(); + }); + + it('does not auto-start shake listener when enableShakeToReport is not set', () => { + mockedIsModalSupported.mockReturnValue(true); + + const integration = feedbackIntegration(); + + const client = new TestClient(getDefaultTestClientOptions()); + setCurrentClient(client); + client.init(); + client.addIntegration(integration); + + render( + + App Components + , + ); + + expect(startShakeListener).not.toHaveBeenCalled(); + }); + + it('stops shake listener when FeedbackWidgetProvider unmounts', () => { + mockedIsModalSupported.mockReturnValue(true); + + const integration = feedbackIntegration({ + enableShakeToReport: true, + }); + + const client = new TestClient(getDefaultTestClientOptions()); + setCurrentClient(client); + client.init(); + client.addIntegration(integration); + + const { unmount } = render( + + App Components + , + ); + + expect(startShakeListener).toHaveBeenCalled(); + + unmount(); + + expect(stopShakeListener).toHaveBeenCalled(); + }); + }); +}); diff --git a/samples/react-native/src/App.tsx b/samples/react-native/src/App.tsx index 1a7b478246..56b1233a82 100644 --- a/samples/react-native/src/App.tsx +++ b/samples/react-native/src/App.tsx @@ -113,6 +113,7 @@ Sentry.init({ imagePicker: ImagePicker, enableScreenshot: true, enableTakeScreenshot: true, + enableShakeToReport: true, styles: { submitButton: { backgroundColor: '#6a1b9a',