Skip to content

Commit

Permalink
test: file test (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxiaoqi authored Oct 28, 2024
1 parent c90908a commit 9551a3d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/file.spec.ts
Original file line number Diff line number Diff line change
@@ -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!');
});
});
});

0 comments on commit 9551a3d

Please sign in to comment.