Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfriesen committed Mar 4, 2024
1 parent 5248e47 commit 25727c8
Show file tree
Hide file tree
Showing 14 changed files with 2,424 additions and 66 deletions.
2,027 changes: 1,987 additions & 40 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"File"
],
"scripts": {
"test": "vitest run",
"clean": "rm -rf dist/*",
"prebuild": "npm run clean",
"build": "npm run build:lib && npm run build:types && npm run build:example",
Expand All @@ -33,7 +34,10 @@
"homepage": "https://github.com/alexfriesen/data-transfer-helper#readme",
"devDependencies": {
"@types/wicg-file-system-access": "^2023.10.1",
"@vitest/coverage-v8": "^1.3.1",
"esbuild": "^0.19.5",
"typescript": "^5.0.0"
"jsdom": "^24.0.0",
"typescript": "^5.0.0",
"vitest": "^1.3.1"
}
}
137 changes: 137 additions & 0 deletions src/filesystem.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { describe, it, expect } from "vitest";

import {
isFileSystemDirectory,
isFileSystemDirectoryHandle,
isFileSystemFile,
isFileSystemFileHanle,
parseDataTransferItem,
readFileSystemEntryAsync,
readFileSystemHandlesAsync,
} from "./filesystem";
import {
createMockDataTransferItem,
createMockFileSystemDirectoryEntry,
createMockFileSystemDirectoryHandle,
createMockFileSystemFileEntry,
createMockFileSystemFileHandle,
} from "./utils/testing";

describe("Filesystem", () => {
describe("isFileSystemFileHanle", () => {
it("should return true if the given FileSystemHandle is a file", () => {
const handle = { kind: "file" } as FileSystemHandle;
expect(isFileSystemFileHanle(handle)).toBeTruthy();
});

it("should return false if the given FileSystemHandle is a directory", () => {
const handle = { kind: "directory" } as FileSystemHandle;
expect(isFileSystemFileHanle(handle)).toBeFalsy();
});
});

describe("isFileSystemDirectoryHandle", () => {
it("should return true if the given FileSystemHandle is a directory", () => {
const handle = { kind: "directory" } as FileSystemHandle;
expect(isFileSystemDirectoryHandle(handle)).toBeTruthy();
});

it("should return false if the given FileSystemHandle is a file", () => {
const handle = { kind: "file" } as FileSystemHandle;
expect(isFileSystemDirectoryHandle(handle)).toBeFalsy();
});
});

describe("isFileSystemFile", () => {
it("should return true if the given FileSystemEntry is a file", () => {
const entry = { isFile: true } as FileSystemEntry;
expect(isFileSystemFile(entry)).toBeTruthy();
});

it("should return false if the given FileSystemEntry is a directory", () => {
const entry = { isDirectory: true } as FileSystemEntry;
expect(isFileSystemFile(entry)).toBeFalsy();
});
});

describe("isFileSystemDirectory", () => {
it("should return true if the given FileSystemEntry is a directory", () => {
const entry = { isDirectory: true } as FileSystemEntry;
expect(isFileSystemDirectory(entry)).toBeTruthy();
});

it("should return false if the given FileSystemEntry is a file", () => {
const entry = { isFile: true } as FileSystemEntry;
expect(isFileSystemDirectory(entry)).toBeFalsy();
});
});

describe("parseDataTransferItem", () => {
it("should return a File if the DataTransferItem is a File", async () => {
const file = new File([""], "file.txt");
const dataTransferItem = createMockDataTransferItem(file);
const result = await parseDataTransferItem(dataTransferItem);
expect(result).toStrictEqual([file]);
});

it("should return an empty array if no files are passed", async () => {
const dataTransferItem = createMockDataTransferItem(null);
const result = await parseDataTransferItem(dataTransferItem);
expect(result).toStrictEqual([]);
});
});

describe("readFileSystemEntryAsync", () => {
it("should return a list of files", async () => {
const file = new File([""], "file.txt");
const file2 = new File([""], "file2.txt");
const entry = createMockFileSystemDirectoryEntry("test", [
createMockFileSystemFileEntry(file),
createMockFileSystemFileEntry(file2),
]);

const result = await readFileSystemEntryAsync(entry);
expect(result).toStrictEqual([file, file2]);
});

it("should return a list of files and apply directory path", async () => {
const file = new File(["123"], "file.txt");
const entry = createMockFileSystemDirectoryEntry("test", [
createMockFileSystemFileEntry(file),
]);

const result = await readFileSystemEntryAsync(entry, {
addDirectoryName: true,
});
expect(result).toStrictEqual([file]);
expect(result[0].name).toEqual("test/file.txt");
});
});

describe("readFileSystemHandlesAsync", () => {
it("should return a list of files", async () => {
const file = new File([""], "file.txt");
const file2 = new File([""], "file2.txt");
const handle = createMockFileSystemDirectoryHandle("test", [
createMockFileSystemFileHandle(file),
createMockFileSystemFileHandle(file2),
]);

const result = await readFileSystemHandlesAsync(handle);
expect(result).toStrictEqual([file, file2]);
});

it("should return a list of files and apply directory path", async () => {
const file = new File(["123"], "file.txt");
const handle = createMockFileSystemDirectoryHandle("test", [
createMockFileSystemFileHandle(file),
]);

const result = await readFileSystemHandlesAsync(handle, {
addDirectoryName: true,
});
expect(result).toStrictEqual([file]);
expect(result[0].name).toEqual("test/file.txt");
});
});
});
14 changes: 6 additions & 8 deletions src/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Options } from "./options";
import { checkFile, extendFile } from "./utils/file";
import { appendPath, generatorToArray } from "./utils/utils";
import {
appendPath,
checkFile,
extendFile,
generatorToArray,
supportsFileSystemAccessAPI,
supportsWebkitGetAsEntry,
} from "./utils";
import { Options } from "./options";
} from "./utils/suppot";

