@@ -6,6 +6,17 @@ import { UNKNOWN_COMPONENT, useProfiler, withProfiler } from '../src/profiler';
66
77const mockPushActivity = jest . fn ( ) . mockReturnValue ( 1 ) ;
88const mockPopActivity = jest . fn ( ) ;
9+ const mockLoggerWarn = jest . fn ( ) ;
10+
11+ let integrationIsNull = false ;
12+
13+ jest . mock ( '@sentry/utils' , ( ) => ( {
14+ logger : {
15+ warn : ( message : string ) => {
16+ mockLoggerWarn ( message ) ;
17+ } ,
18+ } ,
19+ } ) ) ;
920
1021jest . mock ( '@sentry/browser' , ( ) => ( {
1122 getCurrentHub : ( ) => ( {
@@ -20,7 +31,11 @@ jest.mock('@sentry/browser', () => ({
2031 public static popActivity : ( ) => void = mockPopActivity ;
2132 }
2233
23- return new MockIntegration ( 'test' ) ;
34+ if ( ! integrationIsNull ) {
35+ return new MockIntegration ( 'test' ) ;
36+ }
37+
38+ return null ;
2439 } ,
2540 } ) ,
2641} ) ) ;
@@ -30,6 +45,8 @@ describe('withProfiler', () => {
3045 jest . useFakeTimers ( ) ;
3146 mockPushActivity . mockClear ( ) ;
3247 mockPopActivity . mockClear ( ) ;
48+ mockLoggerWarn . mockClear ( ) ;
49+ integrationIsNull = false ;
3350 } ) ;
3451
3552 it ( 'sets displayName properly' , ( ) => {
@@ -39,6 +56,18 @@ describe('withProfiler', () => {
3956 expect ( ProfiledComponent . displayName ) . toBe ( 'profiler(TestComponent)' ) ;
4057 } ) ;
4158
59+ it ( 'sets a custom displayName' , ( ) => {
60+ const TestComponent = ( ) => < h1 > Hello World</ h1 > ;
61+
62+ const ProfiledComponent = withProfiler ( TestComponent , 'BestComponent' ) ;
63+ expect ( ProfiledComponent . displayName ) . toBe ( 'profiler(BestComponent)' ) ;
64+ } ) ;
65+
66+ it ( 'defaults to an unknown displayName' , ( ) => {
67+ const ProfiledComponent = withProfiler ( ( ) => < h1 > Hello World</ h1 > ) ;
68+ expect ( ProfiledComponent . displayName ) . toBe ( `profiler(${ UNKNOWN_COMPONENT } )` ) ;
69+ } ) ;
70+
4271 it ( 'popActivity() is called when unmounted' , ( ) => {
4372 const ProfiledComponent = withProfiler ( ( ) => < h1 > Hello World</ h1 > ) ;
4473
@@ -63,13 +92,32 @@ describe('withProfiler', () => {
6392 op : 'react' ,
6493 } ) ;
6594 } ) ;
95+
96+ it ( 'does not start an activity when integration is disabled' , ( ) => {
97+ integrationIsNull = true ;
98+ const ProfiledComponent = withProfiler ( ( ) => < h1 > Hello World</ h1 > ) ;
99+
100+ expect ( mockPushActivity ) . toHaveBeenCalledTimes ( 0 ) ;
101+ expect ( mockLoggerWarn ) . toHaveBeenCalledTimes ( 0 ) ;
102+
103+ const profiler = render ( < ProfiledComponent /> ) ;
104+ expect ( mockPopActivity ) . toHaveBeenCalledTimes ( 0 ) ;
105+ expect ( mockPushActivity ) . toHaveBeenCalledTimes ( 0 ) ;
106+
107+ expect ( mockLoggerWarn ) . toHaveBeenCalledTimes ( 1 ) ;
108+
109+ profiler . unmount ( ) ;
110+ expect ( mockPopActivity ) . toHaveBeenCalledTimes ( 0 ) ;
111+ } ) ;
66112} ) ;
67113
68114describe ( 'useProfiler()' , ( ) => {
69115 beforeEach ( ( ) => {
70116 jest . useFakeTimers ( ) ;
71117 mockPushActivity . mockClear ( ) ;
72118 mockPopActivity . mockClear ( ) ;
119+ mockLoggerWarn . mockClear ( ) ;
120+ integrationIsNull = false ;
73121 } ) ;
74122
75123 it ( 'popActivity() is called when unmounted' , ( ) => {
@@ -95,4 +143,20 @@ describe('useProfiler()', () => {
95143 op : 'react' ,
96144 } ) ;
97145 } ) ;
146+
147+ it ( 'does not start an activity when integration is disabled' , ( ) => {
148+ integrationIsNull = true ;
149+ expect ( mockPushActivity ) . toHaveBeenCalledTimes ( 0 ) ;
150+ expect ( mockLoggerWarn ) . toHaveBeenCalledTimes ( 0 ) ;
151+
152+ // tslint:disable-next-line: no-void-expression
153+ const profiler = renderHook ( ( ) => useProfiler ( 'Example' ) ) ;
154+ expect ( mockPopActivity ) . toHaveBeenCalledTimes ( 0 ) ;
155+ expect ( mockPushActivity ) . toHaveBeenCalledTimes ( 0 ) ;
156+
157+ expect ( mockLoggerWarn ) . toHaveBeenCalledTimes ( 1 ) ;
158+
159+ profiler . unmount ( ) ;
160+ expect ( mockPopActivity ) . toHaveBeenCalledTimes ( 0 ) ;
161+ } ) ;
98162} ) ;
0 commit comments