From 3bdd25e968d1ba93a83f4492e863afa2eb967dce Mon Sep 17 00:00:00 2001 From: neon <18559658+neonsolstice@users.noreply.github.com> Date: Mon, 21 Sep 2026 17:29:36 +0000 Subject: [PATCH] test(client): type the recorded request bodies The new assertions read `apiMock.mock.calls[0][1].body` straight off an `unknown`, which vitest runs happily and `vue-tsc` rejects. Read them through one typed helper instead. --- .../shared/composables/useReaderSettings.test.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/client/src/features/reader/shared/composables/useReaderSettings.test.ts b/client/src/features/reader/shared/composables/useReaderSettings.test.ts index fdd14db43..c48211b7f 100644 --- a/client/src/features/reader/shared/composables/useReaderSettings.test.ts +++ b/client/src/features/reader/shared/composables/useReaderSettings.test.ts @@ -5,6 +5,12 @@ import { CBX_READER_DEFAULTS, READER_GROUP_DEFAULTS, type PdfReaderSettings } fr const apiMock = vi.hoisted(() => vi.fn<(...args: unknown[]) => Promise>()) vi.mock('@/lib/api', () => ({ api: apiMock })) +/** The JSON body of a recorded `api()` call, so an assertion can read what was actually sent. */ +function sentBody(call = 0): Record { + const init = apiMock.mock.calls[call]?.[1] as RequestInit | undefined + return JSON.parse(String(init?.body ?? '{}')) as Record +} + const toastMock = vi.hoisted(() => ({ success: vi.fn<(...args: unknown[]) => unknown>(), })) @@ -353,7 +359,7 @@ describe('useReaderSettings - updateBookSettings', () => { s.updateBookSettings({ fontSize: 22 } as never) expect(apiMock).toHaveBeenCalledWith(`/api/v1/reader/preferences/${BOOK_FILE_ID}`, expect.objectContaining({ method: 'PATCH' })) - expect(JSON.parse(apiMock.mock.calls[0][1].body)).toEqual({ set: { fontSize: 22 } }) + expect(sentBody()).toEqual({ set: { fontSize: 22 } }) }) it('sends nothing when the patch is empty, because the server rejects an empty body', () => { @@ -433,10 +439,9 @@ describe('useReaderSettings - updateDefaultSettings', () => { s.updateDefaultSettings({ fontSize: 18 } as never) expect(apiMock).toHaveBeenCalledWith('/api/v1/reader/defaults/epub', expect.objectContaining({ method: 'PATCH' })) - const body = JSON.parse(apiMock.mock.calls[0][1].body) + const body = sentBody() expect(body).toEqual({ set: { fontSize: 18 } }) // The whole point: nothing another client owns rides along and overwrites it. - expect(body.set.themeName).toBeUndefined() expect(body.settings).toBeUndefined() }) @@ -662,7 +667,7 @@ describe('useReaderDefaultSettings - update', () => { s.update({ fontSize: 22 } as never) expect(apiMock).toHaveBeenCalledWith('/api/v1/reader/defaults/epub', expect.objectContaining({ method: 'PATCH' })) - expect(JSON.parse(apiMock.mock.calls[0][1].body)).toEqual({ set: { fontSize: 22 } }) + expect(sentBody()).toEqual({ set: { fontSize: 22 } }) }) it('sends nothing when the patch is empty', () => {