Syntax previewClick to edit
1234
export default function once(fn) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
1234567891011121314151617181920212223
import once from './file';
describe('once', () => {
test('calls the function only once', () => {
let calls = 0;
const fn = once(() => { calls += 1; return calls; });
expect(fn()).toBe(1);
expect(fn()).toBe(1);
expect(calls).toBe(1);
});
test('returns the first result on later calls', () => {
const add = once((a, b) => a + b);
expect(add(1, 2)).toBe(3);
expect(add(5, 6)).toBe(3);
});
test('preserves this context', () => {
const obj = { x: 2, get: once(function (n) { return this.x + n; }) };
expect(obj.get(3)).toBe(5);
});
});
Run tests to see results.