UNPKG

6.08 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const spying_1 = require("../spying");
13const function_spy_matcher_1 = require("./function-spy-matcher");
14const matcher_1 = require("./matcher");
15const stringification_1 = require("../stringification");
16class FunctionMatcher extends matcher_1.Matcher {
17 toThrow() {
18 const error = this._getError();
19 this._registerMatcher((error === null) !== this.shouldMatch, `Expected an error ` +
20 `${this.shouldMatch ? "" : "not "}to be thrown ` +
21 `but ${this.shouldMatch ? "no errors were" : "an error was"} thrown.`, this.shouldMatch ? "an error thrown" : "no errors thrown", {
22 errorThrown: error ? error.toString() : "none"
23 });
24 }
25 toThrowAsync() {
26 return __awaiter(this, void 0, void 0, function* () {
27 const error = yield this._getAsyncError();
28 this._registerMatcher((error === null) !== this.shouldMatch, `Expected an error ` +
29 `${this.shouldMatch ? "" : "not "}to be thrown ` +
30 `but ${this.shouldMatch ? "no errors were" : "an error was"} thrown.`, this.shouldMatch ? "an error thrown" : "no errors thrown", {
31 errorThrown: error ? error.toString() : "none"
32 });
33 });
34 }
35 toThrowError(errorType, errorMessage) {
36 const error = this._getError();
37 this._errorMatches(error, errorType, errorMessage);
38 }
39 toThrowErrorAsync(errorType, errorMessage) {
40 return __awaiter(this, void 0, void 0, function* () {
41 const error = yield this._getAsyncError();
42 this._errorMatches(error, errorType, errorMessage);
43 });
44 }
45 toHaveBeenCalled() {
46 if (this._isFunctionSpyOrSpiedOnFunction(this.actualValue) === false) {
47 throw new TypeError("toHaveBeenCalled requires value passed in to Expect to be a FunctionSpy or a spied on function.");
48 }
49 const spy = this.actualValue;
50 this._registerMatcher((spy.calls.length === 0) !== this.shouldMatch, `Expected function ${!this.shouldMatch ? "not " : ""}to be called.`, `function ${!this.shouldMatch ? "not " : ""}to be called`);
51 return new function_spy_matcher_1.FunctionSpyMatcher(spy);
52 }
53 toHaveBeenCalledWith(...expectedArguments) {
54 if (this._isFunctionSpyOrSpiedOnFunction(this.actualValue) === false) {
55 throw new TypeError("toHaveBeenCalledWith requires value passed in to Expect to be a FunctionSpy or a spied on function.");
56 }
57 const spy = this.actualValue;
58 this._registerMatcher(spy.calls.some(call => this._callArgumentsMatch(call, expectedArguments)) === this.shouldMatch, `Expected function ${!this.shouldMatch ? "not " : ""}to be called` +
59 `${this._stringifyArguments(expectedArguments)}.`, `function ${!this.shouldMatch ? "not " : ""}to be called` +
60 `${this._stringifyArguments(expectedArguments)}.`, {
61 expectedArguments: stringification_1.stringify(expectedArguments),
62 actualArguments: stringification_1.stringify(spy.calls.map(call => call.args))
63 });
64 return new function_spy_matcher_1.FunctionSpyMatcher(spy, expectedArguments);
65 }
66 _stringifyArguments(expectedArguments) {
67 return expectedArguments ? ` with ${stringification_1.stringify(expectedArguments)}` : "";
68 }
69 _getError() {
70 try {
71 this.actualValue();
72 return null;
73 }
74 catch (error) {
75 return error;
76 }
77 }
78 _getAsyncError() {
79 return __awaiter(this, void 0, void 0, function* () {
80 try {
81 yield this.actualValue();
82 return null;
83 }
84 catch (error) {
85 return error;
86 }
87 });
88 }
89 _errorMatches(error, errorType, errorMessage) {
90 const threwRightError = error instanceof errorType && error.message === errorMessage;
91 this._registerMatcher(threwRightError === this.shouldMatch, `Expected an error with ` +
92 `${errorMessage ? `message "${errorMessage}"` : ""} ` +
93 `${errorMessage && errorType ? "and " : ""}` +
94 `${errorType ? `type ${errorType.name} to ${!this.shouldMatch ? "not " : ""}` : ""}` +
95 `have been thrown, but it was${!this.shouldMatch ? "" : "n't"}.`, this.shouldMatch ? "an error of the right type thrown" : "no errors of type thrown", {
96 actualError: error ? error.toString() : "none",
97 expectedError: errorType.name,
98 expectedErrorMessage: errorMessage
99 });
100 }
101 _callArgumentsMatch(call, expectedArguments) {
102 if (call.args.length !== expectedArguments.length) {
103 return false;
104 }
105 return call.args.every((arg, index) => {
106 const expectedArgument = expectedArguments[index];
107 return (arg === expectedArgument ||
108 expectedArgument === spying_1.Any ||
109 (expectedArgument instanceof spying_1.TypeMatcher &&
110 expectedArgument.test(arg)));
111 });
112 }
113 _isFunctionSpyOrSpiedOnFunction(value) {
114 return (value instanceof spying_1.FunctionSpy ||
115 (value instanceof Function && value.calls !== undefined));
116 }
117}
118exports.FunctionMatcher = FunctionMatcher;
119//# sourceMappingURL=function-matcher.js.map
\No newline at end of file