-
Notifications
You must be signed in to change notification settings - Fork 0
/
useTabSharedState.test.tsx
53 lines (42 loc) · 1.16 KB
/
useTabSharedState.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React, { useEffect } from "react";
import { act, render, screen, waitFor } from "@testing-library/react";
import { useTabSharedState } from "./useTabSharedState";
const KEY = "test-key";
const AppA = () => {
const [value, setValue] = useTabSharedState<number>(KEY, 1);
useEffect(() => {
setValue(5);
}, []);
return <div>A: {value}</div>;
};
const AppB = () => {
const [value] = useTabSharedState<number>(KEY, 1);
return <div>B: {value}</div>;
};
const App = () => {
return (
<div>
<AppA />
<AppB />
</div>
);
};
test("state syncs changes after loading", async () => {
render(<App />); // default testing lib
const stateAValue = screen.getByText(/A: 5/i);
expect(stateAValue).toBeInTheDocument();
const stateBInitialValue = screen.getByText(/B: 1/i);
expect(stateBInitialValue).toBeInTheDocument();
// fake the storage even that the browser would have triggered:
act(() => {
window.dispatchEvent(
new StorageEvent("storage", {
key: KEY,
})
);
});
await waitFor(() => {
const stateBValue = screen.getByText(/B: 5/i);
return expect(stateBValue).toBeInTheDocument();
});
});