From 63caa5d4b93651ec1130a6a2cca5fe15bc13d117 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Wed, 24 Sep 2014 17:47:14 -0700 Subject: [PATCH] Make Mock interface generic And fill out the rest of the global interfaces --- jest/jest-tests.ts | 26 +++++++++++++++++++++++++- jest/jest.d.ts | 35 ++++++++++++++++++++++------------- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/jest/jest-tests.ts b/jest/jest-tests.ts index c734e34a8a..764daf142a 100644 --- a/jest/jest-tests.ts +++ b/jest/jest-tests.ts @@ -96,4 +96,28 @@ describe('CheckboxWithLabel', function() { TestUtils.Simulate.change(input); expect(label.getDOMNode().textContent).toEqual('On'); }); -}); \ No newline at end of file +}); + +function testInstances() { + var mockFn = jest.genMockFunction(); + var a = new mockFn(); + var b = new mockFn(); + + mockFn.mock.instances[0] === a; // true + mockFn.mock.instances[1] === b; // true +} + +function testMockImplementation() { + var mockFn = jest.genMockFunction().mockImplementation(function (scalar:number):number { + return 42 + scalar; + }); + + var a = mockFn(0); + var b = mockFn(1); + + a === 42; // true + b === 43; // true + + mockFn.mock.calls[0][0] === 0; // true + mockFn.mock.calls[1][0] === 1; // true +} \ No newline at end of file diff --git a/jest/jest.d.ts b/jest/jest.d.ts index 190a3a67e6..bf4a0cc50b 100644 --- a/jest/jest.d.ts +++ b/jest/jest.d.ts @@ -3,22 +3,30 @@ // Definitions by: Phips Peter // Definitions: https://github.com/borisyankov/DefinitelyTyped +declare function afterEach(fn: jest.EmptyFunction): void; +declare function beforeEach(fn: jest.EmptyFunction): void; declare function describe(name: string, fn: jest.EmptyFunction): void; -declare function expect(actual: any): jest.Matchers; declare var it: jest.It; +declare function pit(name: string, fn: jest.EmptyFunction): void; declare var require: jest.Require; +declare function xdescribe(name: string, fn: jest.EmptyFunction): void; +declare function xit(name: string, fn: jest.EmptyFunction): void; + +declare function expect(actual: any): jest.Matchers; declare module jest { function autoMockOff(): void; function autoMockOn(): void; function clearAllTimers(): void; function dontMock(moduleName: string): void; - function genMockFromModule(moduleName: string): Mock; - function genMockFunction(): Mock; - function genMockFn(): Mock; + function genMockFromModule(moduleName: string): Mock; + function genMockFunction(): Mock; + function genMockFn(): Mock; function mock(moduleName: string): void; function runAllTicks(): void; function runAllTimers(): void; + function runOnlyPendingTimers(): void; + function setMock(moduleName: string, moduleExports: T): void; interface EmptyFunction { (): void; @@ -53,18 +61,19 @@ declare module jest { requireActual(moduleName: string): any; } - interface Mock { - mock: MockContext; + interface Mock { + new(): T; + mock: MockContext; mockClear(): void; - mockImplementation(fn: Function): void; - mockImpl(fn: Function): void; - mockReturnThis(): void; - mockReturnValue(value: any): void; - mockReturnValueOnce(value: any): void; + mockImplementation(fn: Function): Mock; + mockImpl(fn: Function): Mock; + mockReturnThis(): Mock; + mockReturnValue(value: any): Mock; + mockReturnValueOnce(value: any): Mock; } - interface MockContext { + interface MockContext { calls: any[][]; - instances: any[][]; + instances: T[]; } } \ No newline at end of file