test: data router, one render helper, fetchRoutes in every test

This commit is contained in:
lobo committed 2026-09-29 16:15:50 +02:00
1 parent d0ea9e900d
commit 2a22789435
37 files changed
+454 -425

No files matched your search

+8 -6
View File
@@ -1,16 +1,18 @@
// @vitest-environment jsdom
import {describe, expect, it, vi} from 'vitest';
import {describe, expect, it} from 'vitest';
import {refreshSession} from './api.js';
import {viewAs} from './admin.js';
import {fetchRoutes, json} from '../test/fetchRoutes.js';
describe('viewAs', () => {
it('waits for a running refresh before it sends the refresh cookie', async () => {
let finishRefresh;
globalThis.fetch = vi.fn((url) => (url.endsWith('/api/auth/refresh')
? new Promise((resolve) => {
finishRefresh = () => resolve({ok: true, status: 204});
})
: Promise.resolve({ok: true, status: 204})));
fetchRoutes({
'POST /api/auth/refresh': () => new Promise((resolve) => {
finishRefresh = () => resolve(json(204));
}),
'POST /api/auth/view-as': json(204),
});
const refreshing = refreshSession();
const switching = viewAs('USER');
+4 -5
View File
@@ -1,10 +1,11 @@
// @vitest-environment jsdom
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import {ACHIEVEMENT_ICONS, COVER, reportBrokenImage, resetBrokenImageReports} from './brokenImages.js';
import {fetchRoutes, json} from '../test/fetchRoutes.js';
beforeEach(() => {
localStorage.setItem('tbp_session', '1');
globalThis.fetch = vi.fn(async () => ({ok: true, status: 204}));
fetchRoutes({'POST /api/library/broken-images': json(204, {})});
});
afterEach(() => {
@@ -40,9 +41,7 @@ describe('reportBrokenImage', () => {
});
it('ignores a failed request', async () => {
globalThis.fetch = vi.fn(async () => {
throw new TypeError('network');
});
fetchRoutes({'POST /api/library/broken-images': () => { throw new TypeError('network'); }});
expect(() => reportBrokenImage(620, COVER)).not.toThrow();
await Promise.resolve();