@@ -6,6 +6,17 @@ import { UNKNOWN_COMPONENT, useProfiler, withProfiler } from '../src/profiler';
6
6
7
7
const mockPushActivity = jest . fn ( ) . mockReturnValue ( 1 ) ;
8
8
const 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
+ } ) ) ;
9
20
10
21
jest . mock ( '@sentry/browser' , ( ) => ( {
11
22
getCurrentHub : ( ) => ( {
@@ -20,7 +31,11 @@ jest.mock('@sentry/browser', () => ({
20
31
public static popActivity : ( ) => void = mockPopActivity ;
21
32
}
22
33
23
- return new MockIntegration ( 'test' ) ;
34
+ if ( ! integrationIsNull ) {
35
+ return new MockIntegration ( 'test' ) ;
36
+ }
37
+
38
+ return null ;
24
39
} ,
25
40
} ) ,
26
41
} ) ) ;
@@ -30,6 +45,8 @@ describe('withProfiler', () => {
30
45
jest . useFakeTimers ( ) ;
31
46
mockPushActivity . mockClear ( ) ;
32
47
mockPopActivity . mockClear ( ) ;
48
+ mockLoggerWarn . mockClear ( ) ;
49
+ integrationIsNull = false ;
33
50
} ) ;
34
51
35
52
it ( 'sets displayName properly' , ( ) => {
@@ -39,6 +56,18 @@ describe('withProfiler', () => {
39
56
expect ( ProfiledComponent . displayName ) . toBe ( 'profiler(TestComponent)' ) ;
40
57
} ) ;
41
58
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
+
42
71
it ( 'popActivity() is called when unmounted' , ( ) => {
43
72
const ProfiledComponent = withProfiler ( ( ) => < h1 > Hello World</ h1 > ) ;
44
73
@@ -63,13 +92,32 @@ describe('withProfiler', () => {
63
92
op : 'react' ,
64
93
} ) ;
65
94
} ) ;
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
+ } ) ;
66
112
} ) ;
67
113
68
114
describe ( 'useProfiler()' , ( ) => {
69
115
beforeEach ( ( ) => {
70
116
jest . useFakeTimers ( ) ;
71
117
mockPushActivity . mockClear ( ) ;
72
118
mockPopActivity . mockClear ( ) ;
119
+ mockLoggerWarn . mockClear ( ) ;
120
+ integrationIsNull = false ;
73
121
} ) ;
74
122
75
123
it ( 'popActivity() is called when unmounted' , ( ) => {
@@ -95,4 +143,20 @@ describe('useProfiler()', () => {
95
143
op : 'react' ,
96
144
} ) ;
97
145
} ) ;
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
+ } ) ;
98
162
} ) ;
0 commit comments