diff --git a/packages/sveltekit/test/client/vendor/lookUpCache.test.ts b/packages/sveltekit/test/client/vendor/lookUpCache.test.ts index 76c44d5cc21b..8be8f0fdac9a 100644 --- a/packages/sveltekit/test/client/vendor/lookUpCache.test.ts +++ b/packages/sveltekit/test/client/vendor/lookUpCache.test.ts @@ -1,67 +1,45 @@ import * as utils from '@sentry/utils'; +import { JSDOM } from 'jsdom'; import { vi } from 'vitest'; import { isRequestCached } from '../../../src/client/vendor/lookUpCache'; -let scriptElement: { - textContent: string; - getAttribute: (name: string) => string | null; -} | null; +globalThis.document = new JSDOM().window.document; -vi.spyOn(utils, 'getDomElement').mockImplementation(() => { - return scriptElement; -}); +vi.useFakeTimers().setSystemTime(new Date('2023-06-22')); +vi.spyOn(performance, 'now').mockReturnValue(1000); describe('isRequestCached', () => { it('should return true if a script tag with the same selector as the constructed request selector is found', () => { - scriptElement = { - textContent: 'test', - getAttribute: () => null, - }; + globalThis.document.body.innerHTML = + ''; expect(isRequestCached('/api/todos/1', undefined)).toBe(true); }); it('should return false if a script with the same selector as the constructed request selector is not found', () => { - scriptElement = null; + globalThis.document.body.innerHTML = ''; expect(isRequestCached('/api/todos/1', undefined)).toBe(false); }); it('should return true if a script with the same selector as the constructed request selector is found and its TTL is valid', () => { - scriptElement = { - textContent: 'test', - getAttribute: () => (performance.now() / 1000 + 1).toString(), - }; + globalThis.document.body.innerHTML = + ''; expect(isRequestCached('/api/todos/1', undefined)).toBe(true); }); it('should return false if a script with the same selector as the constructed request selector is found and its TTL is expired', () => { - scriptElement = { - textContent: 'test', - getAttribute: () => (performance.now() / 1000 - 1).toString(), - }; - - expect(isRequestCached('/api/todos/1', undefined)).toBe(false); - }); - - it("should return false if the TTL is set but can't be parsed", () => { - scriptElement = { - textContent: 'test', - getAttribute: () => 'NotANumber', - }; + globalThis.document.body.innerHTML = + ''; expect(isRequestCached('/api/todos/1', undefined)).toBe(false); }); - it('should return false if an error was thrown turing TTL evaluation', () => { - scriptElement = { - textContent: 'test', - getAttribute: () => { - throw new Error('test'); - }, - }; + it("should return false if the TTL is set but can't be parsed as a number", () => { + globalThis.document.body.innerHTML = + ''; expect(isRequestCached('/api/todos/1', undefined)).toBe(false); });