Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Jun 23, 2023
1 parent 568bc7c commit 3840964
Showing 1 changed file with 14 additions and 36 deletions.
50 changes: 14 additions & 36 deletions packages/sveltekit/test/client/vendor/lookUpCache.test.ts
Original file line number Diff line number Diff line change
@@ -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 =
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1">{"status":200}</script>';

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 =
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="10">{"status":200}</script>';

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 =
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="1">{"status":200}</script>';

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 =
'<script type="application/json" data-sveltekit-fetched data-url="/api/todos/1" data-ttl="notANumber">{"status":200}</script>';

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

0 comments on commit 3840964

Please sign in to comment.