@@ -13,18 +13,27 @@ const mockOnAddScreenshot = jest.fn();
13
13
const mockOnSubmitSuccess = jest . fn ( ) ;
14
14
const mockOnFormSubmitted = jest . fn ( ) ;
15
15
const mockOnSubmitError = jest . fn ( ) ;
16
- const mockGetUser = jest . fn ( ( ) => ( {
16
+
17
+ const mockCurrentScopeGetUser = jest . fn ( ( ) => ( {
17
18
18
19
name : 'Test User' ,
19
20
} ) ) ;
21
+ const mockIsolationScopeGetUser = jest . fn ( ) ;
22
+ const mockGlobalScopeGetUser = jest . fn ( ) ;
20
23
21
24
jest . spyOn ( Alert , 'alert' ) ;
22
25
23
26
jest . mock ( '@sentry/core' , ( ) => ( {
24
27
...jest . requireActual ( '@sentry/core' ) ,
25
28
captureFeedback : jest . fn ( ) ,
26
29
getCurrentScope : jest . fn ( ( ) => ( {
27
- getUser : mockGetUser ,
30
+ getUser : mockCurrentScopeGetUser ,
31
+ } ) ) ,
32
+ getIsolationScope : jest . fn ( ( ) => ( {
33
+ getUser : mockIsolationScopeGetUser ,
34
+ } ) ) ,
35
+ getGlobalScope : jest . fn ( ( ) => ( {
36
+ getUser : mockGlobalScopeGetUser ,
28
37
} ) ) ,
29
38
lastEventId : jest . fn ( ) ,
30
39
} ) ) ;
@@ -99,6 +108,12 @@ const customStyles: FeedbackWidgetStyles = {
99
108
} ;
100
109
101
110
describe ( 'FeedbackWidget' , ( ) => {
111
+ beforeEach ( ( ) => {
112
+ mockIsolationScopeGetUser . mockReturnValue ( undefined ) ;
113
+ mockGlobalScopeGetUser . mockReturnValue ( undefined ) ;
114
+ FeedbackWidget . reset ( ) ;
115
+ } ) ;
116
+
102
117
afterEach ( ( ) => {
103
118
jest . clearAllMocks ( ) ;
104
119
} ) ;
@@ -163,25 +178,77 @@ describe('FeedbackWidget', () => {
163
178
expect ( queryByTestId ( 'sentry-logo' ) ) . toBeNull ( ) ;
164
179
} ) ;
165
180
166
- it ( 'name and email are prefilled when sentry user is set' , ( ) => {
167
- const { getByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
181
+ describe ( 'User data prefilling' , ( ) => {
182
+ const users = {
183
+ prop :
{ name :
'Prop User' , email :
'[email protected] ' } ,
184
+ current :
{ name :
'Current User' , email :
'[email protected] ' } ,
185
+ isolation :
{ name :
'Isolation User' , email :
'[email protected] ' } ,
186
+ global :
{ name :
'Global User' , email :
'[email protected] ' } ,
187
+ } ;
188
+
189
+ it ( 'prefills from useSentryUser prop when provided' , ( ) => {
190
+ mockCurrentScopeGetUser . mockReturnValue ( users . current ) ;
191
+ const { getByPlaceholderText } = render (
192
+ < FeedbackWidget { ...defaultProps } useSentryUser = { users . prop } /> ,
193
+ ) ;
194
+ expect ( getByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( users . prop . name ) ;
195
+ expect ( getByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( users . prop . email ) ;
196
+ } ) ;
168
197
169
- const nameInput = getByPlaceholderText ( defaultProps . namePlaceholder ) ;
170
- const emailInput = getByPlaceholderText ( defaultProps . emailPlaceholder ) ;
198
+ it ( 'prefills from currentScope when useSentryUser prop is not set' , ( ) => {
199
+ mockCurrentScopeGetUser . mockReturnValue ( users . current ) ;
200
+ mockIsolationScopeGetUser . mockReturnValue ( users . isolation ) ;
201
+ mockGlobalScopeGetUser . mockReturnValue ( users . global ) ;
202
+
203
+ const { getByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
204
+ expect ( getByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( users . current . name ) ;
205
+ expect ( getByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( users . current . email ) ;
206
+ } ) ;
171
207
172
- expect ( nameInput . props . value ) . toBe ( 'Test User' ) ;
173
- expect ( emailInput . props . value ) . toBe ( '[email protected] ' ) ;
208
+ it ( 'prefills from isolationScope when useSentryUser prop and currentScope user are not set' , ( ) => {
209
+ mockCurrentScopeGetUser . mockReturnValue ( undefined ) ;
210
+ mockIsolationScopeGetUser . mockReturnValue ( users . isolation ) ;
211
+ mockGlobalScopeGetUser . mockReturnValue ( users . global ) ;
212
+
213
+ const { getByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
214
+ expect ( getByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( users . isolation . name ) ;
215
+ expect ( getByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( users . isolation . email ) ;
216
+ } ) ;
217
+
218
+ it ( 'prefills from globalScope when useSentryUser prop, currentScope, and isolationScope users are not set' , ( ) => {
219
+ mockCurrentScopeGetUser . mockReturnValue ( undefined ) ;
220
+ mockIsolationScopeGetUser . mockReturnValue ( undefined ) ;
221
+ mockGlobalScopeGetUser . mockReturnValue ( users . global ) ;
222
+
223
+ const { getByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
224
+ expect ( getByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( users . global . name ) ;
225
+ expect ( getByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( users . global . email ) ;
226
+ } ) ;
227
+
228
+ it ( 'prefills with empty strings if no user data is available from props or any scope' , ( ) => {
229
+ mockCurrentScopeGetUser . mockReturnValue ( undefined ) ;
230
+ mockIsolationScopeGetUser . mockReturnValue ( undefined ) ;
231
+ mockGlobalScopeGetUser . mockReturnValue ( undefined ) ;
232
+
233
+ const { getByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
234
+ expect ( getByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( '' ) ;
235
+ expect ( getByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( '' ) ;
236
+ } ) ;
174
237
} ) ;
175
238
176
- it ( 'ensure getUser is called only after the component is rendered' , ( ) => {
177
- // Ensure getUser is not called before render
178
- expect ( mockGetUser ) . not . toHaveBeenCalled ( ) ;
239
+ it ( 'ensure scope getUser methods are called during initialization when useSentryUser prop is not set' , ( ) => {
240
+ // Ensure scope getUser methods are not called before render
241
+ expect ( mockCurrentScopeGetUser ) . not . toHaveBeenCalled ( ) ;
242
+ expect ( mockIsolationScopeGetUser ) . not . toHaveBeenCalled ( ) ;
243
+ expect ( mockGlobalScopeGetUser ) . not . toHaveBeenCalled ( ) ;
179
244
180
245
// Render the component
181
- render ( < FeedbackWidget /> ) ;
246
+ render ( < FeedbackWidget { ... defaultProps } /> ) ;
182
247
183
- // After rendering, check that getUser was called twice (email and name)
184
- expect ( mockGetUser ) . toHaveBeenCalledTimes ( 2 ) ;
248
+ // After rendering, _getUser is called twice (for name and email).
249
+ expect ( mockCurrentScopeGetUser ) . toHaveBeenCalledTimes ( 2 ) ;
250
+ expect ( mockIsolationScopeGetUser ) . toHaveBeenCalledTimes ( 2 ) ;
251
+ expect ( mockGlobalScopeGetUser ) . toHaveBeenCalledTimes ( 2 ) ;
185
252
} ) ;
186
253
187
254
it ( 'shows an error message if required fields are empty' , async ( ) => {
@@ -399,8 +466,8 @@ describe('FeedbackWidget', () => {
399
466
unmount ( ) ;
400
467
const { queryByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
401
468
402
- expect ( queryByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( 'Test User ' ) ;
403
- expect ( queryByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( '[email protected] ' ) ;
469
+ expect ( queryByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( '' ) ;
470
+ expect ( queryByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( '' ) ;
404
471
expect ( queryByPlaceholderText ( defaultProps . messagePlaceholder ) . props . value ) . toBe ( '' ) ;
405
472
} ) ;
406
473
0 commit comments