|
1 | 1 | import { addGlobalEventProcessor, init as browserInitRaw, SDK_VERSION } from '@sentry/browser';
|
| 2 | +import { EventProcessor } from '@sentry/types'; |
2 | 3 |
|
3 |
| -import { detectAndReportSvelteKit, init as svelteInit } from '../src/sdk'; |
| 4 | +import { detectAndReportSvelteKit, init as svelteInit, isSvelteKitApp } from '../src/sdk'; |
4 | 5 |
|
5 | 6 | const browserInit = browserInitRaw as jest.Mock;
|
6 | 7 | const addGlobalEventProcessorFunction = addGlobalEventProcessor as jest.Mock;
|
| 8 | +let passedEventProcessor: EventProcessor | undefined; |
| 9 | +addGlobalEventProcessorFunction.mockImplementation(proc => { |
| 10 | + passedEventProcessor = proc; |
| 11 | +}); |
7 | 12 |
|
8 | 13 | jest.mock('@sentry/browser');
|
9 | 14 |
|
@@ -33,23 +38,52 @@ describe('Initialize Svelte SDk', () => {
|
33 | 38 | });
|
34 | 39 |
|
35 | 40 | describe('detectAndReportSvelteKit()', () => {
|
| 41 | + const originalHtmlBody = document.body.innerHTML; |
36 | 42 | afterEach(() => {
|
37 | 43 | jest.clearAllMocks();
|
| 44 | + document.body.innerHTML = originalHtmlBody; |
| 45 | + passedEventProcessor = undefined; |
38 | 46 | });
|
39 | 47 |
|
40 |
| - it('detects SvelteKit correctly and adds a global event processor', async () => { |
41 |
| - document.body.innerHTML += '<div id="svelte-announcer">Navigated to Home</div>'; |
42 |
| - |
43 |
| - await detectAndReportSvelteKit(); |
| 48 | + it('registers a global event processor', async () => { |
| 49 | + detectAndReportSvelteKit(); |
44 | 50 |
|
45 | 51 | expect(addGlobalEventProcessorFunction).toHaveBeenCalledTimes(1);
|
| 52 | + expect(passedEventProcessor?.id).toEqual('svelteKitProcessor'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('adds "SvelteKit" as a module to the event, if SvelteKit was detected', () => { |
| 56 | + document.body.innerHTML += '<div id="svelte-announcer">Home</div>'; |
| 57 | + detectAndReportSvelteKit(); |
| 58 | + |
| 59 | + const processedEvent = passedEventProcessor && passedEventProcessor({} as unknown as any, {}); |
| 60 | + |
| 61 | + expect(processedEvent).toBeDefined(); |
| 62 | + expect(processedEvent).toEqual({ modules: { svelteKit: '1.0' } }); |
46 | 63 | });
|
47 | 64 |
|
48 |
| - it("doesn't add global event processor, if SvelteKit was not detected", async () => { |
| 65 | + it("doesn't add anything to the event, if SvelteKit was not detected", () => { |
49 | 66 | document.body.innerHTML = '';
|
| 67 | + detectAndReportSvelteKit(); |
50 | 68 |
|
51 |
| - await detectAndReportSvelteKit(); |
| 69 | + const processedEvent = passedEventProcessor && passedEventProcessor({} as unknown as any, {}); |
52 | 70 |
|
53 |
| - expect(addGlobalEventProcessorFunction).not.toHaveBeenCalled(); |
| 71 | + expect(processedEvent).toBeDefined(); |
| 72 | + expect(processedEvent).toEqual({}); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('isSvelteKitApp()', () => { |
| 76 | + it('returns true if the svelte-announcer div is present', () => { |
| 77 | + document.body.innerHTML += '<div id="svelte-announcer">Home</div>'; |
| 78 | + expect(isSvelteKitApp()).toBe(true); |
| 79 | + }); |
| 80 | + it('returns false if the svelte-announcer div is not present (but similar elements)', () => { |
| 81 | + document.body.innerHTML += '<div id="svelte-something">Home</div>'; |
| 82 | + expect(isSvelteKitApp()).toBe(false); |
| 83 | + }); |
| 84 | + it('returns false if no div is present', () => { |
| 85 | + document.body.innerHTML = ''; |
| 86 | + expect(isSvelteKitApp()).toBe(false); |
| 87 | + }); |
54 | 88 | });
|
55 | 89 | });
|
0 commit comments