mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-20 10:13:35 +08:00
Share workspace packages between Ray Admin and the interactive docs. BREAKING CHANGE: v6 is a new major line. v5 stays on legacy/v5.
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import { fileToBase64 } from '../../apps/admin/src/utils/files'
|
|
|
|
it('encodes a file with its MIME type and preserves raw byte values', async () => {
|
|
const file = new File([new Uint8Array([0, 128, 255])], 'binary', {
|
|
type: 'application/octet-stream',
|
|
})
|
|
|
|
await expect(fileToBase64(file)).resolves.toBe(
|
|
'data:application/octet-stream;base64,AID/',
|
|
)
|
|
})
|
|
|
|
it.each(['error', 'abort'] as const)(
|
|
'rejects when FileReader emits %s',
|
|
async (event) => {
|
|
const error = new DOMException('failed', 'NotReadableError')
|
|
const read = vi
|
|
.spyOn(FileReader.prototype, 'readAsDataURL')
|
|
.mockImplementation(function (this: FileReader) {
|
|
Object.defineProperty(this, 'error', { value: error })
|
|
this.dispatchEvent(new Event(event))
|
|
})
|
|
|
|
try {
|
|
const promise = fileToBase64(new File([], 'test'))
|
|
|
|
if (event === 'error') await expect(promise).rejects.toBe(error)
|
|
else await expect(promise).rejects.toMatchObject({ name: 'AbortError' })
|
|
} finally {
|
|
read.mockRestore()
|
|
}
|
|
},
|
|
)
|