Skip to content

Commit

Permalink
Added possibility to reset and resetCalls for multiple mock instances
Browse files Browse the repository at this point in the history
  • Loading branch information
NagRock committed Jun 8, 2020
1 parent d718e55 commit 6637048
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 4 additions & 4 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export function capture(method: (...args: any[]) => any): ArgCaptor {
}
}

export function reset<T>(mockedValue: T): void {
(mockedValue as any).__tsmockitoMocker.reset();
export function reset<T>(...mockedValues: T[]): void {
mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.reset());
}

export function resetCalls<T>(mockedValue: T): void {
(mockedValue as any).__tsmockitoMocker.resetCalls();
export function resetCalls<T>(...mockedValues: T[]): void {
mockedValues.forEach(mockedValue => (mockedValue as any).__tsmockitoMocker.resetCalls());
}

export function anyOfClass<T>(expectedClass: new (...args: any[]) => T): any {
Expand Down
20 changes: 19 additions & 1 deletion test/reseting.calls.spec.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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<Mocker>();
const secondMock = mock<Mocker>();

// when
resetCalls({__tsmockitoMocker: instance(firstMock)}, {__tsmockitoMocker: instance(secondMock)});

// then
verify(firstMock.resetCalls()).once();
verify(secondMock.resetCalls()).once();
});
});
18 changes: 18 additions & 0 deletions test/reseting.spec.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<Mocker>();
const secondMock = mock<Mocker>();

// when
reset({__tsmockitoMocker: instance(firstMock)}, {__tsmockitoMocker: instance(secondMock)});

// then
verify(firstMock.reset()).once();
verify(secondMock.reset()).once();
});
});

0 comments on commit 6637048

Please sign in to comment.