UNPKG

4.23 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var lodash_1 = require("./wrap/lodash");
4var args_match_1 = require("./args-match");
5var calls_1 = require("./store/calls");
6var log_1 = require("./log");
7var store_1 = require("./store");
8var arguments_1 = require("./stringify/arguments");
9var stubbings_1 = require("./store/stubbings");
10var notify_after_satisfaction_1 = require("./matchers/notify-after-satisfaction");
11exports.default = (function (__userDoesRehearsalInvocationHere__, config) {
12 if (config === void 0) { config = {}; }
13 var last = calls_1.default.pop();
14 ensureRehearsalOccurred(last);
15 if (calls_1.default.wasInvoked(last.testDouble, last.args, config)) {
16 notifyMatchers(last.testDouble, last.args, config);
17 warnIfStubbed(last.testDouble, last.args);
18 }
19 else {
20 log_1.default.fail(unsatisfiedErrorMessage(last.testDouble, last.args, config));
21 }
22});
23var ensureRehearsalOccurred = function (last) {
24 if (!last) {
25 log_1.default.error('td.verify', "No test double invocation detected for `verify()`.\n\n Usage:\n verify(myTestDouble('foo'))");
26 }
27};
28var notifyMatchers = function (testDouble, expectedArgs, config) {
29 lodash_1.default.each(calls_1.default.where(testDouble, expectedArgs, config), function (invocation) {
30 notify_after_satisfaction_1.default(expectedArgs, invocation.args);
31 });
32};
33var warnIfStubbed = function (testDouble, actualArgs) {
34 if (lodash_1.default.some(stubbings_1.default.for(testDouble), function (stubbing) {
35 return args_match_1.default(stubbing.args, actualArgs, stubbing.config);
36 })) {
37 log_1.default.warn('td.verify', "test double" + stringifyName(testDouble) + " was both stubbed and verified with arguments (" + arguments_1.default(actualArgs) + "), which is redundant and probably unnecessary.", 'https://github.com/testdouble/testdouble.js/blob/master/docs/B-frequently-asked-questions.md#why-shouldnt-i-call-both-tdwhen-and-tdverify-for-a-single-interaction-with-a-test-double');
38 }
39};
40var unsatisfiedErrorMessage = function (testDouble, args, config) {
41 return baseSummary(testDouble, args, config) +
42 matchedInvocationSummary(testDouble, args, config) +
43 invocationSummary(testDouble, args, config);
44};
45var stringifyName = function (testDouble) {
46 var name = store_1.default.for(testDouble).name;
47 return name ? " `" + name + "`" : '';
48};
49var baseSummary = function (testDouble, args, config) {
50 return "Unsatisfied verification on test double" + stringifyName(testDouble) + ".\n\n Wanted:\n - called with `(" + arguments_1.default(args) + ")`" + timesMessage(config) + ignoreMessage(config) + ".";
51};
52var invocationSummary = function (testDouble, args, config) {
53 var calls = calls_1.default.for(testDouble);
54 if (calls.length === 0) {
55 return '\n\n But there were no invocations of the test double.';
56 }
57 else {
58 return lodash_1.default.reduce(calls, function (desc, call) {
59 return desc + ("\n - called with `(" + arguments_1.default(call.args) + ")`.");
60 }, '\n\n All calls of the test double, in order were:');
61 }
62};
63var matchedInvocationSummary = function (testDouble, args, config) {
64 var calls = calls_1.default.where(testDouble, args, config);
65 var expectedCalls = config.times || 0;
66 if (calls.length === 0 || calls.length > expectedCalls) {
67 return '';
68 }
69 else {
70 return lodash_1.default.reduce(lodash_1.default.groupBy(calls, 'args'), function (desc, callsMatchingArgs, args) {
71 return desc + ("\n - called " + pluralize(callsMatchingArgs.length, 'time') + " with `(" + arguments_1.default(callsMatchingArgs[0].args) + ")`.");
72 }, "\n\n " + pluralize(calls.length, 'call') + " that satisfied this verification:");
73 }
74};
75var pluralize = function (x, msg) {
76 return x + " " + msg + (x === 1 ? '' : 's');
77};
78var timesMessage = function (config) {
79 return config.times != null
80 ? " " + pluralize(config.times, 'time')
81 : '';
82};
83var ignoreMessage = function (config) {
84 return config.ignoreExtraArgs != null
85 ? ', ignoring any additional arguments'
86 : '';
87};