-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: localStorage * WIP: localStorage dummy test * WIP: implemented localstorage tests * docs: localstorage docs
- Loading branch information
Showing
13 changed files
with
643 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { useLocalStorage } from "../src/localStorage"; | ||
import { nextTick } from "./utils"; | ||
import { promisedTimeout } from "../src/utils"; | ||
|
||
describe("localStorage", () => { | ||
const _localStorage = localStorage; | ||
let localStore: Record<string, string> = {}; | ||
let len = 0; | ||
|
||
const setItem = jest.fn((key: string, value: string) => { | ||
localStore[key] = value.toString(); | ||
len = Object.keys(localStore).length; | ||
}); | ||
const getItem = jest.fn((key: string) => localStore[key]); | ||
const removeItem = jest.fn((key: string) => { | ||
delete localStore[key]; | ||
len = Object.keys(localStore).length; | ||
}); | ||
|
||
const key = jest.fn(index => { | ||
return Object.keys(localStore)[index]; | ||
}); | ||
|
||
const clear = jest.fn(() => (localStore = {})); | ||
|
||
let mockedLocalStorage: Storage = { | ||
setItem, | ||
getItem, | ||
clear, | ||
removeItem, | ||
length: len, | ||
key | ||
}; | ||
|
||
Object.defineProperty(window, "localStorage", { | ||
value: mockedLocalStorage | ||
}); | ||
|
||
beforeEach(() => { | ||
setItem.mockClear(); | ||
getItem.mockClear(); | ||
clear.mockClear(); | ||
removeItem.mockClear(); | ||
key.mockClear(); | ||
}); | ||
|
||
afterEach(async () => { | ||
useLocalStorage("").clear(); | ||
await promisedTimeout(100); | ||
localStore = {}; | ||
len = 0; | ||
}); | ||
|
||
afterAll(() => { | ||
Object.defineProperty(window, "localStorage", { | ||
value: _localStorage | ||
}); | ||
}); | ||
|
||
it("should store in the localStore", () => { | ||
localStorage.setItem("test", "test"); | ||
|
||
expect(localStore["test"]).toBe("test"); | ||
}); | ||
|
||
it("should store object in localStorage if default is passed", async () => { | ||
const obj = { a: 1 }; | ||
const { storage } = useLocalStorage("test", obj); | ||
|
||
await promisedTimeout(100); | ||
|
||
expect(storage.value).toMatchObject(obj); | ||
expect(setItem).toHaveBeenCalledWith("test", JSON.stringify(obj)); | ||
}); | ||
|
||
it("should update the localstorage if value changes", async () => { | ||
const obj = { a: 1 }; | ||
|
||
const { storage } = useLocalStorage("test", obj); | ||
await nextTick(); | ||
await promisedTimeout(100); | ||
|
||
expect(storage.value).toMatchObject(obj); | ||
expect(setItem).toHaveBeenCalledWith("test", JSON.stringify(obj)); | ||
|
||
storage.value.a = 33; | ||
await nextTick(); | ||
|
||
await promisedTimeout(100); | ||
|
||
expect(storage.value).toMatchObject({ a: 33 }); | ||
expect(setItem).toHaveBeenCalledWith("test", JSON.stringify({ a: 33 })); | ||
|
||
expect(localStore["test"]).toBe(JSON.stringify({ a: 33 })); | ||
}); | ||
|
||
it("should get the same object if the same key is used", () => { | ||
const key = "test"; | ||
const { storage: storage1 } = useLocalStorage(key, { a: 1 }); | ||
const { storage: storage2 } = useLocalStorage(key, { a: 1 }); | ||
|
||
expect(storage1).toBe(storage2); | ||
}); | ||
|
||
it("should remove from localstorage", async () => { | ||
const key = "test"; | ||
const { remove } = useLocalStorage(key, { a: 1 }); | ||
|
||
remove(); | ||
await nextTick(); | ||
expect(localStore[key]).toBeUndefined(); | ||
}); | ||
|
||
it("should clear all localstorage keys", async () => { | ||
localStorage.setItem("_other_", "secret"); | ||
const s1 = useLocalStorage("key", { a: 1 }); | ||
const s2 = useLocalStorage("key2", { a: 2 }); | ||
|
||
await promisedTimeout(100); | ||
|
||
expect(localStore).toMatchObject({ | ||
key: JSON.stringify(s1.storage.value), | ||
key2: JSON.stringify(s2.storage.value), | ||
_other_: "secret" | ||
}); | ||
|
||
s1.clear(); | ||
|
||
await nextTick(); | ||
await promisedTimeout(200); | ||
|
||
expect(s1.storage.value).toBeUndefined(); | ||
expect(s2.storage.value).toBeUndefined(); | ||
expect(localStore).toStrictEqual({ | ||
_other_: "secret" | ||
}); | ||
}); | ||
|
||
|
||
it("should load from localStorage", () => { | ||
const key = "hello"; | ||
localStorage.setItem(key, JSON.stringify({ k: 1 })); | ||
|
||
const { storage } = useLocalStorage(key, { k: 10 }); | ||
|
||
expect(storage.value).toMatchObject({ k: 1 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
export * from './event/event'; | ||
export * from './pagination/arrayPagination'; | ||
export * from './debounce'; | ||
export * from './event/onMouseMove'; | ||
export * from './event/onResize'; | ||
export * from './event/onScroll'; | ||
export * from './pagination/pagination'; | ||
export * from './promise/promise'; | ||
export * from './promise/cancellablePromise'; | ||
export * from './promise/retry'; | ||
export * from './web/fetch'; | ||
export * from './web/axios'; | ||
export * from './web/webSocket'; | ||
export * from "./event/event"; | ||
export * from "./pagination/arrayPagination"; | ||
export * from "./debounce"; | ||
export * from "./event/onMouseMove"; | ||
export * from "./event/onResize"; | ||
export * from "./event/onScroll"; | ||
export * from "./pagination/pagination"; | ||
export * from "./promise/promise"; | ||
export * from "./promise/cancellablePromise"; | ||
export * from "./promise/retry"; | ||
export * from "./web/fetch"; | ||
export * from "./web/axios"; | ||
export * from "./web/webSocket"; | ||
export * from "./localStorage"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Ref } from "@vue/composition-api"; | ||
import { RefTyped } from "./utils"; | ||
export declare type LocalStorageTyped<T> = string; | ||
export interface LocalStorageReturn<T> { | ||
storage: Ref<T>; | ||
/** | ||
* @description Removes current item from the store | ||
*/ | ||
remove: () => void; | ||
/** | ||
* @description Clears all tracked localStorage items | ||
*/ | ||
clear: () => void; | ||
} | ||
export declare function useLocalStorage<T = any>(key: LocalStorageTyped<T> | string, defaultValue?: RefTyped<T>): LocalStorageReturn<T>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.