Merge pull request #26471 from jounisuo/chai-spies-sandbox-restore

[@types/chai-spies] Added possibility to create sandboxes and restore original methods.
This commit is contained in:
Nathan Shively-Sanders
2018-06-18 13:52:44 -07:00
committed by GitHub
2 changed files with 131 additions and 56 deletions

View File

@@ -24,7 +24,12 @@ let array = [ 1, 2, 3 ];
chai.spy.on(array, 'push');
// or you can track multiple object's methods
chai.spy.on(array, 'push', 'pop');
chai.spy.on(array, ['push', 'pop']);
// or you can track multiple object's methods
chai.spy.on(array, 'push', function(item) {
array.push(item);
});
array.push(5);
@@ -149,4 +154,20 @@ spy.should.not.have.been.called.above(3);
expect(spy).to.have.been.called.below(3);
expect(spy).to.not.have.been.called.lt(3);
spy.should.have.been.called.lt(3);
spy.should.not.have.been.called.below(3);
spy.should.not.have.been.called.below(3);
// You can also create sandbox
let sb = chai.spy.sandbox();
sb.on(array, 'pop', () => {
return 1;
})
let one = array.pop();
expect(one).to.equal(1);
// Can restore methods in sandbox
sb.restore();
array.push(2);
let two = array.pop();
expect(two).to.equal(2);

View File

