1 | import _ from './wrap/lodash'
|
2 | import calls from './store/calls'
|
3 | import store from './store'
|
4 | import stubbings from './store/stubbings'
|
5 | import imitate from './imitate'
|
6 |
|
7 | export default function func (nameOrFunc, __optionalName) {
|
8 | return _.isFunction(nameOrFunc)
|
9 | ? imitate(nameOrFunc)
|
10 | : createTestDoubleNamed(nameOrFunc || __optionalName)
|
11 | }
|
12 |
|
13 | const createTestDoubleNamed = function (name) {
|
14 | return _.tap(createTestDoubleFunction(), (testDouble) => {
|
15 | const entry = store.for(testDouble, true)
|
16 | if (name != null) {
|
17 | entry.name = name
|
18 | testDouble.toString = () => `[test double for "${name}"]`
|
19 | } else {
|
20 | testDouble.toString = () => '[test double (unnamed)]'
|
21 | }
|
22 | })
|
23 | }
|
24 |
|
25 | const createTestDoubleFunction = function () {
|
26 | return function testDouble (...args) {
|
27 | calls.log(testDouble, args, this)
|
28 | return stubbings.invoke(testDouble, args, this)
|
29 | }
|
30 | }
|