|
1 | 1 | import * as utils from '@sentry/utils'; |
| 2 | +import { JSDOM } from 'jsdom'; |
2 | 3 | import { vi } from 'vitest'; |
3 | 4 |
|
4 | 5 | import { isRequestCached } from '../../../src/client/vendor/lookUpCache'; |
5 | 6 |
|
6 | | -let scriptElement: { |
7 | | - textContent: string; |
8 | | - getAttribute: (name: string) => string | null; |
9 | | -} | null; |
| 7 | +globalThis.document = new JSDOM().window.document; |
10 | 8 |
|
11 | | -vi.spyOn(utils, 'getDomElement').mockImplementation(() => { |
12 | | - return scriptElement; |
13 | | -}); |
| 9 | +vi.useFakeTimers().setSystemTime(new Date('2023-06-22')); |
| 10 | +vi.spyOn(performance, 'now').mockReturnValue(1000); |
14 | 11 |
|
15 | 12 | describe('isRequestCached', () => { |
16 | 13 | it('should return true if a script tag with the same selector as the constructed request selector is found', () => { |
17 | | - scriptElement = { |
18 | | - textContent: 'test', |
19 | | - getAttribute: () => null, |
20 | | - }; |
| 14 | + globalThis.document.body.innerHTML = |
| 15 | + '<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1">{"status":200}</script>'; |
21 | 16 |
|
22 | 17 | expect(isRequestCached('/api/todos/1', undefined)).toBe(true); |
23 | 18 | }); |
24 | 19 |
|
25 | 20 | it('should return false if a script with the same selector as the constructed request selector is not found', () => { |
26 | | - scriptElement = null; |
| 21 | + globalThis.document.body.innerHTML = ''; |
27 | 22 |
|
28 | 23 | expect(isRequestCached('/api/todos/1', undefined)).toBe(false); |
29 | 24 | }); |
30 | 25 |
|
31 | 26 | it('should return true if a script with the same selector as the constructed request selector is found and its TTL is valid', () => { |
32 | | - scriptElement = { |
33 | | - textContent: 'test', |
34 | | - getAttribute: () => (performance.now() / 1000 + 1).toString(), |
35 | | - }; |
| 27 | + globalThis.document.body.innerHTML = |
| 28 | + '<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="10">{"status":200}</script>'; |
36 | 29 |
|
37 | 30 | expect(isRequestCached('/api/todos/1', undefined)).toBe(true); |
38 | 31 | }); |
39 | 32 |
|
40 | 33 | it('should return false if a script with the same selector as the constructed request selector is found and its TTL is expired', () => { |
41 | | - scriptElement = { |
42 | | - textContent: 'test', |
43 | | - getAttribute: () => (performance.now() / 1000 - 1).toString(), |
44 | | - }; |
45 | | - |
46 | | - expect(isRequestCached('/api/todos/1', undefined)).toBe(false); |
47 | | - }); |
48 | | - |
49 | | - it("should return false if the TTL is set but can't be parsed", () => { |
50 | | - scriptElement = { |
51 | | - textContent: 'test', |
52 | | - getAttribute: () => 'NotANumber', |
53 | | - }; |
| 34 | + globalThis.document.body.innerHTML = |
| 35 | + '<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="1">{"status":200}</script>'; |
54 | 36 |
|
55 | 37 | expect(isRequestCached('/api/todos/1', undefined)).toBe(false); |
56 | 38 | }); |
57 | 39 |
|
58 | | - it('should return false if an error was thrown turing TTL evaluation', () => { |
59 | | - scriptElement = { |
60 | | - textContent: 'test', |
61 | | - getAttribute: () => { |
62 | | - throw new Error('test'); |
63 | | - }, |
64 | | - }; |
| 40 | + it("should return false if the TTL is set but can't be parsed as a number", () => { |
| 41 | + globalThis.document.body.innerHTML = |
| 42 | + '<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="notANumber">{"status":200}</script>'; |
65 | 43 |
|
66 | 44 | expect(isRequestCached('/api/todos/1', undefined)).toBe(false); |
67 | 45 | }); |
|
0 commit comments