-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
12 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,21 @@ | ||
export const checkInternetConnection = async ( | ||
onConnected: () => void, | ||
onDisconnected: () => void, | ||
onError: () => void, | ||
): Promise<void> => { | ||
try { | ||
const response = await fetch('https://sentry.io', { method: 'HEAD' }); | ||
if (response.ok) { | ||
onConnected(); | ||
} else { | ||
onDisconnected(); | ||
} | ||
} catch (error) { | ||
onError(); | ||
} | ||
}; | ||
|
||
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 |
---|---|---|
|
@@ -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(() => ({ | ||
|
@@ -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, | ||
|
@@ -37,9 +43,16 @@ const defaultProps: FeedbackFormProps = { | |
formError: 'Please fill out all required fields.', | ||
emailError: 'The email address is not valid.', | ||
successMessageText: 'Feedback success', | ||
networkError: 'Network error', | ||
genericError: 'Generic error', | ||
}; | ||
|
||
describe('FeedbackForm', () => { | ||
beforeEach(() => { | ||
(checkInternetConnection as jest.Mock).mockImplementation((onConnected, _onDisconnected, _onError) => { | ||
onConnected(); | ||
}); | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
@@ -125,6 +138,42 @@ describe('FeedbackForm', () => { | |
}); | ||
}); | ||
|
||
it('shows an error message when there is no network connection', async () => { | ||
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, onDisconnected, _onError) => { | ||
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('shows an error message when there is a generic connection', async () => { | ||
(checkInternetConnection as jest.Mock).mockImplementationOnce((_onConnected, _onDisconnected, onError) => { | ||
onError(); | ||
}); | ||
|
||
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 onFormClose when the form is submitted successfully', async () => { | ||
const { getByPlaceholderText, getByText } = render(<FeedbackForm {...defaultProps} />); | ||
|
||
|