-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlodash.once-test.js
More file actions
49 lines (41 loc) · 1.28 KB
/
lodash.once-test.js
File metadata and controls
49 lines (41 loc) · 1.28 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
'use strict';
const assert = require('node:assert/strict');
const once = require('lodash.once');
try {
// Track how many times the underlying function is actually called
let callCount = 0;
const greet = once(function (name) {
callCount += 1;
return 'Hello, ' + name + '!';
});
// First call should work normally
const first = greet('Alice');
assert.equal(first, 'Hello, Alice!');
assert.equal(callCount, 1);
// Subsequent calls should return the first result, not re-execute
const second = greet('Bob');
assert.equal(second, 'Hello, Alice!');
assert.equal(callCount, 1);
const third = greet('Charlie');
assert.equal(third, 'Hello, Alice!');
assert.equal(callCount, 1);
// Side effects should only happen once
let sideEffectCount = 0;
const init = once(function () {
sideEffectCount += 1;
return { initialized: true };
});
const obj1 = init();
const obj2 = init();
const obj3 = init();
assert.equal(sideEffectCount, 1);
assert.deepEqual(obj1, { initialized: true });
// Same reference returned every time
assert.equal(obj1, obj2);
assert.equal(obj2, obj3);
console.log('lodash.once-test:ok');
} catch (err) {
console.error('lodash.once-test:fail');
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
}