fix: update definition to match wtih jest-when@2.4.1 (#34755)

This commit is contained in:
Trung Dang 2019-05-02 01:38:17 +08:00 committed by Wesley Wigham
parent bea39a0360
commit b497be0de0
2 changed files with 28 additions and 1 deletions

View File

@ -1,6 +1,7 @@
// Type definitions for jest-when 1.1
// Type definitions for jest-when 2.4
// Project: https://github.com/timkindberg/jest-when#readme
// Definitions by: Alden Taylor <https://github.com/aldentaylor>
// Trung Dang <https://github.com/immanuel192>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
@ -15,6 +16,8 @@ export interface WhenMock<T = any, Y extends any[] = any> extends jest.Mock<T, Y
mockResolvedValueOnce(value: jest.ResolvedValue<T>): WhenMock<T, Y>;
mockRejectedValue(value: jest.RejectedValue<T>): WhenMock<T, Y>;
mockRejectedValueOnce(value: jest.RejectedValue<T>): WhenMock<T, Y>;
mockImplementation(fn: (...args: Y) => T): WhenMock<T, Y>;
mockImplementationOnce(fn?: (...args: Y) => T): WhenMock<T, Y>;
}
export type When = <T, Y extends any[]>(fn: jest.Mock<T, Y>) => WhenMock<T, Y>;

View File

@ -89,4 +89,28 @@ describe('mock-when test', () => {
}
expect(testPassed).toBeTruthy();
});
it('should support for mockImplementation', () => {
const fn = jest.fn();
const expectValue = { a: 1, b: 2 };
when(fn).calledWith(
expect.anything()
).mockImplementation(() => (expectValue));
const result = fn('whatever');
expect(result).toMatchObject(expectValue);
});
it('should support for mockImplementationOnce', () => {
const fn = jest.fn();
const expectValue = { a: 1, b: 2 };
when(fn).calledWith(
expect.anything()
).mockImplementationOnce(() => (expectValue));
const result = fn('whatever');
const result2 = fn('whatever');
expect(result).toMatchObject(expectValue);
expect(result2).toEqual(undefined);
});
});