-
-
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.
- Loading branch information
Showing
8 changed files
with
352 additions
and
0 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
50 changes: 50 additions & 0 deletions
50
packages/core/src/js/feedback/FeedbackFormScreen.styles.ts
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,50 @@ | ||
import { StyleSheet } from 'react-native'; | ||
|
||
const defaultStyles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
padding: 20, | ||
backgroundColor: '#fff', | ||
}, | ||
title: { | ||
fontSize: 24, | ||
fontWeight: 'bold', | ||
marginBottom: 20, | ||
textAlign: 'center', | ||
}, | ||
input: { | ||
height: 50, | ||
borderColor: '#ccc', | ||
borderWidth: 1, | ||
borderRadius: 5, | ||
paddingHorizontal: 10, | ||
marginBottom: 15, | ||
fontSize: 16, | ||
}, | ||
textArea: { | ||
height: 100, | ||
textAlignVertical: 'top', | ||
}, | ||
submitButton: { | ||
backgroundColor: '#6a1b9a', | ||
paddingVertical: 15, | ||
borderRadius: 5, | ||
alignItems: 'center', | ||
marginBottom: 10, | ||
}, | ||
submitText: { | ||
color: '#fff', | ||
fontSize: 18, | ||
fontWeight: 'bold', | ||
}, | ||
cancelButton: { | ||
paddingVertical: 15, | ||
alignItems: 'center', | ||
}, | ||
cancelText: { | ||
color: '#6a1b9a', | ||
fontSize: 16, | ||
}, | ||
}); | ||
|
||
export default defaultStyles; |
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,102 @@ | ||
import { captureFeedback } from '@sentry/core'; | ||
import type { SendFeedbackParams } from '@sentry/types'; | ||
import * as React from 'react'; | ||
import type { KeyboardTypeOptions } from 'react-native'; | ||
import { Alert, Text, TextInput, TouchableOpacity, View } from 'react-native'; | ||
|
||
import defaultStyles from './FeedbackFormScreen.styles'; | ||
import type { FeedbackFormScreenProps, FeedbackFormScreenState } from './FeedbackFormScreen.types'; | ||
|
||
/** | ||
* Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback. | ||
*/ | ||
export class FeedbackFormScreen extends React.Component<FeedbackFormScreenProps, FeedbackFormScreenState> { | ||
public constructor(props: FeedbackFormScreenProps) { | ||
super(props); | ||
this.state = { | ||
name: '', | ||
email: '', | ||
description: '', | ||
}; | ||
} | ||
|
||
public handleFeedbackSubmit: () => void = () => { | ||
const { name, email, description } = this.state; | ||
const { closeScreen, text } = this.props; | ||
|
||
const trimmedName = name?.trim(); | ||
const trimmedEmail = email?.trim(); | ||
const trimmedDescription = description?.trim(); | ||
|
||
if (!trimmedName || !trimmedEmail || !trimmedDescription) { | ||
const errorMessage = text?.formError || 'Please fill out all required fields.'; | ||
Alert.alert(text?.errorTitle || 'Error', errorMessage); | ||
return; | ||
} | ||
|
||
if (!this._isValidEmail(trimmedEmail)) { | ||
const errorMessage = text?.emailError || 'Please enter a valid email address.'; | ||
Alert.alert(text?.errorTitle || 'Error', errorMessage); | ||
return; | ||
} | ||
|
||
const userFeedback: SendFeedbackParams = { | ||
message: trimmedDescription, | ||
name: trimmedName, | ||
email: trimmedEmail, | ||
}; | ||
|
||
captureFeedback(userFeedback); | ||
closeScreen(); | ||
}; | ||
|
||
/** | ||
* Renders the feedback form screen. | ||
*/ | ||
public render(): React.ReactNode { | ||
const { closeScreen, text, styles } = this.props; | ||
const { name, email, description } = this.state; | ||
|
||
return ( | ||
<View style={styles?.container || defaultStyles.container}> | ||
<Text style={styles?.title || defaultStyles.title}>{text?.formTitle || 'Feedback Form'}</Text> | ||
|
||
<TextInput | ||
style={styles?.input || defaultStyles.input} | ||
placeholder={text?.namePlaceholder || 'Name'} | ||
value={name} | ||
onChangeText={(value) => this.setState({ name: value })} | ||
/> | ||
|
||
<TextInput | ||
style={styles?.input || defaultStyles.input} | ||
placeholder={text?.emailPlaceholder || 'Email'} | ||
keyboardType={'email-address' as KeyboardTypeOptions} | ||
value={email} | ||
onChangeText={(value) => this.setState({ email: value })} | ||
/> | ||
|
||
<TextInput | ||
style={[styles?.input || defaultStyles.input, styles?.textArea || defaultStyles.textArea]} | ||
placeholder={text?.descriptionPlaceholder || 'Description (required)'} | ||
value={description} | ||
onChangeText={(value) => this.setState({ description: value })} | ||
multiline | ||
/> | ||
|
||
<TouchableOpacity style={styles?.submitButton || defaultStyles.submitButton} onPress={this.handleFeedbackSubmit}> | ||
<Text style={styles?.submitText || defaultStyles.submitText}>{text?.submitButton || 'Send Feedback'}</Text> | ||
</TouchableOpacity> | ||
|
||
<TouchableOpacity style={styles?.cancelButton || defaultStyles.cancelButton} onPress={closeScreen}> | ||
<Text style={styles?.cancelText || defaultStyles.cancelText}>{text?.cancelButton || 'Cancel'}</Text> | ||
</TouchableOpacity> | ||
</View> | ||
); | ||
} | ||
|
||
private _isValidEmail = (email: string): boolean => { | ||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { TextStyle, ViewStyle } from 'react-native'; | ||
|
||
export interface FeedbackFormScreenProps { | ||
closeScreen: () => void; | ||
text: FeedbackFormText; | ||
styles?: FeedbackFormScreenStyles; | ||
} | ||
|
||
export interface FeedbackFormText { | ||
formTitle?: string; | ||
namePlaceholder?: string; | ||
emailPlaceholder?: string; | ||
descriptionPlaceholder?: string; | ||
submitButton?: string; | ||
cancelButton?: string; | ||
errorTitle?: string; | ||
formError?: string; | ||
emailError?: string; | ||
} | ||
|
||
export interface FeedbackFormScreenStyles { | ||
container?: ViewStyle; | ||
title?: TextStyle; | ||
input?: TextStyle; | ||
textArea?: ViewStyle; | ||
submitButton?: ViewStyle; | ||
submitText?: TextStyle; | ||
cancelButton?: ViewStyle; | ||
cancelText?: TextStyle; | ||
} | ||
|
||
export interface FeedbackFormScreenState { | ||
name: string; | ||
email: string; | ||
description: string; | ||
} |
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
111 changes: 111 additions & 0 deletions
111
packages/core/test/feedback/FeedbackFormScreen.test.tsx
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,111 @@ | ||
import { captureFeedback } from '@sentry/core'; | ||
import { fireEvent, render, waitFor } from '@testing-library/react-native'; | ||
import * as React from 'react'; | ||
import { Alert } from 'react-native'; | ||
|
||
import { FeedbackFormScreen } from '../../src/js/feedback/FeedbackFormScreen'; | ||
import type { FeedbackFormScreenProps } from '../../src/js/feedback/FeedbackFormScreen.types'; | ||
|
||
const mockCloseScreen = jest.fn(); | ||
|
||
jest.spyOn(Alert, 'alert'); | ||
|
||
jest.mock('@sentry/core', () => ({ | ||
captureFeedback: jest.fn(), | ||
})); | ||
|
||
const defaultProps: FeedbackFormScreenProps = { | ||
closeScreen: mockCloseScreen, | ||
text: { | ||
formTitle: 'Feedback Form', | ||
namePlaceholder: 'Name', | ||
emailPlaceholder: 'Email', | ||
descriptionPlaceholder: 'Description', | ||
submitButton: 'Submit', | ||
cancelButton: 'Cancel', | ||
errorTitle: 'Error', | ||
formError: 'Please fill out all required fields.', | ||
emailError: 'The email address is not valid.', | ||
}, | ||
}; | ||
|
||
describe('FeedbackFormScreen', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly', () => { | ||
const { getByPlaceholderText, getByText } = render(<FeedbackFormScreen {...defaultProps} />); | ||
|
||
expect(getByText(defaultProps.text.formTitle)).toBeTruthy(); | ||
expect(getByPlaceholderText(defaultProps.text.namePlaceholder)).toBeTruthy(); | ||
expect(getByPlaceholderText(defaultProps.text.emailPlaceholder)).toBeTruthy(); | ||
expect(getByPlaceholderText(defaultProps.text.descriptionPlaceholder)).toBeTruthy(); | ||
expect(getByText(defaultProps.text.submitButton)).toBeTruthy(); | ||
expect(getByText(defaultProps.text.cancelButton)).toBeTruthy(); | ||
}); | ||
|
||
it('shows an error message if required fields are empty', async () => { | ||
const { getByText } = render(<FeedbackFormScreen {...defaultProps} />); | ||
|
||
fireEvent.press(getByText(defaultProps.text.submitButton)); | ||
|
||
await waitFor(() => { | ||
expect(Alert.alert).toHaveBeenCalledWith(defaultProps.text.errorTitle, defaultProps.text.formError); | ||
}); | ||
}); | ||
|
||
it('shows an error message if the email is not valid', async () => { | ||
const { getByPlaceholderText, getByText } = render(<FeedbackFormScreen {...defaultProps} />); | ||
|
||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.namePlaceholder), 'John Doe'); | ||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.emailPlaceholder), 'not-an-email'); | ||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.descriptionPlaceholder), 'This is a feedback message.'); | ||
|
||
fireEvent.press(getByText(defaultProps.text.submitButton)); | ||
|
||
await waitFor(() => { | ||
expect(Alert.alert).toHaveBeenCalledWith(defaultProps.text.errorTitle, defaultProps.text.emailError); | ||
}); | ||
}); | ||
|
||
it('calls captureFeedback when the form is submitted successfully', async () => { | ||
const { getByPlaceholderText, getByText } = render(<FeedbackFormScreen {...defaultProps} />); | ||
|
||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.namePlaceholder), 'John Doe'); | ||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.emailPlaceholder), '[email protected]'); | ||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.descriptionPlaceholder), 'This is a feedback message.'); | ||
|
||
fireEvent.press(getByText(defaultProps.text.submitButton)); | ||
|
||
await waitFor(() => { | ||
expect(captureFeedback).toHaveBeenCalledWith({ | ||
message: 'This is a feedback message.', | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
}); | ||
}); | ||
}); | ||
|
||
it('calls closeScreen when the form is submitted successfully', async () => { | ||
const { getByPlaceholderText, getByText } = render(<FeedbackFormScreen {...defaultProps} />); | ||
|
||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.namePlaceholder), 'John Doe'); | ||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.emailPlaceholder), '[email protected]'); | ||
fireEvent.changeText(getByPlaceholderText(defaultProps.text.descriptionPlaceholder), 'This is a feedback message.'); | ||
|
||
fireEvent.press(getByText(defaultProps.text.submitButton)); | ||
|
||
await waitFor(() => { | ||
expect(mockCloseScreen).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
it('calls closeScreen when the cancel button is pressed', () => { | ||
const { getByText } = render(<FeedbackFormScreen {...defaultProps} />); | ||
|
||
fireEvent.press(getByText(defaultProps.text.cancelButton)); | ||
|
||
expect(mockCloseScreen).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
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