Make Mock interface generic

And fill out the rest of the global interfaces
This commit is contained in:
Josh Smith
2014-09-24 17:47:14 -07:00
parent b497b77d6d
commit 63caa5d4b9
2 changed files with 47 additions and 14 deletions

View File

@@ -96,4 +96,28 @@ describe('CheckboxWithLabel', function() {
TestUtils.Simulate.change(input);
expect(label.getDOMNode().textContent).toEqual('On');
});
});
});
function testInstances() {
var mockFn = jest.genMockFunction<Function>();
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<Function>().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
}

35
jest/jest.d.ts vendored
View File

@@ -3,22 +3,30 @@
// Definitions by: Phips Peter <https://github.com/pspeter3/>
// 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<T>(moduleName: string): Mock<T>;
function genMockFunction<T>(): Mock<T>;
function genMockFn<T>(): Mock<T>;
function mock(moduleName: string): void;
function runAllTicks(): void;
function runAllTimers(): void;
function runOnlyPendingTimers(): void;
function setMock<T>(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<T> {
new(): T;
mock: MockContext<T>;
mockClear(): void;
mockImplementation(fn: Function): void;
mockImpl(fn: Function): void;
mockReturnThis(): void;
mockReturnValue(value: any): void;
mockReturnValueOnce(value: any): void;
mockImplementation(fn: Function): Mock<T>;
mockImpl(fn: Function): Mock<T>;
mockReturnThis(): Mock<T>;
mockReturnValue(value: any): Mock<T>;
mockReturnValueOnce(value: any): Mock<T>;
}
interface MockContext {
interface MockContext<T> {
calls: any[][];
instances: any[][];
instances: T[];
}
}