Skip to content

Commit c0533e1

Browse files
committed
Update tests
1 parent 4a5ac6e commit c0533e1

File tree

7 files changed

+136
-0
lines changed

7 files changed

+136
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function CounterWithoutSelectors() {
5+
const [{ count }, setState] = useStore();
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
return (
9+
<div>
10+
<h2>Counter Without Selectors</h2>
11+
<p>Rerender is triggered every time the state changes.</p>
12+
<p>Count: {count}</p>
13+
<button onClick={() => setState(state => ({ ...state, count: count + 1 }))}>Increment</button>
14+
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button>
15+
<p data-testid="counter2-display">Render Count: {renderCount.current}</p>
16+
</div>
17+
);
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function Counter() {
5+
const [{ count }, setState] = useStore(/^count$/);
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
return (
9+
<div>
10+
<h2>Counter With Selectors</h2>
11+
<p>Rerender is triggered by RGS only when count changes.</p>
12+
<p>Count: {count}</p>
13+
<button
14+
data-testid="increment-btn"
15+
onClick={() => setState(state => ({ ...state, count: count + 1 }))}>
16+
Increment
17+
</button>
18+
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button>
19+
<p data-testid="counter1-display">Render Count: {renderCount.current}</p>
20+
</div>
21+
);
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
import styles from "../demo.module.scss";
4+
import { listToRegExp } from "../../src/utils";
5+
6+
export function Header() {
7+
const [{ name }] = useStore(listToRegExp(["name"]));
8+
const renderCount = useRef(0);
9+
renderCount.current++;
10+
return (
11+
<>
12+
<h1>Example with Selectors</h1>
13+
<header className={styles.header}>
14+
<h2>My name is {name}</h2>
15+
<small>
16+
<i>
17+
Updates only when <code>name</code> changes.{" "}
18+
<code data-testid="header-render-count">renderCount = {renderCount.current}</code>
19+
</i>
20+
</small>
21+
</header>
22+
</>
23+
);
24+
}

lib/tests/with-selectors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./with-selectors";

lib/tests/with-selectors/store.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useRGS } from "../../src";
2+
3+
interface MyStore {
4+
count: number;
5+
name: string;
6+
user: {
7+
name: string;
8+
age: number;
9+
};
10+
}
11+
12+
export const useStore = (includeRegExp?: RegExp | null | 0, excludeRegExp?: RegExp) =>
13+
useRGS<MyStore>(
14+
"my-store-with-selectors",
15+
{
16+
count: 0,
17+
name: "John",
18+
user: {
19+
name: "John",
20+
age: 30,
21+
},
22+
},
23+
includeRegExp,
24+
excludeRegExp,
25+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function UserData() {
5+
const [{ user }, setState] = useStore(/^user$/);
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
9+
e.preventDefault();
10+
const formData = new FormData(e.currentTarget);
11+
const name = formData.get("name") as string;
12+
const age = Number(formData.get("age"));
13+
setState(state => ({ ...state, user: { name, age }, name }));
14+
};
15+
return (
16+
<div>
17+
<h1>UserData</h1>
18+
<small>renderCount = {renderCount.current}</small>
19+
<p>Name: {user.name}</p>
20+
<p>Age: {user.age}</p>
21+
<form onSubmit={onSubmit}>
22+
<input type="text" name="name" defaultValue={user.name} />
23+
<input type="number" name="age" defaultValue={user.age} />
24+
<button type="submit">Update User</button>
25+
</form>
26+
</div>
27+
);
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Counter } from "./counter";
2+
import styles from "../demo.module.scss";
3+
import { CounterWithoutSelectors } from "./counter-without-selectors";
4+
import { Header } from "./header";
5+
import { UserData } from "./user-data";
6+
7+
export function WithSelector() {
8+
return (
9+
<div className={styles.preview}>
10+
<Header />
11+
<div className={styles.flex}>
12+
<Counter />
13+
<CounterWithoutSelectors />
14+
<UserData />
15+
</div>
16+
</div>
17+
);
18+
}

0 commit comments

Comments
 (0)