-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathrepeat.test.ts
More file actions
133 lines (124 loc) · 3.88 KB
/
Copy pathrepeat.test.ts
File metadata and controls
133 lines (124 loc) · 3.88 KB
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
import { expect, describe, it } from "vitest";
import { createComputed, createRoot, createSignal, onCleanup } from "solid-js";
import { Repeat, repeat } from "../src/index.js";
describe("repeat", () => {
it("maps only added items", () =>
createRoot(dispose => {
const [length, setLength] = createSignal(5);
const captured: number[] = [];
const mapped = repeat(length, i => {
captured.push(i);
return i;
});
expect(mapped(), "initial mapped").toEqual([0, 1, 2, 3, 4]);
expect(captured, "initial captured").toEqual([0, 1, 2, 3, 4]);
setLength(7);
expect(mapped(), "1 mapped").toEqual([0, 1, 2, 3, 4, 5, 6]);
expect(captured, "1 captured").toEqual([0, 1, 2, 3, 4, 5, 6]);
setLength(3);
expect(mapped(), "2 mapped").toEqual([0, 1, 2]);
expect(captured, "2 captured").toEqual([0, 1, 2, 3, 4, 5, 6]);
setLength(5);
expect(mapped(), "3 mapped").toEqual([0, 1, 2, 3, 4]);
expect(captured, "3 captured").toEqual([0, 1, 2, 3, 4, 5, 6, 3, 4]);
dispose();
}));
it("uses fallback if length is 0", () =>
createRoot(dispose => {
const [length, setLength] = createSignal(4);
const captured: (string | number)[] = [];
const mapped = repeat<string | number>(
length,
i => {
captured.push(i);
return i;
},
{
fallback: () => {
captured.push("fb");
return "fb";
},
},
);
expect(mapped(), "initial mapped").toEqual([0, 1, 2, 3]);
expect(captured, "initial captured").toEqual([0, 1, 2, 3]);
setLength(0);
expect(mapped(), "1 mapped").toEqual(["fb"]);
expect(captured, "1 captured").toEqual([0, 1, 2, 3, "fb"]);
setLength(3);
expect(mapped(), "2 mapped").toEqual([0, 1, 2]);
expect(captured, "2 captured").toEqual([0, 1, 2, 3, "fb", 0, 1, 2]);
dispose();
}));
it("disposing on remove and cleanup", () =>
createRoot(dispose => {
const [length, setLength] = createSignal(2);
const cleanups: (string | number)[] = [];
const mapped = repeat<string | number>(
length,
i => {
onCleanup(() => cleanups.push(i));
return i;
},
{
fallback: () => {
onCleanup(() => cleanups.push("fb"));
return "fb";
},
},
);
// cleanups happen on access
createComputed(mapped);
expect(cleanups, "initial cleanups").toEqual([]);
setLength(1);
expect(cleanups, "1 cleanups").toEqual([1]);
setLength(0);
expect(cleanups, "2 cleanups").toEqual([1, 0]);
dispose();
expect(cleanups, "3 cleanups").toEqual([1, 0, "fb"]);
setLength(3);
expect(mapped(), "mapped after dispose").toEqual(["fb"]);
}));
it("uses fallback when length is initially 0", () =>
createRoot(disposer => {
const map = repeat(
() => 0,
i => i,
{ fallback: () => NaN },
);
expect(map()).toEqual([NaN]);
disposer();
}));
});
describe("<Repeat/>", () => {
it("notifies observers on length change", () => {
const [length, setLength] = createSignal(3);
const [dispose, accessor] = createRoot(dispose => {
const accessor = Repeat({
get times() {
return length();
},
fallback: () => 0,
children: () => 1,
}) as never as () => {};
return [dispose, accessor];
});
let notifications = 0;
createComputed(() => {
accessor();
notifications++;
});
expect(notifications).toEqual(1);
setLength(4);
expect(notifications).toEqual(2);
setLength(0);
expect(notifications).toEqual(3);
setLength(2);
expect(notifications).toEqual(4);
setLength(1);
expect(notifications).toEqual(5);
setLength(1.5);
expect(notifications).toEqual(5);
dispose();
});
});