Improved jasmine SpyObj type

Only object methods will be merged with a Spy
This commit is contained in:
Domas Trijonis 2018-12-18 22:36:13 +01:00
parent f25ce87763
commit d89ab9a678
4 changed files with 36 additions and 8 deletions

View File

@ -9,6 +9,7 @@
// Chris Yungmann <https://github.com/cyungmann>
// Giles Roadnight <https://github.com/Roaders>
// Yaroslav Admin <https://github.com/devoto13>
// Domas Trijonis <https://github.com/fdim>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// For ddescribe / iit use : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/karma-jasmine/karma-jasmine.d.ts
@ -643,8 +644,8 @@ declare namespace jasmine {
withArgs(...args: any[]): Spy;
}
type SpyObj<T> = T & {
[k in keyof T]: Spy;
type SpyObj<T> = {
[k in keyof T]: T[k] extends Function ? T[k] & Spy : T[k];
}
interface SpyAnd {

View File

@ -701,11 +701,20 @@ describe("A spy, when created manually", () => {
});
describe("Multiple spies, when created manually", () => {
var tape: any;
interface Tape {
play(): void;
pause(): void;
rewind(pos: number): void;
stop(): void;
readonly isPlaying: boolean; // spy obj makes this writable
}
var tape: jasmine.SpyObj<Tape>;
var el: jasmine.SpyObj<Element>;
beforeEach(() => {
tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
tape = jasmine.createSpyObj<Tape>('tape', ['play', 'pause', 'stop', 'rewind']);
(tape as { isPlaying: boolean }).isPlaying = false;
el = jasmine.createSpyObj<Element>('Element', ['hasAttribute']);
el.hasAttribute.and.returnValue(false);
@ -733,6 +742,10 @@ describe("Multiple spies, when created manually", () => {
it("tracks all the arguments of its calls", () => {
expect(tape.rewind).toHaveBeenCalledWith(0);
});
it("read isPlaying property", () => {
expect(tape.isPlaying).toBe(false);
})
});
describe("jasmine.nothing", () => {

View File

@ -8,6 +8,7 @@
// Boris Breuer <https://github.com/Engineer2B>
// Chris Yungmann <https://github.com/cyungmann>
// Yaroslav Admin <https://github.com/devoto13>
// Domas Trijonis <https://github.com/fdim>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// For ddescribe / iit use : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/karma-jasmine/karma-jasmine.d.ts
@ -628,8 +629,8 @@ declare namespace jasmine {
calls: Calls;
}
type SpyObj<T> = T & {
[k in keyof T]: Spy;
type SpyObj<T> = {
[k in keyof T]: T[k] extends Function ? T[k] & Spy : T[k];
}
interface SpyAnd {

View File

@ -643,11 +643,20 @@ describe("A spy, when created manually", () => {
});
describe("Multiple spies, when created manually", () => {
var tape: any;
interface Tape {
play(): void;
pause(): void;
rewind(pos: number): void;
stop(): void;
readonly isPlaying: boolean; // spy obj makes this writable
}
var tape: jasmine.SpyObj<Tape>;
var el: jasmine.SpyObj<Element>;
beforeEach(() => {
tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
tape = jasmine.createSpyObj<Tape>('tape', ['play', 'pause', 'stop', 'rewind']);
(tape as { isPlaying: boolean }).isPlaying = false;
el = jasmine.createSpyObj<Element>('Element', ['hasAttribute']);
el.hasAttribute.and.returnValue(false);
@ -675,6 +684,10 @@ describe("Multiple spies, when created manually", () => {
it("tracks all the arguments of its calls", () => {
expect(tape.rewind).toHaveBeenCalledWith(0);
});
it("read isPlaying property", () => {
expect(tape.isPlaying).toBe(false);
})
});
describe("jasmine.nothing", () => {