ray-template/__test__/hooks/useDomToImage.spec.ts
XiaoDaiGua-Ray 39d2e82439 feat: enhance template layouts, hooks, and icon workflow
Add interactive hook examples, shared Pinia runtime state, and configurable layouts.
Generate typed namespaced icons during development, HMR, and builds.
Refine the UI and establish project comment and design conventions.

Validation: typecheck, ESLint, 181 tests, and production build passed.
2026-11-03 13:17:58 +08:00

71 lines
2.1 KiB
TypeScript

import { toBlob, toPng } from 'html-to-image'
import { useDomToImage } from '../../src/hooks/web/useDomToImage'
vi.mock('html-to-image', () => ({
toSvg: vi.fn(),
toPng: vi.fn(),
toJpeg: vi.fn(),
toBlob: vi.fn(),
toPixelData: vi.fn(),
toCanvas: vi.fn(),
}))
describe('useDomToImage', () => {
afterEach(() => vi.clearAllMocks())
it('infers the configured format and supports typed per-call overrides', async () => {
vi.mocked(toPng).mockResolvedValue('data:image/png;base64,test')
const blob = new Blob(['test'])
vi.mocked(toBlob).mockResolvedValue(blob)
const created = vi.fn()
const element = document.createElement('div')
const { create } = useDomToImage(element, {
imageType: 'png',
created,
})
const png = await create()
const result = await create('blob')
expectTypeOf(png).toEqualTypeOf<string>()
expectTypeOf(result).toEqualTypeOf<Blob | null>()
expect(png).toBe('data:image/png;base64,test')
expect(result).toBe(blob)
expect(created).toHaveBeenLastCalledWith(blob, element)
})
it('rejects with an Error and runs cleanup when the target is missing', async () => {
const failed = vi.fn()
const finished = vi.fn()
const { create } = useDomToImage(null, {
createdError: failed,
finally: finished,
})
await expect(create()).rejects.toThrow('target element is undefined')
expect(failed).toHaveBeenCalledWith(expect.any(Error))
expect(finished).toHaveBeenCalledTimes(1)
expect(toPng).not.toHaveBeenCalled()
})
it('propagates renderer failures and completes cleanup before settling', async () => {
const error = new Error('capture failed')
const finished = vi.fn()
const failed = vi.fn()
vi.mocked(toPng).mockRejectedValue(error)
const created = vi.fn()
const element = document.createElement('div')
const { create } = useDomToImage(element, {
imageType: 'png',
created,
createdError: failed,
finally: finished,
})
await expect(create()).rejects.toThrow('capture failed')
expect(failed).toHaveBeenCalledWith(error)
expect(finished).toHaveBeenCalledTimes(1)
})
})