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