Skip to content

Commit

Permalink
chore: Add test for switching storage type to IndexedDB
Browse files Browse the repository at this point in the history
- Rename existing storage type test to be more specific

- Add indexedDB storage type check
  • Loading branch information
intercepted16 committed Jun 2, 2024
1 parent c0e840d commit 12be16b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions test/localStorageStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ describe('persisted()', () => {
expect(await localforage.getItem("myKey11")).toEqual(serializer.stringify(new Set([1, 2, 3, 4])))
})

it('lets you switch storage type', () => {
it('lets you switch storage type to sessionStorage', async () => {
vi.spyOn(Object.getPrototypeOf(window.sessionStorage), 'setItem')
Object.setPrototypeOf(window.sessionStorage.setItem, vi.fn())

Expand All @@ -313,4 +313,22 @@ describe('persisted()', () => {

expect(window.sessionStorage.setItem).toHaveBeenCalled()
})
})

it("lets you switch storage type to indexedDB", async () => {
/* Testing direct calls to the mock IndexedDB is not feasible due to the timing
of spy setup and localforage import.
Localforage's internal calls to IndexedDB occur before the spy can be set up.
As a workaround, verify if localforage's setDriver method was called with the correct arguments. */
const setDriverSpy = vi.spyOn(localforage, "setDriver");

const value = "foo";

const store = await persisted("myKey12", value, {
storage: "indexedDB",
});

await store.set("bar");

expect(setDriverSpy).toHaveBeenCalledWith(localforage.INDEXEDDB);
});
})

0 comments on commit 12be16b

Please sign in to comment.