Merge branch 'spy-calls-thisFor' of https://github.com/ajvincent/jasmine into main
* Merges #1903 from @ajvincent * Adds Spy#calls#thisFor
This commit is contained in:
@@ -3001,6 +3001,19 @@ getJasmineRequireObj().CallTracker = function(j$) {
|
|||||||
return call ? call.args : [];
|
return call ? call.args : [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the "this" object that was passed to a specific invocation of this spy.
|
||||||
|
* @name Spy#calls#thisFor
|
||||||
|
* @since 3.7.1
|
||||||
|
* @function
|
||||||
|
* @param {Integer} index The 0-based invocation index.
|
||||||
|
* @return {Object?}
|
||||||
|
*/
|
||||||
|
this.thisFor = function(index) {
|
||||||
|
var call = calls[index];
|
||||||
|
return call ? call.object : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the raw calls array for this spy.
|
* Get the raw calls array for this spy.
|
||||||
* @name Spy#calls#all
|
* @name Spy#calls#all
|
||||||
|
|||||||
@@ -30,6 +30,20 @@ describe('CallTracker', function() {
|
|||||||
expect(callTracker.argsFor(1)).toEqual([0, 'foo']);
|
expect(callTracker.argsFor(1)).toEqual([0, 'foo']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("tracks the 'this' object from each execution", function() {
|
||||||
|
var callTracker = new jasmineUnderTest.CallTracker();
|
||||||
|
|
||||||
|
var this0 = {},
|
||||||
|
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() {
|
it('returns any empty array when there was no call', function() {
|
||||||
var callTracker = new jasmineUnderTest.CallTracker();
|
var callTracker = new jasmineUnderTest.CallTracker();
|
||||||
|
|
||||||
|
|||||||
@@ -49,6 +49,19 @@ getJasmineRequireObj().CallTracker = function(j$) {
|
|||||||
return call ? call.args : [];
|
return call ? call.args : [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the "this" object that was passed to a specific invocation of this spy.
|
||||||
|
* @name Spy#calls#thisFor
|
||||||
|
* @since 3.7.1
|
||||||
|
* @function
|
||||||
|
* @param {Integer} index The 0-based invocation index.
|
||||||
|
* @return {Object?}
|
||||||
|
*/
|
||||||
|
this.thisFor = function(index) {
|
||||||
|
var call = calls[index];
|
||||||
|
return call ? call.object : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the raw calls array for this spy.
|
* Get the raw calls array for this spy.
|
||||||
* @name Spy#calls#all
|
* @name Spy#calls#all
|
||||||
|
|||||||
Reference in New Issue
Block a user