Skip to content

Commit

Permalink
fix(tests): Add tests for filesystem helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Apr 26, 2024
1 parent 57d8f71 commit 37d269d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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

0 comments on commit 37d269d

Please sign in to comment.