Skip to content

Commit 75cd373

Browse files
committed
feat: add expectCallsAndReset()
1 parent 0e57148 commit 75cd373

5 files changed

Lines changed: 58 additions & 1 deletion

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expectCallsAndReset } from "./expect-calls-and-reset";
2+
3+
describe("expectCallsAndReset()", () => {
4+
it("matches arguments", () => {
5+
const spy = jasmine.createSpy();
6+
7+
spy("a thing", "or two");
8+
expectCallsAndReset(spy, ["a thing", "or two"]);
9+
10+
spy();
11+
expectCallsAndReset(spy, []);
12+
13+
spy(1);
14+
spy(2);
15+
spy(3, 4);
16+
expectCallsAndReset(spy, [1], [2], [3, 4]);
17+
});
18+
19+
it("resets the spy", () => {
20+
const spy = jasmine.createSpy();
21+
22+
spy();
23+
expectCallsAndReset(spy, []);
24+
expect(spy).not.toHaveBeenCalled();
25+
26+
spy(1);
27+
expectCallsAndReset(spy, [1]);
28+
expect(spy).not.toHaveBeenCalled();
29+
});
30+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Expects the spy to have been called the exact number of times as the argument lists provided, with those arguments, then resets the spy.
3+
*
4+
* ```ts
5+
* const spy = jasmine.createSpy();
6+
*
7+
* spy(1)
8+
* spy(2, 3)
9+
* spy()
10+
* expectCallsAndReset(spy, [1], [2, 3], []);
11+
* ```
12+
*/
13+
export function expectCallsAndReset(spy: jasmine.Spy, ...allArgs: any[][]) {
14+
expect(spy).toHaveBeenCalledTimes(allArgs.length);
15+
expect(spy.calls.allArgs()).toEqual(allArgs);
16+
spy.calls.reset();
17+
}

projects/s-ng-dev-utils/src/lib/expect-type.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
export function expectType<Expected, Actual extends Expected>(): void;
1111
export function expectType<Expected>(value: Expected): void;
1212

13-
export function expectType(_value?: any) {}
13+
export function expectType(_value?: any) {
14+
expect().nothing();
15+
}

projects/s-ng-dev-utils/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Public API Surface of s-ng-dev-utils
33
*/
44

5+
export { expectCallsAndReset } from "./lib/expect-calls-and-reset";
56
export { expectSingleCallAndReset } from "./lib/expect-single-call-and-reset";
67
export { expectType } from "./lib/expect-type";
78
export { marbleTest } from "./lib/marble-test";

src/app/integration.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
Existant,
3+
expectCallsAndReset,
34
expectSingleCallAndReset,
45
expectType,
56
Falsey,
@@ -17,6 +18,12 @@ import {
1718
} from "s-ng-dev-utils";
1819

1920
describe("s-ng-dev-utils", () => {
21+
it("has expectCallsAndReset()", () => {
22+
const spy = jasmine.createSpy();
23+
spy();
24+
expectCallsAndReset(spy, []);
25+
});
26+
2027
it("has expectSingleCallAndReset()", () => {
2128
const spy = jasmine.createSpy();
2229
spy();

0 commit comments

Comments
 (0)