-
-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathonce.test.js
More file actions
37 lines (30 loc) · 803 Bytes
/
once.test.js
File metadata and controls
37 lines (30 loc) · 803 Bytes
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
import { deepStrictEqual, ok, strictEqual } from "node:assert";
import test from "node:test";
import { once } from "../../src/utils.js";
test("once(fn)", () => {
let f = 0;
function fn(g) {
strictEqual(f, 0);
f += 1;
return f + g + this;
}
fn.ownProperty = {};
const wrapped = once(fn);
strictEqual(fn.ownProperty, wrapped.ownProperty);
strictEqual(wrapped.called, false);
for (let i = 0; i < 1e3; i += 1) {
strictEqual(f, i === 0 ? 0 : 1);
const g = wrapped.call(1, 1);
ok(wrapped.called);
strictEqual(g, 3);
strictEqual(f, 1);
}
});
test("once wrapper preserves callback own properties", () => {
function fn() {
return true;
}
fn.meta = { keep: true };
const wrapped = once(fn);
deepStrictEqual(wrapped.meta, { keep: true });
});