diff --git a/app/src/ReaderFonts.jsx b/app/src/ReaderFonts.jsx index 78fda58..db547ad 100644 --- a/app/src/ReaderFonts.jsx +++ b/app/src/ReaderFonts.jsx @@ -143,7 +143,7 @@ export default function ReaderFonts({ base, device }) { } notify({ title: `Sent ${family}`, detail: 'Pick it in Settings › Reader › Font Family' }) setTick((t) => t + 1) - }, `Couldn't send ${family}`) + }, `Couldn't send ${font.family}`) const status = (family, fallback) => (job?.family === family ? job.label : fallback) diff --git a/app/src/ReaderFonts.test.jsx b/app/src/ReaderFonts.test.jsx new file mode 100644 index 0000000..b4294c1 --- /dev/null +++ b/app/src/ReaderFonts.test.jsx @@ -0,0 +1,47 @@ +// @vitest-environment jsdom +import { afterEach, expect, it, vi } from 'vitest' +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' +import ReaderFonts from './ReaderFonts.jsx' + +vi.mock('@tauri-apps/api/core', () => ({ + invoke: async (command) => { + if (command === 'list_saved_fonts') return [{ family: 'Literata', files: ['Literata_12.cpfont'] }] + if (command === 'read_font_file') return [67, 80, 70, 79, 78, 84] + throw new Error(`Unexpected command: ${command}`) + }, +})) +vi.mock('./api.js', () => ({ http: (...args) => fetch(...args), isApp: false })) +vi.mock('./ui.jsx', async () => { + const actual = await vi.importActual('./ui.jsx') + return { ...actual, notify: vi.fn() } +}) +import { notify } from './ui.jsx' + +afterEach(() => { cleanup(); vi.unstubAllGlobals(); vi.clearAllMocks() }) + +it.each([false, true])('sends a saved font and reports the result (upload failure: %s)', async (fails) => { + const uploads = [] + vi.stubGlobal('fetch', async (url, options) => { + if (url === 'http://reader/api/fonts') return { ok: true, json: async () => ({ families: [] }) } + if (url === 'http://reader/api/fonts/upload') { + uploads.push(options) + return { ok: !fails, text: async () => 'Reader storage full' } + } + throw new Error(`Unexpected request: ${url}`) + }) + render() + fireEvent.click(await screen.findByRole('button', { name: 'Send' })) + await waitFor(() => expect(notify).toHaveBeenCalledWith(fails + ? { error: true, title: "Couldn't send Literata", detail: 'Reader storage full' } + : { title: 'Sent Literata', detail: 'Pick it in Settings › Reader › Font Family' })) + expect(uploads).toHaveLength(1) + expect(uploads[0].method).toBe('POST') + expect(uploads[0].body.get('family')).toBe('Literata') + const file = uploads[0].body.get('file') + expect(file.name).toBe('Literata_12.cpfont') + expect(await new Promise((resolve) => { + const reader = new FileReader() + reader.onload = () => resolve([...new Uint8Array(reader.result)]) + reader.readAsArrayBuffer(file) + })).toEqual([67, 80, 70, 79, 78, 84]) +})