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