Skip to content

Commit 3840964

Browse files
committed
improve tests
1 parent 568bc7c commit 3840964

File tree

1 file changed

+14
-36
lines changed

1 file changed

+14
-36
lines changed

Diff for: packages/sveltekit/test/client/vendor/lookUpCache.test.ts

+14-36
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,45 @@
11
import * as utils from '@sentry/utils';
2+
import { JSDOM } from 'jsdom';
23
import { vi } from 'vitest';
34

45
import { isRequestCached } from '../../../src/client/vendor/lookUpCache';
56

6-
let scriptElement: {
7-
textContent: string;
8-
getAttribute: (name: string) => string | null;
9-
} | null;
7+
globalThis.document = new JSDOM().window.document;
108

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);
1411

1512
describe('isRequestCached', () => {
1613
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>';
2116

2217
expect(isRequestCached('/api/todos/1', undefined)).toBe(true);
2318
});
2419

2520
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 = '';
2722

2823
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
2924
});
3025

3126
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>';
3629

3730
expect(isRequestCached('/api/todos/1', undefined)).toBe(true);
3831
});
3932

4033
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>';
5436

5537
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
5638
});
5739

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>';
6543

6644
expect(isRequestCached('/api/todos/1', undefined)).toBe(false);
6745
});

0 commit comments

Comments
 (0)