describe('CallTracker', function() { it('tracks that it was called when executed', function() { const callTracker = new privateUnderTest.CallTracker(); expect(callTracker.any()).toBe(false); callTracker.track(); expect(callTracker.any()).toBe(true); }); it('tracks that number of times that it is executed', function() { const callTracker = new privateUnderTest.CallTracker(); expect(callTracker.count()).toEqual(0); callTracker.track(); expect(callTracker.count()).toEqual(1); }); it('tracks the params from each execution', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.track({ object: void 0, args: [] }); callTracker.track({ object: {}, args: [0, 'foo'] }); expect(callTracker.argsFor(0)).toEqual([]); expect(callTracker.argsFor(1)).toEqual([0, 'foo']); }); it("tracks the 'this' object from each execution", function() { const callTracker = new privateUnderTest.CallTracker(); const this0 = {}; const this1 = {}; callTracker.track({ object: this0, args: [] }); callTracker.track({ object: this1, args: [] }); callTracker.track({ args: [] }); expect(callTracker.thisFor(0)).toBe(this0); expect(callTracker.thisFor(1)).toBe(this1); expect(callTracker.thisFor(2)).toBe(undefined); }); it('returns any empty array when there was no call', function() { const callTracker = new privateUnderTest.CallTracker(); expect(callTracker.argsFor(0)).toEqual([]); }); it('allows access for the arguments for all calls', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.track({ object: {}, args: [] }); callTracker.track({ object: {}, args: [0, 'foo'] }); expect(callTracker.allArgs()).toEqual([[], [0, 'foo']]); }); it('tracks the context and arguments for each call', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.track({ object: {}, args: [] }); callTracker.track({ object: {}, args: [0, 'foo'] }); expect(callTracker.all()[0]).toEqual({ object: {}, args: [] }); expect(callTracker.all()[1]).toEqual({ object: {}, args: [0, 'foo'] }); }); it('simplifies access to the arguments for the last (most recent) call', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.track(); callTracker.track({ object: {}, args: [0, 'foo'] }); expect(callTracker.mostRecent()).toEqual({ object: {}, args: [0, 'foo'] }); }); it("returns a useful falsy value when there isn't a last (most recent) call", function() { const callTracker = new privateUnderTest.CallTracker(); expect(callTracker.mostRecent()).toBeFalsy(); }); it('simplifies access to the arguments for the first (oldest) call', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.track({ object: {}, args: [0, 'foo'] }); expect(callTracker.first()).toEqual({ object: {}, args: [0, 'foo'] }); }); it("returns a useful falsy value when there isn't a first (oldest) call", function() { const callTracker = new privateUnderTest.CallTracker(); expect(callTracker.first()).toBeFalsy(); }); it('allows the tracking to be reset', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.track(); callTracker.track({ object: {}, args: [0, 'foo'] }); callTracker.reset(); expect(callTracker.any()).toBe(false); expect(callTracker.count()).toEqual(0); expect(callTracker.argsFor(0)).toEqual([]); expect(callTracker.all()).toEqual([]); expect(callTracker.mostRecent()).toBeFalsy(); }); it('allows object arguments to be shallow cloned', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.saveArgumentsByValue(); const objectArg = { foo: 'bar' }; const arrayArg = ['foo', 'bar']; callTracker.track({ object: {}, args: [objectArg, arrayArg, false, undefined, null, NaN, '', 0, 1.0] }); expect(callTracker.mostRecent().args[0]).not.toBe(objectArg); expect(callTracker.mostRecent().args[0]).toEqual(objectArg); expect(callTracker.mostRecent().args[1]).not.toBe(arrayArg); expect(callTracker.mostRecent().args[1]).toEqual(arrayArg); }); it('allows object arguments to be deep cloned', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.saveArgumentsByValue(args => JSON.parse(JSON.stringify(args))); const objectArg = { foo: { bar: { baz: ['qux'] } } }; const arrayArg = ['foo', 'bar']; callTracker.track({ object: {}, args: [objectArg, arrayArg, false, undefined, null, NaN, '', 0, 1.0] }); objectArg.foo.bar.baz.push('quux'); expect(callTracker.mostRecent().args[0]).not.toBe(objectArg); expect(callTracker.mostRecent().args[0]).not.toEqual(objectArg); expect(callTracker.mostRecent().args[0]).toEqual({ foo: { bar: { baz: ['qux'] } } }); expect(callTracker.mostRecent().args[1]).not.toBe(arrayArg); expect(callTracker.mostRecent().args[1]).toEqual(arrayArg); }); it('can take any function to transform arguments when saving by value', function() { const callTracker = new privateUnderTest.CallTracker(); callTracker.saveArgumentsByValue(JSON.stringify); const objectArg = { foo: { bar: { baz: ['qux'] } } }; const arrayArg = ['foo', 'bar']; const args = [objectArg, arrayArg, false, undefined, null, NaN, '', 0, 1.0]; callTracker.track({ object: {}, args }); expect(callTracker.mostRecent().args).toEqual(JSON.stringify(args)); }); it('saves primitive arguments by value', function() { const callTracker = new privateUnderTest.CallTracker(); const args = [undefined, null, false, '', /\s/, 0, 1.2, NaN]; callTracker.saveArgumentsByValue(); callTracker.track({ object: {}, args: args }); expect(callTracker.mostRecent().args).toEqual(args); }); });