-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontext.test.ts
75 lines (64 loc) · 1.86 KB
/
context.test.ts
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
71
72
73
74
75
import {
ContextNotFoundError,
createContext,
createRoot,
getContext,
hasContext,
NoOwnerError,
setContext
} from "../src/index.js";
it("should create context", () => {
const context = createContext(1);
expect(context.id).toBeDefined();
expect(context.defaultValue).toEqual(1);
createRoot(() => {
setContext(context);
expect(getContext(context)).toEqual(1);
});
});
it("should forward context across roots", () => {
const context = createContext(1);
createRoot(() => {
setContext(context, 2);
createRoot(() => {
expect(getContext(context)).toEqual(2);
createRoot(() => {
expect(getContext(context)).toEqual(2);
});
});
});
});
it("should not expose context on parent when set in child", () => {
const context = createContext(1);
createRoot(() => {
createRoot(() => {
setContext(context, 4);
});
expect(getContext(context)).toEqual(1);
});
});
it("should return true if context has been provided", () => {
const context = createContext();
createRoot(() => {
setContext(context, 1);
expect(hasContext(context)).toBeTruthy();
});
});
it("should return false if context has not been provided", () => {
const context = createContext();
createRoot(() => {
expect(hasContext(context)).toBeFalsy();
});
});
it("should throw error when trying to get context outside owner", () => {
const context = createContext();
expect(() => getContext(context)).toThrowError(NoOwnerError);
});
it("should throw error when trying to set context outside owner", () => {
const context = createContext();
expect(() => setContext(context)).toThrowError(NoOwnerError);
});
it("should throw error when trying to get context without setting it first", () => {
const context = createContext();
expect(() => createRoot(() => getContext(context))).toThrowError(ContextNotFoundError);
});