ray-template/__test__/hooks/usePrint.spec.ts
XiaoDaiGua-Ray 403fdd589a feat!: rebuild Ray Template as a monorepo
Share workspace packages between Ray Admin and the interactive docs.

BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
2026-11-04 14:01:28 +08:00

45 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import print from 'print-js'
import { usePrint } from '../../apps/admin/src/hooks/web/usePrint'
vi.mock('print-js', () => ({ default: vi.fn() }))
beforeEach(() => vi.clearAllMocks())
it('skips a missing target and prints the latest element in html mode', () => {
const target = ref<HTMLElement>()
const printer = usePrint(target)
printer.print()
expect(print).not.toHaveBeenCalled()
target.value = document.createElement('div')
printer.print()
expect(print).toHaveBeenCalledExactlyOnceWith({
printable: target.value,
type: 'html',
})
})
it('uses the caller’s print mode but never accepts a second printable value', () => {
const printer = usePrint('/report.pdf', {
printable: '/wrong.pdf',
type: 'pdf',
} as never)
printer.print()
expect(print).toHaveBeenCalledExactlyOnceWith({
printable: '/report.pdf',
type: 'pdf',
})
})
it('converts binary PDF and image targets to the payloads print-js expects', async () => {
await usePrint(new Uint8ClampedArray([1, 2, 3])).print()
await usePrint(new Blob(['abc'], { type: 'image/png' })).print()
expect(vi.mocked(print).mock.calls).toEqual([
[{ printable: 'AQID', base64: true, type: 'pdf' }],
[{ printable: 'data:image/png;base64,YWJj', base64: false, type: 'image' }],
])
})