-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
useInheritContext.tsx
70 lines (59 loc) · 1.69 KB
/
useInheritContext.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { MutableRefObject, ReactElement, ReactNode } from "react";
import { renderHook } from "@testing-library/react-hooks";
import { render } from "@testing-library/react";
import { useInheritContext, InheritContext } from "../useInheritContext";
describe("useInheritContext", () => {
it("should default to false", () => {
const { result } = renderHook(() => useInheritContext(undefined));
expect(result.current).toBe(false);
});
it("should update to true based on the InheritContext", () => {
const result: MutableRefObject<boolean | undefined> = {
current: undefined,
};
function Context({ children }: { children: ReactNode }): ReactElement {
return (
<InheritContext.Provider value>{children}</InheritContext.Provider>
);
}
const Test1 = () => {
result.current = useInheritContext(undefined);
return null;
};
const Test2 = () => {
result.current = useInheritContext(false);
return null;
};
const Test3 = () => {
result.current = useInheritContext(true);
return null;
};
render(
<Context>
<Test1 />
</Context>
);
expect(result.current).toBe(true);
result.current = undefined;
render(
<Context>
<Test2 />
</Context>
);
expect(result.current).toBe(false);
result.current = undefined;
render(
<Context>
<Test3 />
</Context>
);
expect(result.current).toBe(true);
result.current = undefined;
render(<Test2 />);
expect(result.current).toBe(false);
result.current = undefined;
render(<Test3 />);
expect(result.current).toBe(true);
result.current = undefined;
});
});