Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: file test #7

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
haoziqaq marked this conversation as resolved.
Show resolved Hide resolved

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!');
});
});
});
Loading