Skip to content

Commit 6637048

Browse files
committed
Added possibility to reset and resetCalls for multiple mock instances
1 parent d718e55 commit 6637048

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ resetCalls(mockedFoo);
202202
verify(mockedFoo.getBar(1)).never(); // has never been called after reset
203203
```
204204
205+
You can also reset calls of multiple mocks at once `resetCalls(firstMock, secondMock, thirdMock)`
206+
205207
### Resetting mock
206208
207209
Or reset mock call counter with all stubs
@@ -227,6 +229,8 @@ verify(mockedFoo.getBar(1)).never(); // has never been called after reset
227229
console.log(foo.getBar(1)); // null - previously added stub has been removed
228230
```
229231
232+
You can also reset multiple mocks at once `reset(firstMock, secondMock, thirdMock)`
233+
230234
### Capturing method arguments
231235
232236
``` typescript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-mockito",
3-
"version": "2.5.0",
3+
"version": "2.6.0",
44
"description": "Mocking library for TypeScript",
55
"main": "lib/ts-mockito.js",
66
"typings": "lib/ts-mockito",

src/ts-mockito.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ export function capture(method: (...args: any[]) => any): ArgCaptor {
7373
}
7474
}
7575

76-
export function reset<T>(mockedValue: T): void {
77-
(mockedValue as any).__tsmockitoMocker.reset();
76+
export function reset<T>(...mockedValues: T[]): void {
77+
mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.reset());
7878
}
7979

80-
export function resetCalls<T>(mockedValue: T): void {
81-
(mockedValue as any).__tsmockitoMocker.resetCalls();
80+
export function resetCalls<T>(...mockedValues: T[]): void {
81+
mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.resetCalls());
8282
}
8383

8484
export function anyOfClass<T>(expectedClass: new (...args: any[]) => T): any {

test/reseting.calls.spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {anything, instance, mock, resetCalls, verify} from "../src/ts-mockito";
1+
import { Mocker } from "../src/Mock";
2+
import { anything, instance, mock, resetCalls, verify } from "../src/ts-mockito";
23
import {Foo} from "./utils/Foo";
34

45
describe("resetting mocked object", () => {
@@ -129,4 +130,21 @@ describe("resetting mocked object", () => {
129130
});
130131
});
131132
});
133+
134+
it("should reset calls of all passed mocks", () => {
135+
if (typeof Proxy === "undefined") {
136+
pending("Testing browser doesn't support Proxy.");
137+
}
138+
139+
// given
140+
const firstMock = mock<Mocker>();
141+
const secondMock = mock<Mocker>();
142+
143+
// when
144+
resetCalls({__tsmockitoMocker: instance(firstMock)}, {__tsmockitoMocker: instance(secondMock)});
145+
146+
// then
147+
verify(firstMock.resetCalls()).once();
148+
verify(secondMock.resetCalls()).once();
149+
});
132150
});

test/reseting.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Mocker } from "../src/Mock";
12
import {anything, instance, mock, reset, verify, when} from "../src/ts-mockito";
23
import {Foo} from "./utils/Foo";
34

@@ -162,4 +163,21 @@ describe("resetting mocked object", () => {
162163
});
163164
});
164165
});
166+
167+
it("should reset all passed mocks", () => {
168+
if (typeof Proxy === "undefined") {
169+
pending("Testing browser doesn't support Proxy.");
170+
}
171+
172+
// given
173+
const firstMock = mock<Mocker>();
174+
const secondMock = mock<Mocker>();
175+
176+
// when
177+
reset({__tsmockitoMocker: instance(firstMock)}, {__tsmockitoMocker: instance(secondMock)});
178+
179+
// then
180+
verify(firstMock.reset()).once();
181+
verify(secondMock.reset()).once();
182+
});
165183
});

0 commit comments

Comments
 (0)