From 6637048fdc72b676efcf210bc5be8d432c4ce1b1 Mon Sep 17 00:00:00 2001 From: NagRock Date: Mon, 8 Jun 2020 21:43:24 +0200 Subject: [PATCH] Added possibility to reset and resetCalls for multiple mock instances --- README.md | 4 ++++ package.json | 2 +- src/ts-mockito.ts | 8 ++++---- test/reseting.calls.spec.ts | 20 +++++++++++++++++++- test/reseting.spec.ts | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e69e539..7584356 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,8 @@ resetCalls(mockedFoo); verify(mockedFoo.getBar(1)).never(); // has never been called after reset ``` +You can also reset calls of multiple mocks at once `resetCalls(firstMock, secondMock, thirdMock)` + ### Resetting mock Or reset mock call counter with all stubs @@ -227,6 +229,8 @@ verify(mockedFoo.getBar(1)).never(); // has never been called after reset console.log(foo.getBar(1)); // null - previously added stub has been removed ``` +You can also reset multiple mocks at once `reset(firstMock, secondMock, thirdMock)` + ### Capturing method arguments ``` typescript diff --git a/package.json b/package.json index 2ae9948..a28d592 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-mockito", - "version": "2.5.0", + "version": "2.6.0", "description": "Mocking library for TypeScript", "main": "lib/ts-mockito.js", "typings": "lib/ts-mockito", diff --git a/src/ts-mockito.ts b/src/ts-mockito.ts index 6d9b2c6..5b37677 100644 --- a/src/ts-mockito.ts +++ b/src/ts-mockito.ts @@ -73,12 +73,12 @@ export function capture(method: (...args: any[]) => any): ArgCaptor { } } -export function reset(mockedValue: T): void { - (mockedValue as any).__tsmockitoMocker.reset(); +export function reset(...mockedValues: T[]): void { + mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.reset()); } -export function resetCalls(mockedValue: T): void { - (mockedValue as any).__tsmockitoMocker.resetCalls(); +export function resetCalls(...mockedValues: T[]): void { + mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.resetCalls()); } export function anyOfClass(expectedClass: new (...args: any[]) => T): any { diff --git a/test/reseting.calls.spec.ts b/test/reseting.calls.spec.ts index 927a748..4dda5f7 100644 --- a/test/reseting.calls.spec.ts +++ b/test/reseting.calls.spec.ts @@ -1,4 +1,5 @@ -import {anything, instance, mock, resetCalls, verify} from "../src/ts-mockito"; +import { Mocker } from "../src/Mock"; +import { anything, instance, mock, resetCalls, verify } from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; describe("resetting mocked object", () => { @@ -129,4 +130,21 @@ describe("resetting mocked object", () => { }); }); }); + + it("should reset calls of all passed mocks", () => { + if (typeof Proxy === "undefined") { + pending("Testing browser doesn't support Proxy."); + } + + // given + const firstMock = mock(); + const secondMock = mock(); + + // when + resetCalls({__tsmockitoMocker: instance(firstMock)}, {__tsmockitoMocker: instance(secondMock)}); + + // then + verify(firstMock.resetCalls()).once(); + verify(secondMock.resetCalls()).once(); + }); }); diff --git a/test/reseting.spec.ts b/test/reseting.spec.ts index 41ee6e2..df55ad3 100644 --- a/test/reseting.spec.ts +++ b/test/reseting.spec.ts @@ -1,3 +1,4 @@ +import { Mocker } from "../src/Mock"; import {anything, instance, mock, reset, verify, when} from "../src/ts-mockito"; import {Foo} from "./utils/Foo"; @@ -162,4 +163,21 @@ describe("resetting mocked object", () => { }); }); }); + + it("should reset all passed mocks", () => { + if (typeof Proxy === "undefined") { + pending("Testing browser doesn't support Proxy."); + } + + // given + const firstMock = mock(); + const secondMock = mock(); + + // when + reset({__tsmockitoMocker: instance(firstMock)}, {__tsmockitoMocker: instance(secondMock)}); + + // then + verify(firstMock.reset()).once(); + verify(secondMock.reset()).once(); + }); });