@akb2/react-use-local-storage 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ import { clearLocalStorage, useLocalStorageState } from "@source";
2
+ import { cleanup, renderHook, waitFor } from "@testing-library/react";
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
+
5
+ beforeEach(() => localStorage.clear());
6
+
7
+ afterEach(() => {
8
+ cleanup();
9
+ clearLocalStorage();
10
+ vi.useRealTimers();
11
+ });
12
+
13
+ describe("LocalStorage direct changing", () => {
14
+ it("No support changes with direct localStorage modification", () => {
15
+ const { result } = renderHook(() => useLocalStorageState("initial"));
16
+
17
+ expect(result.current[0]).toBeUndefined();
18
+
19
+ localStorage.setItem(
20
+ "initial",
21
+ JSON.stringify({ value: "new value with direct modification" }),
22
+ );
23
+
24
+ return waitFor(() => expect(result.current[0]).toBe(undefined), {
25
+ timeout: 2000,
26
+ });
27
+ });
28
+ });
@@ -0,0 +1,40 @@
1
+ import { clearLocalStorage, useLocalStorageState } from "@source";
2
+ import { act, cleanup, renderHook } from "@testing-library/react";
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4
+
5
+ beforeEach(() => localStorage.clear());
6
+
7
+ afterEach(() => {
8
+ cleanup();
9
+ clearLocalStorage();
10
+ vi.useRealTimers();
11
+ });
12
+
13
+ describe("A simple value changing", () => {
14
+ it("No changing a value", () => {
15
+ const { result } = renderHook(() => useLocalStorageState("initial"));
16
+
17
+ expect(result.current[0]).toBeUndefined();
18
+ });
19
+
20
+ it("Changing a value", () => {
21
+ const { result } = renderHook(() => useLocalStorageState("initial"));
22
+
23
+ expect(result.current[0]).toBeUndefined();
24
+ act(() => result.current[1]("new value"));
25
+ expect(result.current[0]).toBe("new value");
26
+ });
27
+
28
+ it("Change a value with timeout", () => {
29
+ vi.useFakeTimers();
30
+
31
+ const { result } = renderHook(() => useLocalStorageState("initial"));
32
+
33
+ expect(result.current[0]).toBeUndefined();
34
+ setTimeout(() => result.current[1]("new value with timeout"), 100);
35
+ act(() => vi.advanceTimersByTime(99));
36
+ expect(result.current[0]).toBeUndefined();
37
+ act(() => vi.advanceTimersByTime(1));
38
+ expect(result.current[0]).toBe("new value with timeout");
39
+ });
40
+ });
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../tsconfig.test.json"
3
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "declaration": true,
6
+ "strict": true,
7
+ "moduleResolution": "Bundler",
8
+ "skipLibCheck": true,
9
+ "outDir": ".dist",
10
+ "rootDir": ".",
11
+ "esModuleInterop": true,
12
+ "jsx": "react-jsx",
13
+ "jsxImportSource": "preact",
14
+ "types": [
15
+ "react",
16
+ "react-dom"
17
+ ],
18
+ "paths": {
19
+ "@hooks/*": [
20
+ "./src/hooks/*"
21
+ ],
22
+ "@utils/*": [
23
+ "./src/utils/*"
24
+ ],
25
+ "@types/*": [
26
+ "./src/types/*"
27
+ ]
28
+ }
29
+ },
30
+ "include": [
31
+ "src"
32
+ ]
33
+ }
@@ -0,0 +1,31 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "noEmit": true,
5
+ "declaration": false,
6
+ "rootDir": ".",
7
+ "lib": [
8
+ "ES2020",
9
+ "DOM",
10
+ "DOM.Iterable"
11
+ ],
12
+ "types": [
13
+ "node",
14
+ "vitest/globals"
15
+ ],
16
+ "paths": {
17
+ "@source": [
18
+ "./src/index.ts"
19
+ ]
20
+ }
21
+ },
22
+ "include": [
23
+ "tests/**/*.ts",
24
+ "tests/**/*.tsx",
25
+ "vitest.config.ts"
26
+ ],
27
+ "exclude": [
28
+ "node_modules",
29
+ ".dist"
30
+ ]
31
+ }
@@ -0,0 +1,19 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { defineConfig } from "vitest/config";
3
+
4
+ export default defineConfig({
5
+ test: {
6
+ environment: "jsdom",
7
+ environmentOptions: {
8
+ jsdom: {
9
+ url: "http://localhost/",
10
+ },
11
+ },
12
+ alias: {
13
+ "@source": fileURLToPath(new URL("./src/index.ts", import.meta.url)),
14
+ "@utils": fileURLToPath(new URL("./src/utils", import.meta.url)),
15
+ "@types": fileURLToPath(new URL("./src/types", import.meta.url)),
16
+ "@hooks": fileURLToPath(new URL("./src/hooks", import.meta.url)),
17
+ },
18
+ },
19
+ });