-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdebounce.test.ts
149 lines (130 loc) · 4.62 KB
/
debounce.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
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
import { deepStrictEqual, ok, strictEqual } from "node:assert";
import { sleep } from "./async.ts";
import jsext from "./index.ts";
import { as } from "./object.ts";
describe("jsext.debounce", () => {
it("success", async () => {
let count = 0;
const fn = jsext.debounce(<T>(obj: T) => {
count++;
return obj;
}, 5);
const [res1, res2] = await Promise.all([
fn({ foo: "hello", bar: "world" }),
fn({ foo: "hi" }),
]);
deepStrictEqual(res1, { foo: "hi" });
ok(res1 === res2);
strictEqual(count, 1);
await sleep(6);
const res3 = await fn({ bar: "world" });
deepStrictEqual(res3, { bar: "world" });
strictEqual(count, 2);
});
it("error", async () => {
let count = 0;
const fn = jsext.debounce(<T>(obj: T) => {
count++;
if ("foo" in (obj as any) && (obj as any)["foo"] === "hi") {
throw new Error("something went wrong");
} else {
return obj;
}
}, 5);
const [[err1], [err2]] = await Promise.all([
jsext.try(fn({ foo: "hello", bar: "world" })),
jsext.try(fn({ foo: "hi" })),
]);
deepStrictEqual(err1, new Error("something went wrong"));
ok(err1 === err2);
strictEqual(count, 1);
await sleep(6);
const res3 = await fn({ bar: "world" });
deepStrictEqual(res3, { bar: "world" });
strictEqual(count, 2);
});
it("async function", async () => {
let count = 0;
const fn = jsext.debounce(async <T>(obj: T) => {
count++;
return await Promise.resolve(obj);
}, 5);
const [res1, res2] = await Promise.all([
fn({ foo: "hello", bar: "world" }),
fn({ foo: "hi" }),
]);
deepStrictEqual(res1, { foo: "hi" });
ok(res1 === res2);
strictEqual(count, 1);
await sleep(6);
const res3 = await fn({ bar: "world" });
deepStrictEqual(res3, { bar: "world" });
strictEqual(count, 2);
});
it("with reducer", async () => {
let count = 0;
const fn = jsext.debounce(async <T>(obj: T) => {
count++;
return await Promise.resolve(obj);
}, 5, (prev, data) => {
return { ...prev, ...data };
});
const [res1, res2] = await Promise.all([
fn({ foo: "hello", bar: "world" }),
fn({ foo: "hi" }),
]);
deepStrictEqual(res1, { foo: "hi", bar: "world" });
ok(res1 === res2);
strictEqual(count, 1);
await sleep(6);
const res3 = await fn({ bar: "world" });
deepStrictEqual(res3, { bar: "world" });
strictEqual(count, 2);
});
it("with key", async () => {
let count = 0;
const [res1, res2] = await Promise.all([
jsext.debounce(async <T>(obj: T) => {
count++;
return await Promise.resolve(obj);
}, { delay: 5, for: "foo" }, (prev, data) => {
return { ...prev, ...data };
})({ foo: "hello", bar: "world" }),
jsext.debounce(async <T>(obj: T) => {
count += 2;
return await Promise.resolve(obj);
}, { delay: 5, for: "foo" }, (prev, data) => {
return { ...prev, ...data };
})({ foo: "hi" }),
]);
deepStrictEqual(res1, { foo: "hi", bar: "world" });
ok(res1 === res2);
strictEqual(count, 2);
await sleep(6);
const res3 = await jsext.debounce(async <T>(obj: T) => {
count += 3;
return await Promise.resolve(obj);
}, { delay: 5, for: "foo" }, (prev, data) => {
return { ...prev, ...data };
})({ bar: "world" });
deepStrictEqual(res3, { bar: "world" });
strictEqual(count, 5);
});
it("abort", async () => {
const controller = new AbortController();
const { signal } = controller;
let count = 0;
const fn = jsext.debounce(async <T>(obj: T) => {
count++;
return await Promise.resolve(obj);
}, { delay: 5, signal });
const task1 = fn({ foo: "hello", bar: "world" });
const task2 = fn({ foo: "hi" });
controller.abort();
const [res1, res2] = await Promise.allSettled([task1, task2]);
strictEqual(res1.status, "rejected");
strictEqual(res2.status, "rejected");
strictEqual(as(res1.reason, Error)?.name, "AbortError");
ok(res1.reason === res2.reason);
});
});