Syntax previewClick to edit
1234
export default function createSpy(impl) {
throw new Error('Not implemented');
}
Syntax previewClick to edit
123456789101112131415161718192021222324252627282930313233343536373839404142
import createSpy from './createSpy';
describe('createSpy', () => {
test('records calls and arguments', () => {
const spy = createSpy();
spy(1, 2);
spy('a');
expect(spy.callCount()).toBe(2);
expect(spy.calls).toEqual([[1, 2], ['a']]);
expect(spy.lastCall()).toEqual(['a']);
});
test('delegates to the implementation and returns its value', () => {
const spy = createSpy((a, b) => a + b);
const out = spy(2, 3);
expect(out).toBe(5);
expect(spy.calls[0]).toEqual([2, 3]);
});
test('preserves this binding when used as a method', () => {
const obj = {
x: 10,
fn: createSpy(function (n) { return this.x + n; })
};
const out = obj.fn(2);
expect(out).toBe(12);
expect(obj.fn.thisValues[0]).toBe(obj);
});
test('reset clears recorded calls', () => {
const spy = createSpy();
spy(1);
spy.reset();
expect(spy.callCount()).toBe(0);
expect(spy.calls).toEqual([]);
expect(spy.lastCall()).toBe(null);
});
});
Run tests to see results.