1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const function_spy_call_count_matcher_1 = require("./function-spy-call-count-matcher");
|
4 | const spy_call_count_type_1 = require("./spy-call-count-type");
|
5 | const errors_1 = require("../errors");
|
6 | const stringification_1 = require("../stringification");
|
7 | class FunctionSpyMatcher {
|
8 | constructor(spy, expectedArguments) {
|
9 | this._expectedArguments = null;
|
10 | if (spy === null || spy === undefined) {
|
11 | throw new TypeError("spy must not be null or undefined.");
|
12 | }
|
13 | if (expectedArguments) {
|
14 | this._expectedArguments = expectedArguments;
|
15 | }
|
16 | this._spy = spy;
|
17 | }
|
18 | exactly(expectedCallCount) {
|
19 | return this._match(count => count !== expectedCallCount, expectedCallCount, "expectedCallCount", spy_call_count_type_1.SpyCallCountType.Exactly, true);
|
20 | }
|
21 | anythingBut(unexpectedCallCount) {
|
22 | return this._match(count => count === unexpectedCallCount, unexpectedCallCount, "unexpectedCallCount", spy_call_count_type_1.SpyCallCountType.Exactly, false);
|
23 | }
|
24 | greaterThan(minimumCallCount) {
|
25 | return this._match(count => count <= minimumCallCount, minimumCallCount, "minimumCallCount", spy_call_count_type_1.SpyCallCountType.GreaterThan, true);
|
26 | }
|
27 | lessThan(maximumCallCount) {
|
28 | return this._match(count => count >= maximumCallCount, maximumCallCount, "maximumCallCount", spy_call_count_type_1.SpyCallCountType.LessThan, true);
|
29 | }
|
30 | _validateCallCount(callCount, callCountName) {
|
31 | if (callCount < 1) {
|
32 | throw new TypeError(`${callCountName} must be greater than 0.`);
|
33 | }
|
34 | }
|
35 | _matchingCallsCount() {
|
36 | if (this._expectedArguments === null) {
|
37 | return this._spy.calls.length;
|
38 | }
|
39 | return this._matchingArguments();
|
40 | }
|
41 | _matchingArguments() {
|
42 | return this._spy.callsWithArguments.apply(this._spy, this._expectedArguments).length;
|
43 | }
|
44 | _match(countIsNotCorrect, callCount, callCountName, callCountType, shouldMatch) {
|
45 | this._validateCallCount(callCount, callCountName);
|
46 | const actualCallCount = this._matchingCallsCount();
|
47 | if (countIsNotCorrect(actualCallCount)) {
|
48 | throw new errors_1.MatchError(`Expected function ${!shouldMatch ? "not " : ""}to be called` +
|
49 | `${this._expectedArguments ? " with " + stringification_1.stringify(this._expectedArguments) : ""}` +
|
50 | `${this.stringifyExpectedCallCount(callCount, callCountType)}.`, `function ${!shouldMatch ? "not " : ""}to be called${this.stringifyExpectedCallCount(callCount, callCountType)}.`, `function was called ${this.stringifyCallCount(actualCallCount)}.`, {
|
51 | actualCallCount: stringification_1.stringify(actualCallCount),
|
52 | expectedCallCount: this.stringifyExpectedCallCount(callCount, callCountType),
|
53 | expectedArguments: stringification_1.stringify(this._expectedArguments)
|
54 | });
|
55 | }
|
56 | return new function_spy_call_count_matcher_1.FunctionSpyCallCountMatcher();
|
57 | }
|
58 | stringifyExpectedCallCount(callCount, callCountType) {
|
59 | return `${this.stringifyCallCountType(callCountType)} ${this.stringifyCallCount(callCount)}`;
|
60 | }
|
61 | stringifyCallCount(callCount) {
|
62 | return `${callCount} time${callCount === 1 ? "" : "s"}`;
|
63 | }
|
64 | stringifyCallCountType(callCountType) {
|
65 | switch (callCountType) {
|
66 | case spy_call_count_type_1.SpyCallCountType.GreaterThan:
|
67 | return " greater than";
|
68 | case spy_call_count_type_1.SpyCallCountType.LessThan:
|
69 | return " less than";
|
70 | default:
|
71 | return "";
|
72 | }
|
73 | }
|
74 | }
|
75 | exports.FunctionSpyMatcher = FunctionSpyMatcher;
|
76 |
|
\ | No newline at end of file |