add new sinon stub types (setters, getters, values) (#16848)

* add new sinon types

* bump sinon version
This commit is contained in:
James Garbutt
2017-06-02 17:01:21 +01:00
committed by Andy
parent a3024cec94
commit ed4f31ba67
2 changed files with 52 additions and 2 deletions

View File

@@ -1,6 +1,10 @@
// Type definitions for Sinon 2.2
// Type definitions for Sinon 2.3
// Project: http://sinonjs.org/
// Definitions by: William Sears <https://github.com/mrbigdog2u>, Jonathan Little <https://github.com/rationull>, Lukas Spieß <https://github.com/lumaxis>, Nico Jansen <https://github.com/nicojs>
// Definitions by: William Sears <https://github.com/mrbigdog2u>
// Jonathan Little <https://github.com/rationull>
// Lukas Spieß <https://github.com/lumaxis>
// Nico Jansen <https://github.com/nicojs>
// James Garbutt <https://github.com/43081j>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// sinon uses DOM dependencies which are absent in browser-less environment like node.js
@@ -110,6 +114,9 @@ declare namespace Sinon {
resolves(value?: any): SinonStub;
throws(type?: string): SinonStub;
throws(obj: any): SinonStub;
throwsArg(index: number): SinonStub;
throwsException(type?: string): SinonStub;
throwsException(obj: any): SinonStub;
rejects(): SinonStub;
rejects(errorType: string): SinonStub;
rejects(value: any): SinonStub;
@@ -123,12 +130,16 @@ declare namespace Sinon {
callsArgWithAsync(index: number, ...args: any[]): SinonStub;
callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub;
callsFake(func: (...args: any[]) => void): SinonStub;
get(func: () => any): SinonStub;
set(func: (v: any) => void): SinonStub;
onCall(n: number): SinonStub;
onFirstCall(): SinonStub;
onSecondCall(): SinonStub;
onThirdCall(): SinonStub;
value(val: any): SinonStub;
yields(...args: any[]): SinonStub;
yieldsOn(context: any, ...args: any[]): SinonStub;
yieldsRight(...args: any[]): SinonStub;
yieldsTo(property: string, ...args: any[]): SinonStub;
yieldsToOn(property: string, context: any, ...args: any[]): SinonStub;
yieldsAsync(...args: any[]): SinonStub;

View File

@@ -159,6 +159,41 @@ function testSetMatcher() {
stub.calledWithMatch(sinon.match.set.contains(new Set([true])));
}
function testGetterStub() {
const myObj: any = {
prop: 'foo'
};
const stub = sinon.stub(myObj, 'prop').get(() => 'bar');
stub.restore();
}
function testSetterStub() {
const myObj: any = {
prop: 'foo',
prop2: 'bar'
};
const stub = sinon.stub(myObj, 'prop').set((val: string) => myObj.prop2 = val);
stub.restore();
}
function testValueStub() {
const myObj: any = {
prop: 'foo'
};
const stub = sinon.stub(myObj, 'prop').value('bar');
stub.restore();
}
function testThrowsStub() {
sinon.stub().throws(new Error('foo'));
sinon.stub().throwsException(new Error('foo'));
sinon.stub().throws('foo');
sinon.stub().throwsException('foo');
}
function testSpy() {
const otherSpy = sinon.spy();
sinon.spy().calledAfter(otherSpy);
@@ -182,6 +217,10 @@ testSpy();
testSymbolMatch();
testResetHistory();
testUsingPromises();
testGetterStub();
testSetterStub();
testValueStub();
testThrowsStub();
let clock = sinon.useFakeTimers();
clock.setSystemTime(1000);