Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/ReaderFonts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
47 changes: 47 additions & 0 deletions app/src/ReaderFonts.test.jsx
Original file line number Diff line number Diff line change
@@ -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(<ReaderFonts base="http://reader" device="X4" />)
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])
})
Loading