|
1 | | -import { render } from '@testing-library/react'; |
2 | 1 | import * as React from 'react'; |
| 2 | +import { create, ReactTestInstance } from 'react-test-renderer'; |
3 | 3 |
|
4 | 4 | import { withProfiler } from '../src/profiler'; |
5 | 5 |
|
| 6 | +const mockPushActivity = jest.fn().mockReturnValue(1); |
| 7 | +const mockPopActivity = jest.fn(); |
| 8 | + |
| 9 | +jest.mock('@sentry/browser', () => ({ |
| 10 | + getCurrentHub: () => ({ |
| 11 | + getIntegration: (_: string) => { |
| 12 | + class MockIntegration { |
| 13 | + public constructor(name: string) { |
| 14 | + this.name = name; |
| 15 | + } |
| 16 | + public name: string; |
| 17 | + public setupOnce: () => void = jest.fn(); |
| 18 | + public static pushActivity: () => void = mockPushActivity; |
| 19 | + public static popActivity: () => void = mockPopActivity; |
| 20 | + } |
| 21 | + |
| 22 | + return new MockIntegration('test'); |
| 23 | + }, |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
6 | 27 | describe('withProfiler', () => { |
7 | | - it("sets displayName properly", () => { |
8 | | - // tslint:disable-next-line: variable-name |
9 | | - const HelloWorld = () => <h1>Hello World</h1>; |
10 | | - |
11 | | - // tslint:disable-next-line: variable-name |
12 | | - const ProfiledComponent = withProfiler(HelloWorld); |
13 | | - expect(ProfiledComponent.displayName).toBe("profiler(HelloWorld)") |
14 | | - }) |
| 28 | + it('sets displayName properly', () => { |
| 29 | + const TestComponent = () => <h1>Hello World</h1>; |
| 30 | + |
| 31 | + const ProfiledComponent = withProfiler(TestComponent); |
| 32 | + expect(ProfiledComponent.displayName).toBe('profiler(TestComponent)'); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('Tracing Integration', () => { |
| 36 | + beforeEach(() => { |
| 37 | + mockPushActivity.mockClear(); |
| 38 | + mockPopActivity.mockClear(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('is called with pushActivity() when mounted', () => { |
| 42 | + const ProfiledComponent = withProfiler(() => <h1>Hello World</h1>); |
| 43 | + |
| 44 | + expect(mockPushActivity).toHaveBeenCalledTimes(0); |
| 45 | + create(<ProfiledComponent />); |
| 46 | + expect(mockPushActivity).toHaveBeenCalledTimes(1); |
| 47 | + }); |
| 48 | + |
| 49 | + it('is called with popActivity() when unmounted', () => { |
| 50 | + const ProfiledComponent = withProfiler(() => <h1>Hello World</h1>); |
| 51 | + |
| 52 | + expect(mockPopActivity).toHaveBeenCalledTimes(0); |
| 53 | + |
| 54 | + const profiler = create(<ProfiledComponent />); |
| 55 | + profiler.unmount(); |
| 56 | + |
| 57 | + expect(mockPopActivity).toHaveBeenCalledTimes(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it('calls finishProfile() when unmounting', () => { |
| 61 | + const ProfiledComponent = withProfiler(() => <h1>Hello World</h1>); |
| 62 | + |
| 63 | + const mockFinishProfile = jest.fn(); |
| 64 | + const profiler = create(<ProfiledComponent />); |
| 65 | + |
| 66 | + const instance = profiler.getInstance() as ReactTestInstance & { finishProfile(): void }; |
| 67 | + instance.finishProfile = mockFinishProfile; |
| 68 | + |
| 69 | + expect(mockFinishProfile).toHaveBeenCalledTimes(0); |
| 70 | + profiler.unmount(); |
| 71 | + expect(mockFinishProfile).toHaveBeenCalledTimes(1); |
| 72 | + }); |
| 73 | + }); |
15 | 74 | }); |
0 commit comments