Skip to content

Commit

Permalink
Use sendFeedback from JS to get error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
antonis committed Dec 17, 2024
1 parent 65a03a8 commit 30ad951
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"react-native": ">=0.65.0"
},
"dependencies": {
"@sentry-internal/feedback": "8.45.1",
"@sentry/babel-plugin-component-annotate": "2.20.1",
"@sentry/browser": "8.45.1",
"@sentry/cli": "2.39.1",
Expand Down
29 changes: 22 additions & 7 deletions packages/core/src/js/feedback/FeedbackForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { captureFeedback, getCurrentScope, lastEventId, logger } from '@sentry/core';
import type { SendFeedbackParams } from '@sentry/types';
import { sendFeedback } from '@sentry-internal/feedback';
import type { EventHint, SendFeedbackParams } from '@sentry/core';
import { getCurrentScope, lastEventId, logger } from '@sentry/core';
import * as React from 'react';
import type { KeyboardTypeOptions } from 'react-native';
import {
Expand All @@ -20,6 +21,15 @@ import defaultStyles from './FeedbackForm.styles';
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';
import { checkInternetConnection, isValidEmail } from './utils';

const submitFeedback = async (feedbackParams: SendFeedbackParams, hint: EventHint, success: () => void, error: (e: string) => void): Promise<void> => {
try {
await sendFeedback(feedbackParams, hint);
success();
} catch (e) {
error(e.toString());
}
};

/**
* @beta
* Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback.
Expand Down Expand Up @@ -75,11 +85,16 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor

// eslint-disable-next-line @typescript-eslint/no-floating-promises
checkInternetConnection(() => { // onConnected
this.setState({ isVisible: false });
captureFeedback(userFeedback);
onSubmitSuccess({ name: trimmedName, email: trimmedEmail, message: trimmedDescription, attachments: undefined });
Alert.alert(text.successMessageText);
onFormSubmitted();
// eslint-disable-next-line @typescript-eslint/no-floating-promises
submitFeedback(userFeedback, undefined, () => { // success
this.setState({ isVisible: false });
onSubmitSuccess({ name: trimmedName, email: trimmedEmail, message: trimmedDescription, attachments: undefined });
Alert.alert(text.successMessageText);
onFormSubmitted();
}, (error: string) => { // error
Alert.alert(text.errorTitle, error);
logger.error(`Feedback form submission failed: ${error}`);
});
}, () => { // onDisconnected
Alert.alert(text.errorTitle, text.networkError);
logger.error(`Feedback form submission failed: ${text.networkError}`);
Expand Down
12 changes: 7 additions & 5 deletions packages/core/test/feedback/FeedbackForm.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureFeedback } from '@sentry/core';
import { sendFeedback } from '@sentry-internal/feedback';
import { fireEvent, render, waitFor } from '@testing-library/react-native';
import * as React from 'react';
import { Alert } from 'react-native';
Expand All @@ -16,7 +16,6 @@ jest.spyOn(Alert, 'alert');

jest.mock('@sentry/core', () => ({
...jest.requireActual('@sentry/core'),
captureFeedback: jest.fn(),
getCurrentScope: jest.fn(() => ({
getUser: jest.fn(() => ({
email: '[email protected]',
Expand All @@ -29,6 +28,9 @@ jest.mock('../../src/js/feedback/utils', () => ({
...jest.requireActual('../../src/js/feedback/utils'),
checkInternetConnection: jest.fn(),
}));
jest.mock('@sentry-internal/feedback', () => ({
sendFeedback: jest.fn(),
}));

const defaultProps: FeedbackFormProps = {
onFormClose: mockOnFormClose,
Expand Down Expand Up @@ -112,7 +114,7 @@ describe('FeedbackForm', () => {
});
});

it('calls captureFeedback when the form is submitted successfully', async () => {
it('calls sendFeedback when the form is submitted successfully', async () => {
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />);

fireEvent.changeText(getByPlaceholderText(defaultProps.namePlaceholder), 'John Doe');
Expand All @@ -122,11 +124,11 @@ describe('FeedbackForm', () => {
fireEvent.press(getByText(defaultProps.submitButtonLabel));

await waitFor(() => {
expect(captureFeedback).toHaveBeenCalledWith({
expect(sendFeedback).toHaveBeenCalledWith({
message: 'This is a feedback message.',
name: 'John Doe',
email: '[email protected]',
});
}, undefined);
});
});

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7821,6 +7821,7 @@ __metadata:
"@react-native/babel-preset": 0.76.3
"@sentry-internal/eslint-config-sdk": 8.45.1
"@sentry-internal/eslint-plugin-sdk": 8.45.1
"@sentry-internal/feedback": 8.45.1
"@sentry-internal/typescript": 8.45.1
"@sentry/babel-plugin-component-annotate": 2.20.1
"@sentry/browser": 8.45.1
Expand Down

0 comments on commit 30ad951

Please sign in to comment.