diff --git a/types/chai-spies/chai-spies-tests.ts b/types/chai-spies/chai-spies-tests.ts index f5247ba13f..7bc9530164 100644 --- a/types/chai-spies/chai-spies-tests.ts +++ b/types/chai-spies/chai-spies-tests.ts @@ -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); \ No newline at end of file +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); diff --git a/types/chai-spies/index.d.ts b/types/chai-spies/index.d.ts index 75f702ca37..bb37fa8cd4 100644 --- a/types/chai-spies/index.d.ts +++ b/types/chai-spies/index.d.ts @@ -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 +// Harm van der Werf +// Jouni Suorsa // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -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; (fn: SpyFunc0): SpyFunc0Proxy; (fn: SpyFunc1): SpyFunc1Proxy; @@ -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(name: string, methods: T): T; - object(methods: T): T; + object(name: string, methods: string[]): any; + object(methods: string[]): any; + object(name: string, methods: T): T; + object(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(value: T): SpyFunc0Proxy; + + /** + * ### 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 { - (a: A1): R; + interface SpyFunc1 { + (a: A1): R; } - interface SpyFunc2 { - (a: A1, b: A2): R; + interface SpyFunc2 { + (a: A1, b: A2): R; } - interface SpyFunc3 { - (a: A1, b: A2, c: A3): R; + interface SpyFunc3 { + (a: A1, b: A2, c: A3): R; } - interface SpyFunc4 { - (a: A1, b: A2, c: A3, d: A4): R; + interface SpyFunc4 { + (a: A1, b: A2, c: A3, d: A4): R; } - interface SpyFunc5 { - (a: A1, b: A2, c: A3, d: A4, e: A5): R; + interface SpyFunc5 { + (a: A1, b: A2, c: A3, d: A4, e: A5): R; } - interface SpyFunc6 { - (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6): R; + interface SpyFunc6 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6): R; } - interface SpyFunc7 { - (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7): R; + interface SpyFunc7 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7): R; } - interface SpyFunc8 { - (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8): R; + interface SpyFunc8 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8): R; } - - interface SpyFunc9 { - (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9): R; + + interface SpyFunc9 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9): R; } - - interface SpyFunc10 { - (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9, j: A10): R; + + interface SpyFunc10 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9, j: A10): R; } interface SpyFunc0Proxy extends SpyFunc0, Resetable { } - interface SpyFunc1Proxy extends SpyFunc1, Resetable { + interface SpyFunc1Proxy extends SpyFunc1, Resetable { } - interface SpyFunc2Proxy extends SpyFunc2, Resetable { + interface SpyFunc2Proxy extends SpyFunc2, Resetable { } - interface SpyFunc3Proxy extends SpyFunc3, Resetable { + interface SpyFunc3Proxy extends SpyFunc3, Resetable { } - interface SpyFunc4Proxy extends SpyFunc4, Resetable { + interface SpyFunc4Proxy extends SpyFunc4, Resetable { } - interface SpyFunc5Proxy extends SpyFunc5, Resetable { + interface SpyFunc5Proxy extends SpyFunc5, Resetable { } - interface SpyFunc6Proxy extends SpyFunc6, Resetable { + interface SpyFunc6Proxy extends SpyFunc6, Resetable { } - interface SpyFunc7Proxy extends SpyFunc7, Resetable { + interface SpyFunc7Proxy extends SpyFunc7, Resetable { } - interface SpyFunc8Proxy extends SpyFunc8, Resetable { + interface SpyFunc8Proxy extends SpyFunc8, Resetable { } - - interface SpyFunc9Proxy extends SpyFunc9, Resetable { + + interface SpyFunc9Proxy extends SpyFunc9, Resetable { } - - interface SpyFunc10Proxy extends SpyFunc10, Resetable { + + interface SpyFunc10Proxy extends SpyFunc10, Resetable { } }