UNPKG

5.53 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const IExpectation_1 = require("./assertionLibraries/IExpectation");
4var ExpectedMessageType;
5(function (ExpectedMessageType) {
6 ExpectedMessageType[ExpectedMessageType["String"] = 0] = "String";
7 ExpectedMessageType[ExpectedMessageType["IMessage"] = 1] = "IMessage";
8 ExpectedMessageType[ExpectedMessageType["Regex"] = 2] = "Regex";
9})(ExpectedMessageType = exports.ExpectedMessageType || (exports.ExpectedMessageType = {}));
10function getExpectedMessageType(expectedResponseCollection) {
11 const firstElt = expectedResponseCollection[0];
12 if (typeof firstElt === 'string') {
13 return ExpectedMessageType.String;
14 }
15 else if (firstElt.constructor.name === 'RegExp') {
16 return ExpectedMessageType.Regex;
17 }
18 else {
19 return ExpectedMessageType.IMessage;
20 }
21}
22/**
23 * Class that wraps expectedResponseCollections for comparison against an outgoing message. One instance of ExpectedMessage represents one
24 * response from the bot.
25 */
26class ExpectedMessage {
27 constructor(expectedResponseCollection) {
28 if (!(expectedResponseCollection instanceof Array)) {
29 this.expectedResponseCollection = [expectedResponseCollection];
30 }
31 else {
32 this.expectedResponseCollection = expectedResponseCollection;
33 }
34 IExpectation_1.expect(this.expectedResponseCollection, 'expected response collections cannot be empty').notToBeEmpty();
35 }
36 /**
37 * routes the outgoingMessage to the proper comparison method based on the expectedResponseCollection. This switch based method exists
38 * due to typescript's lack of polymorphism support.
39 *
40 * @param outgoingMessage outgoing message that is being compared
41 */
42 checkForMessageMatch(outgoingMessage) {
43 switch (getExpectedMessageType(this.expectedResponseCollection)) {
44 case ExpectedMessageType.String:
45 this.checkMessageTextForExactStringMatch(outgoingMessage, this.expectedResponseCollection);
46 break;
47 case ExpectedMessageType.Regex:
48 this.checkMessageTextForRegex(outgoingMessage);
49 break;
50 case ExpectedMessageType.IMessage:
51 // doing this check will highlight if the diff in text instead of a large IMessage diff
52 this.deepMessageMatchCheck(outgoingMessage);
53 break;
54 default:
55 IExpectation_1.expect(outgoingMessage.type).toEqual('save');
56 }
57 }
58 toString() {
59 return JSON.stringify(this.expectedResponseCollection, null, 2);
60 }
61 /**
62 * Asserts that outgoingMessage.text is within the expectedResponseStrings collection
63 *
64 * @param outgoingMessage outgoing message being compared
65 * @param expectedResponseStrings collection of possible string values for comparison
66 */
67 checkMessageTextForExactStringMatch(outgoingMessage, expectedResponseStrings) {
68 const outgoingText = outgoingMessage.text;
69 const errorStringExpectedResponseText = expectedResponseStrings.length > 1 ? `one of ${expectedResponseStrings}` : expectedResponseStrings[0];
70 const errorString = `Bot should have responded with '${errorStringExpectedResponseText}', but was '${outgoingText}`;
71 IExpectation_1.expect(expectedResponseStrings, errorString).toInclude(outgoingText);
72 }
73 /**
74 * Assumes the expectedResponseCollection are regexs. Asserts that outgoingMessage.text matches at least of the regexs in
75 * expectedResponseCollection
76 *
77 * @param outgoingMessage outgoing message being compared
78 */
79 checkMessageTextForRegex(outgoingMessage) {
80 const text = outgoingMessage.text;
81 const regexCollection = this.expectedResponseCollection;
82 IExpectation_1.expect(regexCollection.some((regex) => regex.test(text)), `${text} did not match any regex in ${regexCollection}`).toBeTrue();
83 }
84 /**
85 * Assumes the expectedResponseCollection is an IMessage[]. Asserts that at least one IMessage in expectedResponseCollection is fully
86 * contained within the outgoingMessage (i.e. the outgoingMessage {type: 'message', text:'hello'}) would be matched successfully
87 * against {type: 'message', text: 'hello', user: { id: user1 }} because the outgoing message is contained within the response. It would
88 * not successfulyl be matched against {text: 'hello', user: { id: user1 }} because the expected response is missing { type: 'message' }
89 * @param outgoingMessage outgoing message being compared
90 */
91 deepMessageMatchCheck(outgoingMessage) {
92 const expectedResponseCollectionAsIMessage = this.expectedResponseCollection;
93 const expectedResponseStrings =
94 // get all possible responses as strings. It is possible that the expected responses have no text, so filter out those values
95 expectedResponseCollectionAsIMessage
96 .map((expectedResponse) => expectedResponse.text)
97 .filter((text) => text);
98 if (expectedResponseStrings.length) {
99 // doing this check will highlight if the diff in text instead of a large IMessage diff
100 this.checkMessageTextForExactStringMatch(outgoingMessage, expectedResponseStrings);
101 }
102 IExpectation_1.expect(expectedResponseCollectionAsIMessage).toDeeplyInclude(outgoingMessage);
103 }
104}
105exports.ExpectedMessage = ExpectedMessage;
106//# sourceMappingURL=ExpectedMessage.js.map
\No newline at end of file