-
Notifications
You must be signed in to change notification settings - Fork 93
/
capturing.spec.ts
72 lines (62 loc) · 2.71 KB
/
capturing.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {capture, instance, mock} from "../src/ts-mockito";
import {Foo} from "./utils/Foo";
describe("capturing method arguments", () => {
let mockedFoo: Foo;
let foo: Foo;
beforeEach(() => {
mockedFoo = mock(Foo);
foo = instance(mockedFoo);
});
describe("when method has NOT been called", () => {
it("should NOT try to read call with negative index", () => {
// given
let error;
// then
try {
capture(mockedFoo.concatStringWithNumber).last();
} catch (e) {
error = e;
}
expect(error.message).toEqual('Cannot capture arguments, method has not been called so many times: 0');
});
});
describe("when method has been called", () => {
it("captures all arguments passed to it", () => {
// given
// when
foo.concatStringWithNumber("first", 1);
foo.concatStringWithNumber("second", 2);
foo.concatStringWithNumber("third", 3);
// then
const [firstCapturedValue, secondCapturedValue] = capture(mockedFoo.concatStringWithNumber).first();
expect(firstCapturedValue).toEqual("first");
expect(secondCapturedValue).toEqual(1);
expect(capture(mockedFoo.concatStringWithNumber).first()).toEqual(["first", 1]);
expect(capture(mockedFoo.concatStringWithNumber).second()).toEqual(["second", 2]);
expect(capture(mockedFoo.concatStringWithNumber).third()).toEqual(["third", 3]);
expect(capture(mockedFoo.concatStringWithNumber).beforeLast()).toEqual(["second", 2]);
expect(capture(mockedFoo.concatStringWithNumber).last()).toEqual(["third", 3]);
expect(capture(mockedFoo.concatStringWithNumber).byCallIndex(0)).toEqual(["first", 1]);
expect(capture(mockedFoo.concatStringWithNumber).byCallIndex(1)).toEqual(["second", 2]);
expect(capture(mockedFoo.concatStringWithNumber).byCallIndex(2)).toEqual(["third", 3]);
});
});
describe("when method has been called twice", () => {
describe("but we want check third call arguments", () => {
it("throws error", () => {
// given
foo.concatStringWithNumber("first", 1);
foo.concatStringWithNumber("second", 2);
// when
let error;
try {
capture(mockedFoo.concatStringWithNumber).third();
} catch (e) {
error = e;
}
// then
expect(error.message).toContain("Cannot capture arguments");
});
});
});
});