Skip to content
Merged
Show file tree
Hide file tree
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
59 changes: 59 additions & 0 deletions __tests__/utils/filesystem.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { beforeAll, describe, expect, test } from 'vitest'
import { isFileSystemDirectoryEntry, isFileSystemEntry, isFileSystemFileEntry } from '../../lib/utils/filesystem'

describe('File and Directory API helpers', () => {
describe('Without browser support', () => {
beforeAll(() => {
// @ts-expect-error This is not optional, but we need this removed for testing
delete window.FileSystemEntry
// @ts-expect-error This is not optional, but we need this removed for testing
delete window.FileSystemFileEntry
// @ts-expect-error This is not optional, but we need this removed for testing
delete window.FileSystemDirectoryEntry
})

test('isFileSystemDirectoryEntry', () => {
expect(isFileSystemDirectoryEntry({ })).toBe(false)
})
test('isFileSystemFileEntry', () => {
expect(isFileSystemFileEntry({ })).toBe(false)
})
test('isFileSystemEntry', () => {
expect(isFileSystemEntry({ })).toBe(false)
})
})

describe('With browser support', () => {
beforeAll(() => {
// @ts-expect-error Mocking the class - as of today (2024) neither jsdom nor happy-dom support this
window.FileSystemEntry = class A {}
// @ts-expect-error Mocking the class - as of today (2024) neither jsdom nor happy-dom support this
window.FileSystemFileEntry = class B extends window.FileSystemEntry {}
// @ts-expect-error Mocking the class - as of today (2024) neither jsdom nor happy-dom support this
window.FileSystemDirectoryEntry = class C extends window.FileSystemEntry {}
})

test('isFileSystemDirectoryEntry with other object', () => {
expect(isFileSystemDirectoryEntry({ })).toBe(false)
})
test('isFileSystemFileEntry with other object', () => {
expect(isFileSystemFileEntry({ })).toBe(false)
})
test('isFileSystemEntry with other object', () => {
expect(isFileSystemEntry({ })).toBe(false)
})

test('isFileSystemDirectoryEntry with real entry', () => {
const object = new window.FileSystemDirectoryEntry()
expect(isFileSystemDirectoryEntry(object)).toBe(true)
})
test('isFileSystemFileEntry with real entry', () => {
const object = new window.FileSystemFileEntry()
expect(isFileSystemFileEntry(object)).toBe(true)
})
test('isFileSystemEntry with real entry', () => {
const object = new window.FileSystemEntry()
expect(isFileSystemEntry(object)).toBe(true)
})
})
})
2 changes: 1 addition & 1 deletion lib/utils/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
// Helper to support browser that do not support the API
export const isFileSystemDirectoryEntry = (o: unknown): o is FileSystemDirectoryEntry => 'FileSystemDirectoryEntry' in window && o instanceof FileSystemDirectoryEntry

export const isFileSystemFileEntry = (o: unknown): o is FileSystemFileEntry => 'FilesystemFileEntry' in window && o instanceof FileSystemFileEntry
export const isFileSystemFileEntry = (o: unknown): o is FileSystemFileEntry => 'FileSystemFileEntry' in window && o instanceof FileSystemFileEntry

export const isFileSystemEntry = (o: unknown): o is FileSystemEntry => 'FileSystemEntry' in window && o instanceof FileSystemEntry