-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle
captureFeedback
errors (#4364)
- Loading branch information
Showing
6 changed files
with
128 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const isValidEmail = (email: string): boolean => { | ||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; | ||
return emailRegex.test(email); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ import { FeedbackForm } from '../../src/js/feedback/FeedbackForm'; | |
import type { FeedbackFormProps, FeedbackFormStyles } from '../../src/js/feedback/FeedbackForm.types'; | ||
|
||
const mockOnFormClose = jest.fn(); | ||
const mockOnSubmitSuccess = jest.fn(); | ||
const mockOnFormSubmitted = jest.fn(); | ||
const mockOnSubmitError = jest.fn(); | ||
const mockGetUser = jest.fn(() => ({ | ||
email: '[email protected]', | ||
name: 'Test User', | ||
|
@@ -15,6 +18,7 @@ const mockGetUser = jest.fn(() => ({ | |
jest.spyOn(Alert, 'alert'); | ||
|
||
jest.mock('@sentry/core', () => ({ | ||
...jest.requireActual('@sentry/core'), | ||
captureFeedback: jest.fn(), | ||
getCurrentScope: jest.fn(() => ({ | ||
getUser: mockGetUser, | ||
|
@@ -24,6 +28,9 @@ jest.mock('@sentry/core', () => ({ | |
|
||
const defaultProps: FeedbackFormProps = { | ||
onFormClose: mockOnFormClose, | ||
onSubmitSuccess: mockOnSubmitSuccess, | ||
onFormSubmitted: mockOnFormSubmitted, | ||
onSubmitError: mockOnSubmitError, | ||
formTitle: 'Feedback Form', | ||
nameLabel: 'Name Label', | ||
namePlaceholder: 'Name Placeholder', | ||
|
@@ -38,6 +45,7 @@ const defaultProps: FeedbackFormProps = { | |
formError: 'Please fill out all required fields.', | ||
emailError: 'The email address is not valid.', | ||
successMessageText: 'Feedback success', | ||
genericError: 'Generic error', | ||
}; | ||
|
||
const customStyles: FeedbackFormStyles = { | ||
|
@@ -198,7 +206,57 @@ describe('FeedbackForm', () => { | |
}); | ||
}); | ||
|
||
it('calls onFormClose when the form is submitted successfully', async () => { | ||
it('shows an error message when there is a an error in captureFeedback', async () => { | ||
(captureFeedback as jest.Mock).mockImplementationOnce(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
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.genericError); | ||
}); | ||
}); | ||
|
||
it('calls onSubmitError when there is an error', async () => { | ||
(captureFeedback as jest.Mock).mockImplementationOnce(() => { | ||
throw new Error('Test error'); | ||
}); | ||
|
||
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(mockOnSubmitError).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('calls onSubmitSuccess when the form is submitted successfully', async () => { | ||
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(mockOnSubmitSuccess).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('calls onFormSubmitted when the form is submitted successfully', async () => { | ||
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />); | ||
|
||
fireEvent.changeText(getByPlaceholderText(defaultProps.namePlaceholder), 'John Doe'); | ||
|
@@ -208,7 +266,7 @@ describe('FeedbackForm', () => { | |
fireEvent.press(getByText(defaultProps.submitButtonLabel)); | ||
|
||
await waitFor(() => { | ||
expect(mockOnFormClose).toHaveBeenCalled(); | ||
expect(mockOnFormSubmitted).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters