Skip to content

Commit 7bb70c3

Browse files
committed
chore: Add test for switching storage type to IndexedDB
- Rename existing storage type test to be more specific - Add indexedDB storage type check
1 parent d9e9494 commit 7bb70c3

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

test/localStorageStore.test.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ describe('persisted()', () => {
299299
expect(await localforage.getItem("myKey11")).toEqual(serializer.stringify(new Set([1, 2, 3, 4])))
300300
})
301301

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

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

314314
expect(window.sessionStorage.setItem).toHaveBeenCalled()
315315
})
316-
})
316+
317+
it("lets you switch storage type to indexedDB", async () => {
318+
/* Testing direct calls to the mock IndexedDB is not feasible due to the timing
319+
of spy setup and localforage import.
320+
Localforage's internal calls to IndexedDB occur before the spy can be set up.
321+
As a workaround, verify if localforage's setDriver method was called with the correct arguments. */
322+
const setDriverSpy = vi.spyOn(localforage, "setDriver");
323+
324+
const value = "foo";
325+
326+
const store = await persisted("myKey12", value, {
327+
storage: "indexedDB",
328+
});
329+
330+
await store.set("bar");
331+
332+
expect(setDriverSpy).toHaveBeenCalledWith(localforage.INDEXEDDB);
333+
});
334+
})

0 commit comments

Comments
 (0)