Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Apr 30, 2024
1 parent d2466e9 commit 206ffe4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/r18gs/src/with-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export function create<T>(
*/
export function withPlugins<T>(
plugins?: Plugin<T>[],
): (key: string, value?: T) => [T, SetStateAction<T>] {
): (key: string, value?: T, doNotInit?: boolean) => [T, SetStateAction<T>] {
/**
* todo - this typedoc comments are not visible in IDE suggestions - fix this
* @param key - Unique key to identify the store.
* @param value - Initial value of the store.
* @param doNotInit - @defaultValue false - Do not initialize the store. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized.
Expand Down
38 changes: 34 additions & 4 deletions lib/r18gs/tests/create.test.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,76 @@
import { describe, test } from "vitest";
import { act, fireEvent, render, screen } from "@testing-library/react";
import { create } from "../src/with-plugins";
import { create, withPlugins } from "../src/with-plugins";
import { persist } from "../src/plugins/persist";
import { ChangeEvent, useCallback } from "react";

const COUNTER_RGS_KEY = "count";
const COUNTER_RGS_KEY = "counter";
const TESTID_INPUT = "in1";
const TESTID_DISPLAY = "d1";

const COUNTER2_RGS_KEY = "counter-2";
const TESTID_INPUT2 = "in2";
const TESTID_DISPLAY2 = "d2";

const useMyRGS = create(COUNTER_RGS_KEY, 0, [persist({ storage: "cookie" })]);

const useMyRGS2 = withPlugins<number>([persist()]);

function Component1() {
const [count, setCount] = useMyRGS();
const [count2, setCount2] = useMyRGS2(COUNTER2_RGS_KEY, 0);
const handleChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setCount(parseInt(e.target.value));
},
[setCount],
);
const handleChange2 = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setCount2(parseInt(e.target.value));
},
[setCount2],
);
return (
<div>
<input data-testid={TESTID_INPUT} onChange={handleChange} type="number" value={count} />
<input data-testid={TESTID_INPUT2} onChange={handleChange2} type="number" value={count2} />
</div>
);
}

function Component2() {
const [count] = useMyRGS();
return <h1 data-testid={TESTID_DISPLAY}>{count}</h1>;
const [count2] = useMyRGS2(COUNTER2_RGS_KEY, 10, true);
return (
<>
<h1 data-testid={TESTID_DISPLAY}>{count}</h1>
<h1 data-testid={TESTID_DISPLAY2}>{count2}</h1>
</>
);
}

describe("React18GlobalStore", () => {
test("check state update to multiple components", async ({ expect }) => {
render(<Component1 />);
render(<Component2 />);
render(<Component1 />);
/** Await and allow for the state to update from localStorate */
await new Promise(resolve => setTimeout(resolve, 100));
await act(() => fireEvent.input(screen.getByTestId(TESTID_INPUT), { target: { value: 5 } }));
expect(screen.getByTestId(TESTID_DISPLAY).textContent).toBe("5");
expect(JSON.parse(sessionStorage.getItem(COUNTER_RGS_KEY) ?? "{}").val).toBe(5);
});

test("check state update to multiple components for withPlugins", async ({ expect }) => {
render(<Component2 />);
render(<Component1 />);
/** Await and allow for the state to update from localStorate */
await new Promise(resolve => setTimeout(resolve, 100));
await act(() => fireEvent.input(screen.getByTestId(TESTID_INPUT2), { target: { value: 5 } }));
expect(screen.getByTestId(TESTID_DISPLAY2).textContent).toBe("5");
expect(JSON.parse(localStorage.getItem(COUNTER2_RGS_KEY) ?? "{}").val).toBe(5);
});

test("storage event", async ({ expect }) => {
render(<Component1 />);
render(<Component2 />);
Expand Down
2 changes: 0 additions & 2 deletions lib/r18gs/tests/plugins/persist.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ describe("React18GlobalStore", () => {
await new Promise(resolve => setTimeout(resolve, 100));
await act(() => fireEvent.input(screen.getByTestId(TESTID_INPUT), { target: { value: 5 } }));
expect(screen.getByTestId(TESTID_DISPLAY).textContent).toBe("5");
console.log("sessionStorage --- ", sessionStorage.getItem(COUNTER_RGS_KEY), options);
console.log("localStorage --- ", localStorage.getItem(COUNTER_RGS_KEY));
expect(
JSON.parse(
((options.storage ?? "local") === "local" ? localStorage : sessionStorage).getItem(
Expand Down

0 comments on commit 206ffe4

Please sign in to comment.