Skip to content

Commit 598c5bb

Browse files
committed
chore(react): Update comments and test for Profiler
1 parent d413dad commit 598c5bb

File tree

4 files changed

+341
-40
lines changed

4 files changed

+341
-40
lines changed

packages/react/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@types/hoist-non-react-statics": "^3.3.1",
3333
"@types/react": "^16.9.35",
3434
"jest": "^24.7.1",
35+
"jsdom": "^16.2.2",
3536
"npm-run-all": "^4.1.2",
3637
"prettier": "^1.17.0",
3738
"prettier-check": "^2.0.0",

packages/react/src/profiler.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function afterNextFrame(callback: Function): void {
3939
timeout = window.setTimeout(done, 100);
4040
}
4141

42+
/**
43+
* getInitActivity pushes activity based on React component mount
44+
* @param name displayName of component that started activity
45+
*/
4246
const getInitActivity = (name: string): number | null => {
4347
const tracingIntegration = getCurrentHub().getIntegration(TRACING_GETTER);
4448

@@ -68,10 +72,13 @@ class Profiler extends React.Component<ProfilerProps> {
6872
this.activity = getInitActivity(this.props.name);
6973
}
7074

75+
// If a component mounted, we can finish the mount activity.
7176
public componentDidMount(): void {
7277
afterNextFrame(this.finishProfile);
7378
}
7479

80+
// Sometimes a component will unmount first, so we make
81+
// sure to also finish the mount activity here.
7582
public componentWillUnmount(): void {
7683
afterNextFrame(this.finishProfile);
7784
}
@@ -94,8 +101,15 @@ class Profiler extends React.Component<ProfilerProps> {
94101
}
95102
}
96103

97-
function withProfiler<P extends object>(WrappedComponent: React.ComponentType<P>): React.FC<P> {
98-
const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
104+
/**
105+
* withProfiler is a higher order component that wraps a
106+
* component in a {@link Profiler} component.
107+
*
108+
* @param WrappedComponent component that is wrapped by Profiler
109+
* @param name displayName of component being profiled
110+
*/
111+
function withProfiler<P extends object>(WrappedComponent: React.ComponentType<P>, name?: string): React.FC<P> {
112+
const componentDisplayName = name || WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
99113

100114
const Wrapped: React.FC<P> = (props: P) => (
101115
<Profiler name={componentDisplayName}>

packages/react/test/profiler.test.tsx

+65-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import { UNKNOWN_COMPONENT, useProfiler, withProfiler } from '../src/profiler';
66

77
const mockPushActivity = jest.fn().mockReturnValue(1);
88
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+
}));
920

1021
jest.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

68114
describe('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

Comments
 (0)