|
1 |
| -// TODO |
2 |
| -$console.warn('Not implemented'); |
| 1 | +import { afterEach, expect, test } from 'bun:test'; |
| 2 | +import { spyOn } from 'bun:test'; |
| 3 | +import { describe } from 'bun:test'; |
| 4 | +import { performanceSpy } from '@maxmilton/test-utils/spy'; |
| 5 | +import { reset } from '../setup'; |
| 6 | + |
| 7 | +// Completely reset DOM and global state between tests |
| 8 | +afterEach(reset); |
| 9 | + |
| 10 | +const MODULE_PATH = Bun.resolveSync('./dist/sw.js', '.'); |
| 11 | + |
| 12 | +async function load(noMocks?: boolean) { |
| 13 | + if (!noMocks) { |
| 14 | + const originalFetch = global.fetch; |
| 15 | + |
| 16 | + // @ts-expect-error - monkey patching fetch for testing |
| 17 | + global.fetch = (input, init) => { |
| 18 | + if (input === 'themes.json') { |
| 19 | + return Promise.resolve({ |
| 20 | + json: () => Promise.resolve({}), |
| 21 | + }); |
| 22 | + } |
| 23 | + return originalFetch(input, init); |
| 24 | + }; |
| 25 | + |
| 26 | + chrome.runtime.onInstalled.addListener = (callback) => { |
| 27 | + callback({ reason: 'install' as chrome.runtime.OnInstalledReason.INSTALL }); |
| 28 | + }; |
| 29 | + chrome.runtime.onStartup.addListener = (callback) => { |
| 30 | + callback(); |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + Loader.registry.delete(MODULE_PATH); |
| 35 | + await import(MODULE_PATH); |
| 36 | + await happyDOM.waitUntilComplete(); |
| 37 | +} |
| 38 | + |
| 39 | +test('does not call any console methods', async () => { |
| 40 | + expect.assertions(1); |
| 41 | + await load(); |
| 42 | + expect(happyDOM.virtualConsolePrinter.read()).toBeArrayOfSize(0); |
| 43 | +}); |
| 44 | + |
| 45 | +test('does not call any performance methods', async () => { |
| 46 | + expect.hasAssertions(); // variable number of assertions |
| 47 | + const check = performanceSpy(); |
| 48 | + await load(); |
| 49 | + check(); |
| 50 | +}); |
| 51 | + |
| 52 | +test('does not call fetch() except themes.json', async () => { |
| 53 | + expect.assertions(1); |
| 54 | + const spy = spyOn(global, 'fetch'); |
| 55 | + await load(); |
| 56 | + expect(spy).not.toHaveBeenCalled(); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('onInstalled', () => { |
| 60 | + test('has listener', async () => { |
| 61 | + expect.assertions(1); |
| 62 | + const spy = spyOn(chrome.runtime.onInstalled, 'addListener'); |
| 63 | + await load(true); |
| 64 | + expect(spy).toHaveBeenCalledTimes(1); |
| 65 | + }); |
| 66 | + |
| 67 | + // TODO: Test with various settings. |
| 68 | + // TODO: More and better assertions. |
| 69 | +}); |
| 70 | + |
| 71 | +describe('onStartup', () => { |
| 72 | + test('has listener', async () => { |
| 73 | + expect.assertions(1); |
| 74 | + const spy = spyOn(chrome.runtime.onStartup, 'addListener'); |
| 75 | + await load(true); |
| 76 | + expect(spy).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + // TODO: Test with various settings, especially sync enabled/disabled. |
| 80 | + // TODO: More and better assertions. |
| 81 | +}); |
0 commit comments