Skip to content

Commit

Permalink
feat: 🎸 implement .isSameEntry() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 14, 2023
1 parent 9d5669c commit 438806b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/node-to-fsa/NodeFileSystemHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export abstract class NodeFileSystemHandle {
) {}

/**
* Compares two handles to see if the associated entries (either a file or directory) match.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle/isSameEntry
*/
public isSameEntry(fileSystemHandle: NodeFileSystemHandle): boolean {
throw new Error('Not implemented');
return this.constructor === fileSystemHandle.constructor && (this as any).__path === (fileSystemHandle as any).__path;
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/node-to-fsa/__tests__/NodeFileSystemHandle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {DirectoryJSON, memfs} from '../..';
import {NodeFileSystemDirectoryHandle} from '../NodeFileSystemDirectoryHandle';

const setup = (json: DirectoryJSON = {}) => {
const fs = memfs(json, '/');
const dir = new NodeFileSystemDirectoryHandle(fs as any, '/');
return {dir, fs};
};

test('can instantiate', () => {
const {dir} = setup();
expect(dir).toBeInstanceOf(NodeFileSystemDirectoryHandle);
});

describe('.isSameEntry()', () => {
test('returns true for the same root entry', async () => {
const {dir} = setup();
expect(dir.isSameEntry(dir)).toBe(true);
});

test('returns true for two different instances of the same entry', async () => {
const {dir} = setup({
subdir: null,
});
const subdir = await dir.getDirectoryHandle('subdir');
expect(subdir.isSameEntry(subdir)).toBe(true);
expect(dir.isSameEntry(dir)).toBe(true);
expect(dir.isSameEntry(subdir)).toBe(false);
expect(subdir.isSameEntry(dir)).toBe(false);
});

test('returns false when comparing file with a directory', async () => {
const {dir} = setup({
file: 'lala',
});
const file = await dir.getFileHandle('file');
expect(file.isSameEntry(dir)).toBe(false);
expect(dir.isSameEntry(file)).toBe(false);
});
});

0 comments on commit 438806b

Please sign in to comment.