@@ -12,8 +12,7 @@ SENTRY_APM_SAMPLING = 1
1212)*/
1313const TEST_SPAN_ID = '518999beeceb49af' ;
1414
15- const mockSpanFinish = jest . fn ( ) ;
16- const mockStartChild = jest . fn ( ( spanArgs : SpanContext ) => ( { ...spanArgs , finish : mockSpanFinish } ) ) ;
15+ const mockStartChild = jest . fn ( ( spanArgs : SpanContext ) => ( { ...spanArgs } ) ) ;
1716const TEST_SPAN = {
1817 spanId : TEST_SPAN_ID ,
1918 startChild : mockStartChild ,
@@ -57,7 +56,6 @@ beforeEach(() => {
5756 mockLoggerWarn . mockClear ( ) ;
5857 mockGetActivitySpan . mockClear ( ) ;
5958 mockStartChild . mockClear ( ) ;
60- mockSpanFinish . mockClear ( ) ;
6159} ) ;
6260
6361describe ( 'withProfiler' , ( ) => {
@@ -111,26 +109,30 @@ describe('withProfiler', () => {
111109 } ) ;
112110
113111 describe ( 'render span' , ( ) => {
114- it ( 'does not get created by default ' , ( ) => {
112+ it ( 'is created on unmount ' , ( ) => {
115113 const ProfiledComponent = withProfiler ( ( ) => < h1 > Testing</ h1 > ) ;
116114 expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
117- render ( < ProfiledComponent /> ) ;
118- expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
119- } ) ;
120115
121- it ( 'is created when given hasRenderSpan option' , ( ) => {
122- const ProfiledComponent = withProfiler ( ( ) => < h1 > Testing</ h1 > , { hasRenderSpan : true } ) ;
123- expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
124116 const component = render ( < ProfiledComponent /> ) ;
117+ component . unmount ( ) ;
118+
125119 expect ( mockStartChild ) . toHaveBeenCalledTimes ( 1 ) ;
126- expect ( mockStartChild ) . toHaveBeenLastCalledWith ( {
127- description : `<${ UNKNOWN_COMPONENT } >` ,
128- op : 'react.render' ,
129- } ) ;
120+ expect ( mockStartChild ) . toHaveBeenLastCalledWith (
121+ expect . objectContaining ( {
122+ description : `<${ UNKNOWN_COMPONENT } >` ,
123+ op : 'react.render' ,
124+ } ) ,
125+ ) ;
126+ } ) ;
130127
131- expect ( mockSpanFinish ) . toHaveBeenCalledTimes ( 0 ) ;
128+ it ( 'is not created if hasRenderSpan is false' , ( ) => {
129+ const ProfiledComponent = withProfiler ( ( ) => < h1 > Testing</ h1 > , { hasRenderSpan : false } ) ;
130+ expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
131+
132+ const component = render ( < ProfiledComponent /> ) ;
132133 component . unmount ( ) ;
133- expect ( mockSpanFinish ) . toHaveBeenCalledTimes ( 1 ) ;
134+
135+ expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
134136 } ) ;
135137 } ) ;
136138
@@ -207,25 +209,27 @@ describe('useProfiler()', () => {
207209 } ) ;
208210
209211 describe ( 'render span' , ( ) => {
210- it ( 'does not get created by default ' , ( ) => {
212+ it ( 'does not get created when hasRenderSpan is false ' , ( ) => {
211213 // tslint:disable-next-line: no-void-expression
212- renderHook ( ( ) => useProfiler ( 'Example' ) ) ;
214+ const component = renderHook ( ( ) => useProfiler ( 'Example' , { hasRenderSpan : false } ) ) ;
215+ expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
216+ component . unmount ( ) ;
213217 expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
214218 } ) ;
215219
216- it ( 'is created when given hasRenderSpan option ' , ( ) => {
220+ it ( 'is created by default ' , ( ) => {
217221 // tslint:disable-next-line: no-void-expression
218- const component = renderHook ( ( ) => useProfiler ( 'Example' , { hasRenderSpan : true } ) ) ;
219-
220- expect ( mockStartChild ) . toHaveBeenCalledTimes ( 1 ) ;
221- expect ( mockStartChild ) . toHaveBeenLastCalledWith ( {
222- description : '<Example>' ,
223- op : 'react.render' ,
224- } ) ;
222+ const component = renderHook ( ( ) => useProfiler ( 'Example' ) ) ;
225223
226- expect ( mockSpanFinish ) . toHaveBeenCalledTimes ( 0 ) ;
224+ expect ( mockStartChild ) . toHaveBeenCalledTimes ( 0 ) ;
227225 component . unmount ( ) ;
228- expect ( mockSpanFinish ) . toHaveBeenCalledTimes ( 1 ) ;
226+ expect ( mockStartChild ) . toHaveBeenCalledTimes ( 1 ) ;
227+ expect ( mockStartChild ) . toHaveBeenLastCalledWith (
228+ expect . objectContaining ( {
229+ description : '<Example>' ,
230+ op : 'react.render' ,
231+ } ) ,
232+ ) ;
229233 } ) ;
230234 } ) ;
231235} ) ;
0 commit comments