|
| 1 | +import { Scope } from '@sentry/hub'; |
| 2 | +import { act, render } from '@testing-library/svelte'; |
| 3 | + |
| 4 | +// linter doesn't like Svelte component imports |
| 5 | +// eslint-disable-next-line import/no-unresolved |
| 6 | +import DummyComponent from './components/Dummy.svelte'; |
| 7 | + |
| 8 | +let returnUndefinedTransaction = false; |
| 9 | + |
| 10 | +const testTransaction: { spans: any[]; startChild: jest.Mock; finish: jest.Mock } = { |
| 11 | + spans: [], |
| 12 | + startChild: jest.fn(), |
| 13 | + finish: jest.fn(), |
| 14 | +}; |
| 15 | +const testUpdateSpan = { finish: jest.fn() }; |
| 16 | +const testInitSpan: any = { |
| 17 | + transaction: testTransaction, |
| 18 | + finish: jest.fn(), |
| 19 | + startChild: jest.fn(), |
| 20 | +}; |
| 21 | + |
| 22 | +jest.mock('@sentry/hub', () => { |
| 23 | + const original = jest.requireActual('@sentry/hub'); |
| 24 | + return { |
| 25 | + ...original, |
| 26 | + getCurrentHub(): { |
| 27 | + getScope(): Scope; |
| 28 | + } { |
| 29 | + return { |
| 30 | + getScope(): any { |
| 31 | + return { |
| 32 | + getTransaction: () => { |
| 33 | + return returnUndefinedTransaction ? undefined : testTransaction; |
| 34 | + }, |
| 35 | + }; |
| 36 | + }, |
| 37 | + }; |
| 38 | + }, |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +describe('Sentry.trackComponent()', () => { |
| 43 | + beforeEach(() => { |
| 44 | + jest.resetAllMocks(); |
| 45 | + testTransaction.spans = []; |
| 46 | + |
| 47 | + testTransaction.startChild.mockImplementation(spanCtx => { |
| 48 | + testTransaction.spans.push(spanCtx); |
| 49 | + return testInitSpan; |
| 50 | + }); |
| 51 | + |
| 52 | + testInitSpan.startChild.mockImplementation((spanCtx: any) => { |
| 53 | + testTransaction.spans.push(spanCtx); |
| 54 | + return testUpdateSpan; |
| 55 | + }); |
| 56 | + |
| 57 | + testInitSpan.finish = jest.fn(); |
| 58 | + testInitSpan.endTimestamp = undefined; |
| 59 | + returnUndefinedTransaction = false; |
| 60 | + }); |
| 61 | + |
| 62 | + it('creates nested init and update spans on component initialization', () => { |
| 63 | + render(DummyComponent, { props: { options: {} } }); |
| 64 | + |
| 65 | + expect(testTransaction.startChild).toHaveBeenCalledWith({ |
| 66 | + description: '<Dummy>', |
| 67 | + op: 'ui.svelte.init', |
| 68 | + }); |
| 69 | + |
| 70 | + expect(testInitSpan.startChild).toHaveBeenCalledWith({ |
| 71 | + description: '<Dummy>', |
| 72 | + op: 'ui.svelte.update', |
| 73 | + }); |
| 74 | + |
| 75 | + expect(testInitSpan.finish).toHaveBeenCalledTimes(1); |
| 76 | + expect(testUpdateSpan.finish).toHaveBeenCalledTimes(1); |
| 77 | + expect(testTransaction.spans.length).toEqual(2); |
| 78 | + }); |
| 79 | + |
| 80 | + it('creates an update span, when the component is updated', async () => { |
| 81 | + // Make the finish() function actually end the initSpan |
| 82 | + testInitSpan.finish.mockImplementation(() => { |
| 83 | + testInitSpan.endTimestamp = new Date().getTime(); |
| 84 | + }); |
| 85 | + |
| 86 | + // first we create the component |
| 87 | + const { component } = render(DummyComponent, { props: { options: {} } }); |
| 88 | + |
| 89 | + // then trigger an update |
| 90 | + // (just changing the trackUpdates prop so that we trigger an update. # |
| 91 | + // The value doesn't do anything here) |
| 92 | + await act(() => component.$set({ options: { trackUpdates: true } })); |
| 93 | + |
| 94 | + // once for init (unimportant here), once for starting the update span |
| 95 | + expect(testTransaction.startChild).toHaveBeenCalledTimes(2); |
| 96 | + expect(testTransaction.startChild).toHaveBeenLastCalledWith({ |
| 97 | + description: '<Dummy>', |
| 98 | + op: 'ui.svelte.update', |
| 99 | + }); |
| 100 | + expect(testTransaction.spans.length).toEqual(3); |
| 101 | + }); |
| 102 | + |
| 103 | + it('only creates init spans if trackUpdates is deactivated', () => { |
| 104 | + render(DummyComponent, { props: { options: { trackUpdates: false } } }); |
| 105 | + |
| 106 | + expect(testTransaction.startChild).toHaveBeenCalledWith({ |
| 107 | + description: '<Dummy>', |
| 108 | + op: 'ui.svelte.init', |
| 109 | + }); |
| 110 | + |
| 111 | + expect(testInitSpan.startChild).not.toHaveBeenCalled(); |
| 112 | + |
| 113 | + expect(testInitSpan.finish).toHaveBeenCalledTimes(1); |
| 114 | + expect(testTransaction.spans.length).toEqual(1); |
| 115 | + }); |
| 116 | + |
| 117 | + it('only creates update spans if trackInit is deactivated', () => { |
| 118 | + render(DummyComponent, { props: { options: { trackInit: false } } }); |
| 119 | + |
| 120 | + expect(testTransaction.startChild).toHaveBeenCalledWith({ |
| 121 | + description: '<Dummy>', |
| 122 | + op: 'ui.svelte.update', |
| 123 | + }); |
| 124 | + |
| 125 | + expect(testInitSpan.startChild).not.toHaveBeenCalled(); |
| 126 | + |
| 127 | + expect(testInitSpan.finish).toHaveBeenCalledTimes(1); |
| 128 | + expect(testTransaction.spans.length).toEqual(1); |
| 129 | + }); |
| 130 | + |
| 131 | + it('creates no spans if trackInit and trackUpdates are deactivated', () => { |
| 132 | + render(DummyComponent, { props: { options: { trackInit: false, trackUpdates: false } } }); |
| 133 | + |
| 134 | + expect(testTransaction.startChild).not.toHaveBeenCalled(); |
| 135 | + expect(testInitSpan.startChild).not.toHaveBeenCalled(); |
| 136 | + expect(testTransaction.spans.length).toEqual(0); |
| 137 | + }); |
| 138 | + |
| 139 | + it('sets a custom component name as a span description if `componentName` is provided', async () => { |
| 140 | + render(DummyComponent, { |
| 141 | + props: { options: { componentName: 'CustomComponentName' } }, |
| 142 | + }); |
| 143 | + |
| 144 | + expect(testTransaction.startChild).toHaveBeenCalledWith({ |
| 145 | + description: '<CustomComponentName>', |
| 146 | + op: 'ui.svelte.init', |
| 147 | + }); |
| 148 | + |
| 149 | + expect(testInitSpan.startChild).toHaveBeenCalledWith({ |
| 150 | + description: '<CustomComponentName>', |
| 151 | + op: 'ui.svelte.update', |
| 152 | + }); |
| 153 | + |
| 154 | + expect(testInitSpan.finish).toHaveBeenCalledTimes(1); |
| 155 | + expect(testUpdateSpan.finish).toHaveBeenCalledTimes(1); |
| 156 | + expect(testTransaction.spans.length).toEqual(2); |
| 157 | + }); |
| 158 | + |
| 159 | + it("doesn't do anything, if there's no ongoing transaction", async () => { |
| 160 | + returnUndefinedTransaction = true; |
| 161 | + |
| 162 | + render(DummyComponent, { |
| 163 | + props: { options: { componentName: 'CustomComponentName' } }, |
| 164 | + }); |
| 165 | + |
| 166 | + expect(testInitSpan.finish).toHaveBeenCalledTimes(0); |
| 167 | + expect(testUpdateSpan.finish).toHaveBeenCalledTimes(0); |
| 168 | + expect(testTransaction.spans.length).toEqual(0); |
| 169 | + }); |
| 170 | + |
| 171 | + it("doesn't record update spans, if there's no ongoing transaction at that time", async () => { |
| 172 | + // Make the finish() function actually end the initSpan |
| 173 | + testInitSpan.finish.mockImplementation(() => { |
| 174 | + testInitSpan.endTimestamp = new Date().getTime(); |
| 175 | + }); |
| 176 | + |
| 177 | + // first we create the component |
| 178 | + const { component } = render(DummyComponent, { props: { options: {} } }); |
| 179 | + |
| 180 | + // then clear the current transaction and trigger an update |
| 181 | + returnUndefinedTransaction = true; |
| 182 | + await act(() => component.$set({ options: { trackUpdates: true } })); |
| 183 | + |
| 184 | + // we should only record the init spans (including the initial update) |
| 185 | + // but not the second update |
| 186 | + expect(testTransaction.startChild).toHaveBeenCalledTimes(1); |
| 187 | + expect(testTransaction.startChild).toHaveBeenLastCalledWith({ |
| 188 | + description: '<Dummy>', |
| 189 | + op: 'ui.svelte.init', |
| 190 | + }); |
| 191 | + expect(testTransaction.spans.length).toEqual(2); |
| 192 | + }); |
| 193 | +}); |
0 commit comments