@@ -1,6 +1,8 @@
// Type definitions for chai-spies
// Type definitions for chai-spies 1.0.0
// Project: https://github.com/chaijs/chai-spies
// Definitions by: Ilya Kuznetsov <https://github.com/kuzn-ilya>
// Harm van der Werf <https://github.com/harm-less>
// Jouni Suorsa <https://github.com/jounisuo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="chai" />
@@ -21,7 +23,7 @@ declare namespace Chai {
* ```ts
* expect(spy).to.be.spy;
* spy.should.be.spy;
* ```
* ```
*/
spy: Assertion;
@@ -32,7 +34,7 @@ declare namespace Chai {
* expect(spy).to.have.been.called();
* spy.should.have.been.called();
* ```
* Note that ```called``` can be used as a chainable method.
* Note that ```called``` can be used as a chainable method.
*/
called: ChaiSpies.Called;
@@ -61,7 +63,32 @@ declare namespace Chai {
}
declare namespace ChaiSpies {
interface Sandbox {
/**
* #### chai.spy.on (function)
*
* Wraps an object method into spy. All calls will pass through to the original function.
*
* @param {Object} object
* @param {String} methodNames names to spy on
* @param {function} fn replacement function
* @returns function to actually call
*/
on(object: Object, methodNames: string | string[], fn?: (parameters: any[]|any) => any): any;
/**
* #### chai.spy.restore (function)
*
* Restores previously wrapped object's method.
* Restores all spied objects of a sandbox if called without parameters.
*
* @function
* @param {Object} [object]
* @param {String|String[]} [methods] name or names
* @return {Sandbox} Sandbox instance
*/
restore(object?: Object, methodNames?: string | string[]): void;
}
interface Spy {
/**
* #### chai.spy (function)
@@ -72,9 +99,9 @@ declare namespace ChaiSpies {
* var spy = chai.spy(original)
* , e_spy = chai.spy();
* ```
* @param fn function to spy on. @default ```function () {}```
* @param fn function to spy on. @default ```function () {}```
* @returns function to actually call
*/
*/
(): SpyFunc0Proxy<void>;
<R>(fn: SpyFunc0<R>): SpyFunc0Proxy<R>;
<A1, R>(fn: SpyFunc1<A1, R>): SpyFunc1Proxy<A1, R>;
@@ -107,10 +134,11 @@ declare namespace ChaiSpies {
* var spy = chai.spy.on(Array, 'isArray');
* ```
* @param {Object} object
* @param {String} method name to spy on
* @param {String} method names to spy on
* @param {function} fn replacement function
* @returns function to actually call
*/
on(object: Object, ...methodNames: string[]): any;
*/
on(object: Object, methodNames: string | string[], fn?: (parameters: any[]|any) => any): any;
/**
* #### chai.spy.object (function)
@@ -123,10 +151,24 @@ declare namespace ChaiSpies {
* @param {String[]|Object} method names or method definitions
* @returns object with spied methods
*/
object(name: string, methods: string[]): any;
object(methods: string[]): any;
object<T>(name: string, methods: T): T;
object<T>(methods: T): T;
object(name: string, methods: string[]): any;
object(methods: string[]): any;
object<T>(name: string, methods: T): T;
object<T>(methods: T): T;
/**
* #### chai.spy.restore (function)
*
* Restores spy assigned to DEFAULT sandbox
*
* Restores previously wrapped object's method.
* Restores all spied objects of a sandbox if called without parameters.
*
* @param {Object} [object]
* @param {String|String[]} [methods] name or names
* @return {Sandbox} Sandbox instance
*/
restore(object?: Object, methodNames?: string | string[]): void;
/**
* #### chai.spy.returns (function)
@@ -141,6 +183,18 @@ declare namespace ChaiSpies {
*/
returns<T>(value: T): SpyFunc0Proxy<T>;
/**
* ### chai.spy.sandbox
*
* Creates a sandbox.
*
* Sandbox is a set of spies.
* Sandbox allows to track methods on objects and restore original methods with on restore call.
*
* @returns {Sandbox}
*/
sandbox(): Sandbox;
}
interface Called {
@@ -158,12 +212,12 @@ declare namespace ChaiSpies {
* spy.should.not.have.been.called.once;
* ```
*/
once: Chai.Assertion;
once: Chai.Assertion;
/**
* ####.twice
* Assert that a spy has been called exactly twice.
* ```ts
* ```ts
* expect(spy).to.have.been.called.twice;
* expect(spy).to.not.have.been.called.twice;
* spy.should.have.been.called.twice;
@@ -215,7 +269,7 @@ declare namespace ChaiSpies {
* ```ts
* expect(spy).to.have.been.called.above(3);
* spy.should.not.have.been.called.above(3);
* ```
* ```
*/
above(n: number): Chai.Assertion;
@@ -225,7 +279,7 @@ declare namespace ChaiSpies {
* ```ts
* expect(spy).to.have.been.called.gt(3);
* spy.should.not.have.been.called.gt(3);
* ```
* ```
*/
gt(n: number): Chai.Assertion;
@@ -235,7 +289,7 @@ declare namespace ChaiSpies {
* ```ts
* expect(spy).to.have.been.called.below(3);
* spy.should.not.have.been.called.below(3);
* ```
* ```
*/
below(n: number): Chai.Assertion;
@@ -245,7 +299,7 @@ declare namespace ChaiSpies {
* ```ts
* expect(spy).to.have.been.called.lt(3);
* spy.should.not.have.been.called.lt(3);
* ```
* ```
*/
lt(n: number): Chai.Assertion;
}
@@ -301,7 +355,7 @@ declare namespace ChaiSpies {
* spy.should.have.been.called.with('foo');
* ```
* Will also pass for ```spy('foo', 'bar')``` and ```spy(); spy('foo')```.
* If used with multiple arguments, assert that a spy has been called with all the given arguments at least once.
* If used with multiple arguments, assert that a spy has been called with all the given arguments at least once.
* ```ts
* spy('foo', 'bar', 1);
* expect(spy).to.have.been.called.with('bar', 'foo');
@@ -389,7 +443,7 @@ declare namespace ChaiSpies {
*
* Resets __spy object parameters for instantiation and reuse
* @returns proxy spy object
*/
*/
reset(): this;
}
@@ -397,77 +451,77 @@ declare namespace ChaiSpies {
(): R;
}
interface SpyFunc1<A1, R> {
(a: A1): R;
interface SpyFunc1<A1, R> {
(a: A1): R;
}
interface SpyFunc2<A1, A2, R> {
(a: A1, b: A2): R;
interface SpyFunc2<A1, A2, R> {
(a: A1, b: A2): R;
}
interface SpyFunc3<A1, A2, A3, R> {
(a: A1, b: A2, c: A3): R;
interface SpyFunc3<A1, A2, A3, R> {
(a: A1, b: A2, c: A3): R;
}
interface SpyFunc4<A1, A2, A3, A4, R> {
(a: A1, b: A2, c: A3, d: A4): R;
interface SpyFunc4<A1, A2, A3, A4, R> {
(a: A1, b: A2, c: A3, d: A4): R;
}
interface SpyFunc5<A1, A2, A3, A4, A5, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5): R;
interface SpyFunc5<A1, A2, A3, A4, A5, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5): R;
}
interface SpyFunc6<A1, A2, A3, A4, A5, A6, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6): R;
interface SpyFunc6<A1, A2, A3, A4, A5, A6, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6): R;
}
interface SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7): R;
interface SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7): R;
}
interface SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8): R;
interface SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8): R;
}
interface SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9): R;
interface SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9): R;
}
interface SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9, j: A10): R;
interface SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> {
(a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9, j: A10): R;
}
interface SpyFunc0Proxy<R> extends SpyFunc0<R>, Resetable {
}
interface SpyFunc1Proxy<A1, R> extends SpyFunc1<A1, R>, Resetable {
interface SpyFunc1Proxy<A1, R> extends SpyFunc1<A1, R>, Resetable {
}
interface SpyFunc2Proxy<A1, A2, R> extends SpyFunc2<A1, A2, R>, Resetable {
interface SpyFunc2Proxy<A1, A2, R> extends SpyFunc2<A1, A2, R>, Resetable {
}
interface SpyFunc3Proxy<A1, A2, A3, R> extends SpyFunc3<A1, A2, A3, R>, Resetable {
interface SpyFunc3Proxy<A1, A2, A3, R> extends SpyFunc3<A1, A2, A3, R>, Resetable {
}
interface SpyFunc4Proxy<A1, A2, A3, A4, R> extends SpyFunc4<A1, A2, A3, A4, R>, Resetable {
interface SpyFunc4Proxy<A1, A2, A3, A4, R> extends SpyFunc4<A1, A2, A3, A4, R>, Resetable {
}
interface SpyFunc5Proxy<A1, A2, A3, A4, A5, R> extends SpyFunc5<A1, A2, A3, A4, A5, R>, Resetable {
interface SpyFunc5Proxy<A1, A2, A3, A4, A5, R> extends SpyFunc5<A1, A2, A3, A4, A5, R>, Resetable {
}
interface SpyFunc6Proxy<A1, A2, A3, A4, A5, A6, R> extends SpyFunc6<A1, A2, A3, A4, A5, A6, R>, Resetable {
interface SpyFunc6Proxy<A1, A2, A3, A4, A5, A6, R> extends SpyFunc6<A1, A2, A3, A4, A5, A6, R>, Resetable {
}
interface SpyFunc7Proxy<A1, A2, A3, A4, A5, A6, A7, R> extends SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R>, Resetable {
interface SpyFunc7Proxy<A1, A2, A3, A4, A5, A6, A7, R> extends SpyFunc7<A1, A2, A3, A4, A5, A6, A7, R>, Resetable {
}
interface SpyFunc8Proxy<A1, A2, A3, A4, A5, A6, A7, A8, R> extends SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R>, Resetable {
interface SpyFunc8Proxy<A1, A2, A3, A4, A5, A6, A7, A8, R> extends SpyFunc8<A1, A2, A3, A4, A5, A6, A7, A8, R>, Resetable {
}
interface SpyFunc9Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> extends SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>, Resetable {
interface SpyFunc9Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, R> extends SpyFunc9<A1, A2, A3, A4, A5, A6, A7, A8, A9, R>, Resetable {
}
interface SpyFunc10Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> extends SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>, Resetable {
interface SpyFunc10Proxy<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R> extends SpyFunc10<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, R>, Resetable {
}
}