1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const lodash_1 = require("./wrap/lodash");
|
4 | const log_1 = require("./log");
|
5 | const function_1 = require("./function");
|
6 | const imitate_1 = require("./imitate");
|
7 | const proxy_1 = require("./object/proxy");
|
8 | const DEFAULT_OPTIONS = { excludeMethods: ['then'] };
|
9 | function object(nameOrType, config) {
|
10 | return lodash_1.default.tap(fakeObject(nameOrType, config, arguments.length), (obj) => {
|
11 | addToStringToDouble(obj, nameOrType);
|
12 | });
|
13 | }
|
14 | exports.default = object;
|
15 | var fakeObject = function (nameOrType, config, argCount) {
|
16 | if (lodash_1.default.isArray(nameOrType)) {
|
17 | return createTestDoublesForFunctionNames(nameOrType);
|
18 | }
|
19 | else if (lodash_1.default.isObjectLike(nameOrType)) {
|
20 | return imitate_1.default(nameOrType);
|
21 | }
|
22 | else if (lodash_1.default.isString(nameOrType) || argCount === 0) {
|
23 | return proxy_1.default(nameOrType, withDefaults(config));
|
24 | }
|
25 | else if (lodash_1.default.isFunction(nameOrType)) {
|
26 | ensureFunctionIsNotPassed();
|
27 | }
|
28 | else {
|
29 | ensureOtherGarbageIsNotPassed();
|
30 | }
|
31 | };
|
32 | var createTestDoublesForFunctionNames = (names) => lodash_1.default.transform(names, (acc, funcName) => {
|
33 | acc[funcName] = function_1.default(`.${String(funcName)}`);
|
34 | }, {});
|
35 | var ensureFunctionIsNotPassed = () => log_1.default.error('td.object', 'Functions are not valid arguments to `td.object` (as of testdouble@2.0.0). Please use `td.function()`, `td.constructor()` or `td.instance()` instead for creating fake functions.');
|
36 | var ensureOtherGarbageIsNotPassed = () => log_1.default.error('td.object', `\
|
37 | To create a fake object with td.object(), pass it a plain object that contains
|
38 | functions, an array of function names, or (if your runtime supports ES Proxy
|
39 | objects) a string name.
|
40 |
|
41 | If you passed td.object an instance of a custom type, consider passing the
|
42 | type's constructor to \`td.constructor()\` instead.
|
43 | `);
|
44 | var withDefaults = (config) => lodash_1.default.extend({}, DEFAULT_OPTIONS, config);
|
45 | var addToStringToDouble = (fakeObject, nameOrType) => {
|
46 | const name = nameOf(nameOrType);
|
47 | fakeObject.toString = () => `[test double object${name ? ` for "${name}"` : ''}]`;
|
48 | };
|
49 | var nameOf = (nameOrType) => lodash_1.default.isString(nameOrType)
|
50 | ? nameOrType
|
51 | : '';
|