export const parseDataTransferItem = async (
item: DataTransferItem,
Expand Down Expand Up @@ -39,7 +37,7 @@ export const parseDataTransferItem = async (
return [];
};

const readFileSystemHandlesAsync = async (
export const readFileSystemHandlesAsync = async (
entry: FileSystemHandle,
options?: Options
): Promise<File[]> =>
Expand All @@ -65,7 +63,7 @@ async function* readFileSystemHandleRecursively(
}
}

const readFileSystemEntryAsync = async (
export const readFileSystemEntryAsync = async (
entry: FileSystemEntry,
options?: Options
): Promise<File[]> =>
Expand Down
22 changes: 22 additions & 0 deletions src/filters/dot-files.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect } from "vitest";
import { dotFileFilter } from "./dot-files";

describe("dotFileFilter", () => {
it("should return true if the file name does not start with a dot", () => {
const file = new File([""], "file.txt");
const result = dotFileFilter(file);
expect(result).toStrictEqual(true);
});

it("should return false if the file name starts with a dot", () => {
const file = new File([""], ".file.txt");
const result = dotFileFilter(file);
expect(result).toStrictEqual(false);
});

it("should return false if the file name starts with a dot and contains a path", () => {
const file = new File([""], "test/.file.txt");
const result = dotFileFilter(file);
expect(result).toStrictEqual(false);
});
});
59 changes: 59 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { describe, it, expect } from "vitest";

import { parseDataTransferFiles, parseFilesFromEvent } from "./index.ts";
import {
createMockDataTransfer,
createMockDataTransferEvent,
createMockDataTransferList,
} from "./utils/testing";

describe("parseDataTransferFiles", () => {
it("should return an empty array if the list is undefined", async () => {
const result = await parseDataTransferFiles(undefined);
expect(result).toStrictEqual([]);
});

it("should return an empty array if the list is empty", async () => {
const result = await parseDataTransferFiles(
[] as unknown as DataTransferItemList
);
expect(result).toStrictEqual([]);
});

it("should return an empty array if the list contains no files", async () => {
const result = await parseDataTransferFiles([
{ kind: "not file" },
] as unknown as DataTransferItemList);
expect(result).toStrictEqual([]);
});

it("should return a list of files", async () => {
const file = new File([""], "file.txt");
const list = createMockDataTransferList([file]);
const result = await parseDataTransferFiles(list);
expect(result).toStrictEqual([file]);
});
});

describe("parseFilesFromEvent", () => {
it("should return an empty array if the event has no dataTransfer", async () => {
const event = createMockDataTransferEvent("drop", []);
const result = await parseFilesFromEvent(event);
expect(result).toStrictEqual([]);
});

it("should return an empty array if the dataTransfer has no items", async () => {
const dataTransfer = createMockDataTransfer([]);
const event = new Event("drop");
Object.assign(event, { dataTransfer });
const result = await parseFilesFromEvent(event as DragEvent);
expect(result).toStrictEqual([]);
});

it("should return a list of files", async () => {
const file = new File([""], "file.txt");
const event = createMockDataTransferEvent("drop", [file]);
const result = await parseFilesFromEvent(event);
expect(result).toStrictEqual([file]);
});
});
63 changes: 63 additions & 0 deletions src/utils/file.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, it, expect } from "vitest";
import { checkFile, extendFile, renameFile } from "./file";
import { dotFileFilter } from "../filters/dot-files";

