Skip to content

Commit

Permalink
Added WebStorage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Oct 30, 2024
1 parent ba56ddf commit 7719b5a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ jobs:

- name: Unit tests (IndexedDB)
run: npx zenfs-test tests/setup-idb.ts

- name: Unit tests (WebStorage)
run: npx zenfs-test tests/setup-storage.ts
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"devDependencies": {
"@eslint/js": "^9.12.0",
"eslint": "^9.12.0",
"fake-indexeddb": "^6.0.0",
"globals": "^15.10.0",
"prettier": "^3.2.5",
"typedoc": "^0.26.10",
Expand All @@ -53,6 +52,9 @@
"peerDependencies": {
"@zenfs/core": "^1.2.0"
},
"optionalDependencies": {
"fake-indexeddb": "^6.0.0"
},
"keywords": [
"filesystem",
"node",
Expand Down
3 changes: 3 additions & 0 deletions src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const _WebStorage = {
},
},

/**
* @todo Consider replacing `instanceof` with a duck-typing check?
*/
isAvailable(storage: Storage = globalThis.localStorage): boolean {
return storage instanceof globalThis.Storage;
},
Expand Down
39 changes: 39 additions & 0 deletions tests/setup-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { configureSingle } from '@zenfs/core';
import { WebStorage } from '../src/Storage.js';
import { copy, data } from '@zenfs/core/tests/setup/common.js';

const storage = {
_: new Map<string, string>(),

get length(): number {
return this._.size;
},

clear(): void {
this._.clear();
},

getItem(key: string): string | null {
return this._.get(key) ?? null;
},

key(index: number): string | null {
return Array.from(this._.keys())[index];
},

removeItem(key: string): void {
this._.delete(key);
},

setItem(key: string, value: string): void {
this._.set(key, value);
},
};

// @ts-expect-error 2322
globalThis.Storage = Object; // Bypass `instanceof` check in `WebStorage` is available
globalThis.localStorage = storage;

await configureSingle({ backend: WebStorage, storage });

copy(data);

0 comments on commit 7719b5a

Please sign in to comment.