-
-
Notifications
You must be signed in to change notification settings - Fork 883
/
component.spec.ts
206 lines (193 loc) · 5.1 KB
/
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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"]);
expect((p as any).name).toBeUndefined();
expect((q as any).greeting).toBeUndefined();
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("mergeProps", () => {
test("falsey values", () => {
let props: SimplePropTypes = {
get a() {
return "ji";
},
b: null,
c: "j"
};
props = mergeProps(props, false, null, undefined);
expect(props.a).toBe("ji");
expect(props.b).toBe(null);
expect(props.c).toBe("j");
});
});
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", () => {
test("simple set", () => {
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" };
let props!: SimplePropTypes;
const res: string[] = [];
createRoot(() => {
props = mergeProps(defaults, s);
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");
});
});
test("SplitProps in two with store", () => {
createRoot(() => {
const [state] = createStore({ greeting: "Yo", name: "Bob" });
const out = createComponent(Comp2, state);
expect(out).toBe("Yo Bob");
});
});
test("Merge SplitProps", () => {
let value: string | undefined = undefined;
const [splittedProps] = splitProps({ color: "blue" } as { color: string; other?: string }, [
"color",
"other"
]);
const mergedProps = mergeProps(splittedProps, {
get color() {
return value;
},
other: "value"
});
expect(mergedProps.color).toBe("blue");
value = "red";
expect(mergedProps.color).toBe("red");
});
});
describe("createUniqueId", () => {
test("creating some", () => {
const id1 = createUniqueId();
const id2 = createUniqueId();
expect(id1).toBeDefined();
expect(id2).toBeDefined();
expect(id1).not.toEqual(id2);
});
});