describe("extendFile", () => {
it("should return null if the file is null", () => {
const result = extendFile(null);
expect(result).toStrictEqual(null);
});

it("should return the file if no options are passed", () => {
const file = new File([""], "file.txt");
const result = extendFile(file);
expect(result).toStrictEqual(file);
});

it("should return the file with the directory name if the option is set", () => {
const file = new File([""], "file.txt");
const options = { addDirectoryName: true, baseDirectory: "test/" };
const result = extendFile(file, options);
expect(result?.name).toStrictEqual("test/file.txt");
});
});

describe("renameFile", () => {
it("should return a new file with the given name", () => {
const file = new File([""], "file.txt");
const result = renameFile(file, "new.txt");
expect(result.name).toStrictEqual("new.txt");
expect(result.type).toStrictEqual(file.type);
expect(result.lastModified).toStrictEqual(file.lastModified);
expect(result.size).toStrictEqual(file.size);
expect(result.arrayBuffer).toStrictEqual(file.arrayBuffer);
});
});

describe("checkFile", () => {
it("should return true if no filters are passed", () => {
const file = new File([""], "file.txt");
const result = checkFile(file);
expect(result).toStrictEqual(true);
});

it("should return true if the file passes all filters", () => {
const file = new File([""], "file.txt");
const filter = () => true;
const result = checkFile(file, { filters: [filter] });
expect(result).toStrictEqual(true);
});

it("should return false if the file does not pass a filter", () => {
const file = new File([""], "file.txt");
const filter = () => false;
const result = checkFile(file, { filters: [filter] });
expect(result).toStrictEqual(false);
});

it("should return false if the file does not pass a filter", () => {
const file = new File([""], ".file.txt");
const result = checkFile(file, { filters: [dotFileFilter] });
expect(result).toStrictEqual(false);
});
});
18 changes: 1 addition & 17 deletions src/utils.ts → src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Options } from "./options";
import { Options } from "../options";

export const renameFile = (file: File, name: string): File => {
const renamed = new File([file], name, {
Expand All @@ -8,17 +8,6 @@ export const renameFile = (file: File, name: string): File => {
return renamed;
};

export async function generatorToArray<T>(
generator: AsyncIterable<T>
): Promise<T[]> {
const items: T[] = [];
for await (const item of generator) items.push(item);
return items;
}

export const appendPath = (base: string, path: string): string =>
base + path + "/";

export const extendFile = (file: File | null, options?: Options) => {
if (file && options?.addDirectoryName) {
const filename = options.baseDirectory + file.name;
Expand All @@ -37,8 +26,3 @@ export const checkFile = (file: File, options?: Options) => {

return true;
};

export const supportsFileSystemAccessAPI =
"getAsFileSystemHandle" in DataTransferItem.prototype;
export const supportsWebkitGetAsEntry =
"webkitGetAsEntry" in DataTransferItem.prototype;
5 changes: 5 additions & 0 deletions src/utils/suppot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const supportsFileSystemAccessAPI =
"getAsFileSystemHandle" in DataTransferItem.prototype;

export const supportsWebkitGetAsEntry =
"webkitGetAsEntry" in DataTransferItem.prototype;
Loading

0 comments on commit 25727c8

Please sign in to comment.