Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(2.1) Handles Capture feedback form network connection issues #4364

Open
wants to merge 17 commits into
base: antonis/3859-newCaptureFeedbackAPI-Form
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/core/src/js/feedback/FeedbackForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureFeedback, lastEventId } from '@sentry/core';
import { captureFeedback, lastEventId, logger } from '@sentry/core';
import type { SendFeedbackParams } from '@sentry/types';
import * as React from 'react';
import type { KeyboardTypeOptions } from 'react-native';
Expand All @@ -7,6 +7,7 @@ import { Alert, Text, TextInput, TouchableOpacity, View } from 'react-native';
import { defaultConfiguration } from './defaults';
import defaultStyles from './FeedbackForm.styles';
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';
import { checkInternetConnection, isValidEmail } from './utils';

/**
* @beta
Expand Down Expand Up @@ -40,7 +41,7 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
return;
}

if ((config.isEmailRequired || trimmedEmail.length > 0) && !this._isValidEmail(trimmedEmail)) {
if ((config.isEmailRequired || trimmedEmail.length > 0) && !isValidEmail(trimmedEmail)) {
Alert.alert(text.errorTitle, text.emailError);
return;
}
Expand All @@ -53,11 +54,16 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
associatedEventId: eventId,
};

onFormClose();
this.setState({ isVisible: false });

captureFeedback(userFeedback);
Alert.alert(text.successMessageText);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
checkInternetConnection(() => {
onFormClose();
this.setState({ isVisible: false });
captureFeedback(userFeedback);
Alert.alert(text.successMessageText);
}, () => {
Alert.alert(text.errorTitle, text.networkError);
logger.error(`Feedback form submission failed: ${text.networkError}`);
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
});
};
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down Expand Up @@ -135,9 +141,4 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
</View>
);
}

private _isValidEmail = (email: string): boolean => {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
return emailRegex.test(email);
};
}
5 changes: 5 additions & 0 deletions packages/core/src/js/feedback/FeedbackForm.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ export interface FeedbackTextConfiguration {
* The error message when the email is invalid
*/
emailError?: string;

/**
* Message when there is a network error
*/
networkError?: string;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/js/feedback/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const ERROR_TITLE = 'Error';
const FORM_ERROR = 'Please fill out all required fields.';
const EMAIL_ERROR = 'Please enter a valid email address.';
const SUCCESS_MESSAGE_TEXT = 'Thank you for your report!';
const CONNECTIONS_ERROR_TEXT = 'Unable to send Feedback due to network issues.';

export const defaultConfiguration: Partial<FeedbackFormProps> = {
// FeedbackCallbacks
Expand Down Expand Up @@ -54,4 +55,5 @@ export const defaultConfiguration: Partial<FeedbackFormProps> = {
formError: FORM_ERROR,
emailError: EMAIL_ERROR,
successMessageText: SUCCESS_MESSAGE_TEXT,
networkError: CONNECTIONS_ERROR_TEXT,
};
17 changes: 17 additions & 0 deletions packages/core/src/js/feedback/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const checkInternetConnection = async (onConnected: () => void, onDisconnected: () => void): Promise<void> => {
try {
const response = await fetch('https://sentry.io', { method: 'HEAD' });
if (response.ok) {
onConnected();
} else {
onDisconnected();
}
} catch (error) {
onDisconnected();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i: We should add an onError call and pass error as a parameter, so users know they are facing an issue that isn't unrelated to connectivity issues (for example, wrong dsn, server rejecting the feedback, some sdk error,...)

a suggestion for the error message: "Unable to send feedback due to an unexpected error."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea 👍
Added an onError method to communicate the unexpected error with 40b4fd3

}
};

export const isValidEmail = (email: string): boolean => {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return emailRegex.test(email);
};
30 changes: 30 additions & 0 deletions packages/core/test/feedback/FeedbackForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Alert } from 'react-native';

import { FeedbackForm } from '../../src/js/feedback/FeedbackForm';
import type { FeedbackFormProps } from '../../src/js/feedback/FeedbackForm.types';
import { checkInternetConnection } from '../../src/js/feedback/utils';

const mockOnFormClose = jest.fn();

jest.spyOn(Alert, 'alert');

jest.mock('@sentry/core', () => ({
...jest.requireActual('@sentry/core'),
captureFeedback: jest.fn(),
getCurrentScope: jest.fn(() => ({
getUser: jest.fn(() => ({
Expand All @@ -20,6 +22,10 @@ jest.mock('@sentry/core', () => ({
})),
lastEventId: jest.fn(),
}));
jest.mock('../../src/js/feedback/utils', () => ({
...jest.requireActual('../../src/js/feedback/utils'),
checkInternetConnection: jest.fn(),
}));

const defaultProps: FeedbackFormProps = {
onFormClose: mockOnFormClose,
Expand All @@ -37,9 +43,15 @@ const defaultProps: FeedbackFormProps = {
formError: 'Please fill out all required fields.',
emailError: 'The email address is not valid.',
successMessageText: 'Feedback success',
networkError: 'Network error',
};

describe('FeedbackForm', () => {
beforeEach(() => {
(checkInternetConnection as jest.Mock).mockImplementation((onConnected, _) => {
onConnected();
});
});
afterEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -125,6 +137,24 @@ describe('FeedbackForm', () => {
});
});

it('shows an error message when there is no network connection', async () => {
(checkInternetConnection as jest.Mock).mockImplementationOnce((_, onDisconnected) => {
onDisconnected();
});

const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />);

fireEvent.changeText(getByPlaceholderText(defaultProps.namePlaceholder), 'John Doe');
fireEvent.changeText(getByPlaceholderText(defaultProps.emailPlaceholder), '[email protected]');
fireEvent.changeText(getByPlaceholderText(defaultProps.messagePlaceholder), 'This is a feedback message.');

fireEvent.press(getByText(defaultProps.submitButtonLabel));

await waitFor(() => {
expect(Alert.alert).toHaveBeenCalledWith(defaultProps.errorTitle, defaultProps.networkError);
});
});

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

Expand Down
Loading