1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var lodash_1 = require("./wrap/lodash");
|
4 | var callback_1 = require("./callback");
|
5 | var is_callback_1 = require("./matchers/is-callback");
|
6 | var calls_1 = require("./store/calls");
|
7 | var log_1 = require("./log");
|
8 | var stubbings_1 = require("./store/stubbings");
|
9 | var config_1 = require("./config");
|
10 | function when(__userDoesRehearsalInvocationHere__, config) {
|
11 | if (config === void 0) { config = {}; }
|
12 | return ({
|
13 | thenReturn: function () {
|
14 | var stubbedValues = [];
|
15 | for (var _i = 0; _i < arguments.length; _i++) {
|
16 | stubbedValues[_i] = arguments[_i];
|
17 | }
|
18 | return addStubbing(stubbedValues, config, 'thenReturn');
|
19 | },
|
20 | thenCallback: function () {
|
21 | var stubbedValues = [];
|
22 | for (var _i = 0; _i < arguments.length; _i++) {
|
23 | stubbedValues[_i] = arguments[_i];
|
24 | }
|
25 | return addStubbing(stubbedValues, config, 'thenCallback');
|
26 | },
|
27 | thenDo: function () {
|
28 | var stubbedValues = [];
|
29 | for (var _i = 0; _i < arguments.length; _i++) {
|
30 | stubbedValues[_i] = arguments[_i];
|
31 | }
|
32 | return addStubbing(stubbedValues, config, 'thenDo');
|
33 | },
|
34 | thenThrow: function () {
|
35 | var stubbedValues = [];
|
36 | for (var _i = 0; _i < arguments.length; _i++) {
|
37 | stubbedValues[_i] = arguments[_i];
|
38 | }
|
39 | return addStubbing(stubbedValues, config, 'thenThrow');
|
40 | },
|
41 | thenResolve: function () {
|
42 | var stubbedValues = [];
|
43 | for (var _i = 0; _i < arguments.length; _i++) {
|
44 | stubbedValues[_i] = arguments[_i];
|
45 | }
|
46 | warnIfPromiseless();
|
47 | return addStubbing(stubbedValues, config, 'thenResolve');
|
48 | },
|
49 | thenReject: function () {
|
50 | var stubbedValues = [];
|
51 | for (var _i = 0; _i < arguments.length; _i++) {
|
52 | stubbedValues[_i] = arguments[_i];
|
53 | }
|
54 | warnIfPromiseless();
|
55 | return addStubbing(stubbedValues, config, 'thenReject');
|
56 | }
|
57 | });
|
58 | }
|
59 | exports.default = when;
|
60 | function addStubbing(stubbedValues, config, plan) {
|
61 | var last = calls_1.default.pop();
|
62 | ensureRehearsalOccurred(last);
|
63 | lodash_1.default.assign(config, { plan: plan });
|
64 | stubbings_1.default.add(last.testDouble, concatImpliedCallback(last.args, config), stubbedValues, config);
|
65 | return last.testDouble;
|
66 | }
|
67 | function ensureRehearsalOccurred(last) {
|
68 | if (!last) {
|
69 | return log_1.default.error('td.when', "No test double invocation call detected for `when()`.\n\n Usage:\n when(myTestDouble('foo')).thenReturn('bar')");
|
70 | }
|
71 | }
|
72 | function concatImpliedCallback(args, config) {
|
73 | if (config.plan !== 'thenCallback') {
|
74 | return args;
|
75 | }
|
76 | else if (!lodash_1.default.some(args, is_callback_1.default)) {
|
77 | return args.concat(callback_1.default);
|
78 | }
|
79 | else {
|
80 | return args;
|
81 | }
|
82 | }
|
83 | function warnIfPromiseless() {
|
84 | if (config_1.default().promiseConstructor == null) {
|
85 | log_1.default.warn('td.when', "no promise constructor is set, so this `thenResolve` or `thenReject` stubbing\nwill fail if it's satisfied by an invocation on the test double. You can tell\ntestdouble.js which promise constructor to use with `td.config`, like so:\n\n td.config({\n promiseConstructor: require('bluebird')\n })");
|
86 | }
|
87 | }
|