-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5248e47
commit 25727c8
Showing
14 changed files
with
2,424 additions
and
66 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.