@@ -13,18 +13,27 @@ const mockOnAddScreenshot = jest.fn();
1313const mockOnSubmitSuccess = jest . fn ( ) ;
1414const mockOnFormSubmitted = jest . fn ( ) ;
1515const mockOnSubmitError = jest . fn ( ) ;
16- const mockGetUser = jest . fn ( ( ) => ( {
16+
17+ const mockCurrentScopeGetUser = jest . fn ( ( ) => ( {
1718 email : 'test@example.com' ,
1819 name : 'Test User' ,
1920} ) ) ;
21+ const mockIsolationScopeGetUser = jest . fn ( ) ;
22+ const mockGlobalScopeGetUser = jest . fn ( ) ;
2023
2124jest . spyOn ( Alert , 'alert' ) ;
2225
2326jest . mock ( '@sentry/core' , ( ) => ( {
2427 ...jest . requireActual ( '@sentry/core' ) ,
2528 captureFeedback : jest . fn ( ) ,
2629 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 ,
2837 } ) ) ,
2938 lastEventId : jest . fn ( ) ,
3039} ) ) ;
@@ -99,6 +108,12 @@ const customStyles: FeedbackWidgetStyles = {
99108} ;
100109
101110describe ( 'FeedbackWidget' , ( ) => {
111+ beforeEach ( ( ) => {
112+ mockIsolationScopeGetUser . mockReturnValue ( undefined ) ;
113+ mockGlobalScopeGetUser . mockReturnValue ( undefined ) ;
114+ FeedbackWidget . reset ( ) ;
115+ } ) ;
116+
102117 afterEach ( ( ) => {
103118 jest . clearAllMocks ( ) ;
104119 } ) ;
@@ -163,25 +178,77 @@ describe('FeedbackWidget', () => {
163178 expect ( queryByTestId ( 'sentry-logo' ) ) . toBeNull ( ) ;
164179 } ) ;
165180
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 : 'prop@example.com' } ,
184+ current : { name : 'Current User' , email : 'current@example.com' } ,
185+ isolation : { name : 'Isolation User' , email : 'isolation@example.com' } ,
186+ global : { name : 'Global User' , email : 'global@example.com' } ,
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+ } ) ;
168197
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+ } ) ;
171207
172- expect ( nameInput . props . value ) . toBe ( 'Test User' ) ;
173- expect ( emailInput . props . value ) . toBe ( 'test@example.com' ) ;
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+ } ) ;
174237 } ) ;
175238
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 ( ) ;
179244
180245 // Render the component
181- render ( < FeedbackWidget /> ) ;
246+ render ( < FeedbackWidget { ... defaultProps } /> ) ;
182247
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 ) ;
185252 } ) ;
186253
187254 it ( 'shows an error message if required fields are empty' , async ( ) => {
@@ -399,8 +466,8 @@ describe('FeedbackWidget', () => {
399466 unmount ( ) ;
400467 const { queryByPlaceholderText } = render ( < FeedbackWidget { ...defaultProps } /> ) ;
401468
402- expect ( queryByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( 'Test User ' ) ;
403- expect ( queryByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( 'test@example.com ' ) ;
469+ expect ( queryByPlaceholderText ( defaultProps . namePlaceholder ) . props . value ) . toBe ( '' ) ;
470+ expect ( queryByPlaceholderText ( defaultProps . emailPlaceholder ) . props . value ) . toBe ( '' ) ;
404471 expect ( queryByPlaceholderText ( defaultProps . messagePlaceholder ) . props . value ) . toBe ( '' ) ;
405472 } ) ;
406473
0 commit comments