From 9551a3df215fbb14d6a209e08024e1faee59ef9d Mon Sep 17 00:00:00 2001 From: lzxiaoqi <89337957+lzxiaoqi@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:06:28 +0800 Subject: [PATCH] test: file test (#7) --- tests/file.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/file.spec.ts diff --git a/tests/file.spec.ts b/tests/file.spec.ts new file mode 100644 index 0000000..062f296 --- /dev/null +++ b/tests/file.spec.ts @@ -0,0 +1,36 @@ +import { describe, it, expect, beforeEach } from 'vitest'; +import { toDataURL, toText, toArrayBuffer } from '../src'; + +describe('file', () => { + let file: File; + + beforeEach(() => { + const blob = new Blob(['Hello, World!'], { type: 'text/plain' }); + file = new File([blob], 'hello.txt', { type: 'text/plain' }); + }); + + describe('toDataURL', () => { + it('should convert file to data URL', async () => { + const dataURL = await toDataURL(file); + expect(dataURL).toMatch(/^data:text\/plain;base64,/); + }); + }); + + describe('toText', () => { + it('should read file as text', async () => { + const text = await toText(file); + expect(text).toBe('Hello, World!'); + }); + }); + + describe('toArrayBuffer', () => { + it('should read file as array buffer', async () => { + const arrayBuffer = await toArrayBuffer(file); + expect(arrayBuffer).toBeInstanceOf(ArrayBuffer); + + const uint8Array = new Uint8Array(arrayBuffer); + const text = new TextDecoder().decode(uint8Array); + expect(text).toBe('Hello, World!'); + }); + }); +}); \ No newline at end of file