deno.land / x / solid@v1.5.6 / test / component.spec.ts

component.spec.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { createRoot, createComponent, mergeProps, splitProps, createUniqueId, createSignal, createEffect} from "../src";import { createStore } from "../store/src";
type SimplePropTypes = { a?: string | null; b?: string | null; c?: string | null; d?: string | null;};
const Comp = (props: { greeting: string; name: string }) => `${props.greeting} ${props.name}`;
const Comp2 = (props: { greeting: string; name: string; optional?: string }) => { const [p, q] = splitProps(props, ["greeting", "optional"]); return `${p.greeting} ${q.name}`;};
describe("CreateComponent", () => { test("create simple component", () => { createRoot(() => { const out = createComponent(Comp, { greeting: "Hi", get name() { return "dynamic"; } }); expect(out).toBe("Hi dynamic"); }); }); test("null/undefined props are replaced with empty props", () => { createRoot(() => { const nonObjects = [null, undefined, false]; nonObjects.forEach(nonObject => { const out = createComponent(p => p, nonObject); expect(out).toEqual({}); }); }); });});
describe("Set Default Props", () => { test("simple set", () => { let props: SimplePropTypes = { get a() { return "ji"; }, b: null, c: "j" }, defaults: SimplePropTypes = { a: "yy", b: "ggg", d: "DD" }; props = mergeProps(defaults, props); expect(props.a).toBe("ji"); expect(props.b).toBe(null); expect(props.c).toBe("j"); expect(props.d).toBe("DD"); });});
describe("Clone Props", () => { test("simple set", () => { let reactive = false; const props: SimplePropTypes = { get a() { reactive = true; return "ji"; }, b: null, c: "j" }; const newProps = mergeProps({}, props); expect(reactive).toBe(false); expect(newProps.a).toBe("ji"); expect(reactive).toBe(true); expect(newProps.b).toBe(null); expect(newProps.c).toBe("j"); expect(newProps.d).toBe(undefined); });});
describe("Clone Store", () => { const [state, setState] = createStore<{ a: string; b: string; c?: string }>({ a: "Hi", b: "Jo" }); const clone = mergeProps(state); expect(clone.a).toBe("Hi"); expect(clone.b).toBe("Jo"); setState({ a: "Greetings", c: "John" }); expect(clone.a).toBe("Greetings"); expect(clone.b).toBe("Jo"); expect(clone.c).toBe("John");});
describe("Merge Signal", () => { test("simple set", () => { const [s, set] = createSignal<SimplePropTypes>({ get a() { return "ji"; }, b: null, c: "j" }), defaults: SimplePropTypes = { a: "yy", b: "ggg", d: "DD" }; const props = mergeProps(defaults, s); const res: string[] = []; createRoot(() => { createEffect(() => { res.push(props.a as string); }); }); expect(props.a).toBe("ji"); expect(props.b).toBe(null); expect(props.c).toBe("j"); expect(props.d).toBe("DD"); set({ a: "h" }); expect(props.a).toBe("h"); expect(props.b).toBe("ggg"); expect(props.c).toBeUndefined(); expect(props.d).toBe("DD"); expect(res[0]).toBe("ji"); expect(res[1]).toBe("h"); expect(res.length).toBe(2); });
test("null/undefined/false are ignored", () => { const props = mergeProps({ a: 1 }, null, undefined, false); expect(props).toEqual({ a: 1 }); });});
describe("SplitProps Props", () => { test("SplitProps in two", () => { createRoot(() => { const out = createComponent(Comp2, { greeting: "Hi", get name() { return "dynamic"; } }); expect(out).toBe("Hi dynamic"); }); });});
describe("createUniqueId", () => { test("creating some", () => { const id1 = createUniqueId(); const id2 = createUniqueId();
expect(id1).toBeDefined(); expect(id2).toBeDefined(); expect(id1).not.toEqual(id2); });});
solid

Version Info

Tagged at
a year ago