1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var lodash_1 = require("./wrap/lodash");
|
4 | var proxy_safe_clone_deep_with_1 = require("./wrap/proxy-safe-clone-deep-with");
|
5 | var calls_1 = require("./store/calls");
|
6 | var store_1 = require("./store");
|
7 | var arguments_1 = require("./stringify/arguments");
|
8 | var stubbings_1 = require("./store/stubbings");
|
9 | function explain(testDouble) {
|
10 | if (lodash_1.default.isFunction(testDouble)) {
|
11 | return explainFunction(testDouble);
|
12 | }
|
13 | else if (lodash_1.default.isObject(testDouble)) {
|
14 | return explainObject(testDouble);
|
15 | }
|
16 | else {
|
17 | return explainNonTestDouble(testDouble);
|
18 | }
|
19 | }
|
20 | exports.default = explain;
|
21 | function explainObject(obj) {
|
22 | var _a = explainChildren(obj), explanations = _a.explanations, children = _a.children;
|
23 | return {
|
24 | name: null,
|
25 | callCount: 0,
|
26 | calls: [],
|
27 | description: describeObject(explanations),
|
28 | children: children,
|
29 | isTestDouble: explanations.length > 0
|
30 | };
|
31 | }
|
32 | function explainChildren(thing) {
|
33 | var explanations = [];
|
34 | var children = proxy_safe_clone_deep_with_1.default(thing, function (val, key, obj, stack) {
|
35 | if (lodash_1.default.isFunction(val) && stack) {
|
36 | return lodash_1.default.tap(explainFunction(val), function (explanation) {
|
37 | if (explanation.isTestDouble)
|
38 | explanations.push(explanation);
|
39 | });
|
40 | }
|
41 | });
|
42 | return { explanations: explanations, children: children };
|
43 | }
|
44 | function describeObject(explanations) {
|
45 | var count = explanations.length;
|
46 | if (count === 0)
|
47 | return 'This object contains no test doubles';
|
48 | return "This object contains " + count + " test double function" + (count > 1 ? 's' : '') + ": [" + lodash_1.default.map(explanations, function (e) {
|
49 | return "\"" + e.name + "\"";
|
50 | }).join(', ') + "]";
|
51 | }
|
52 | function explainFunction(testDouble) {
|
53 | if (store_1.default.for(testDouble, false) == null) {
|
54 | return explainNonTestDouble(testDouble);
|
55 | }
|
56 | var calls = calls_1.default.for(testDouble);
|
57 | var stubs = stubbings_1.default.for(testDouble);
|
58 | var children = explainChildren(testDouble).children;
|
59 | return {
|
60 | name: store_1.default.for(testDouble).name,
|
61 | callCount: calls.length,
|
62 | calls: calls,
|
63 | description: testdoubleDescription(testDouble, stubs, calls) +
|
64 | stubbingDescription(stubs) +
|
65 | callDescription(calls),
|
66 | children: children,
|
67 | isTestDouble: true
|
68 | };
|
69 | }
|
70 | function explainNonTestDouble(thing) {
|
71 | return ({
|
72 | name: undefined,
|
73 | callCount: 0,
|
74 | calls: [],
|
75 | description: "This is not a test double" + (lodash_1.default.isFunction(thing) ? ' function' : '') + ".",
|
76 | isTestDouble: false
|
77 | });
|
78 | }
|
79 | function testdoubleDescription(testDouble, stubs, calls) {
|
80 | return "This test double " + stringifyName(testDouble) + "has " + stubs.length + " stubbings and " + calls.length + " invocations.";
|
81 | }
|
82 | function stubbingDescription(stubs) {
|
83 | return stubs.length > 0
|
84 | ? lodash_1.default.reduce(stubs, function (desc, stub) {
|
85 | return desc + ("\n - when called with `(" + arguments_1.default(stub.args) + ")`, then " + planFor(stub) + " " + argsFor(stub) + ".");
|
86 | }, '\n\nStubbings:')
|
87 | : '';
|
88 | }
|
89 | function planFor(stub) {
|
90 | switch (stub.config.plan) {
|
91 | case 'thenCallback': return 'callback';
|
92 | case 'thenResolve': return 'resolve';
|
93 | case 'thenReject': return 'reject';
|
94 | default: return 'return';
|
95 | }
|
96 | }
|
97 | function argsFor(stub) {
|
98 | switch (stub.config.plan) {
|
99 | case 'thenCallback': return "`(" + arguments_1.default(stub.stubbedValues, ', ') + ")`";
|
100 | default: return arguments_1.default(stub.stubbedValues, ', then ', '`');
|
101 | }
|
102 | }
|
103 | function callDescription(calls) {
|
104 | return calls.length > 0
|
105 | ? lodash_1.default.reduce(calls, function (desc, call) { return desc + ("\n - called with `(" + arguments_1.default(call.cloneArgs) + ")`."); }, '\n\nInvocations:')
|
106 | : '';
|
107 | }
|
108 | function stringifyName(testDouble) {
|
109 | var name = store_1.default.for(testDouble).name;
|
110 | return name ? "`" + name + "` " : '';
|
111 | }
|