Skip to content

Commit

Permalink
have the deep nested proxy work with explain
Browse files Browse the repository at this point in the history
  • Loading branch information
lgandecki committed Feb 4, 2019
1 parent d7418d9 commit 247b673
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/explain.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ function explainObject (obj) {
}
}

const forbidden = ['length', 'name', 'prototype']
function explainChildren (thing) {
const explanations = []
const children = _.cloneDeepWith(thing, (val, key, obj, stack) => {
if (_.isFunction(val) && stack) {
return _.tap(explainFunction(val), (explanation) => {
const children = {}

Object.getOwnPropertyNames(thing).forEach((propName) => {
if (_.isFunction(thing[propName])) {
children[propName] = _.tap(explainFunction(thing[propName]), explanation => {
if (explanation.isTestDouble) explanations.push(explanation)
})
} else if (!forbidden.includes(propName) && _.isObject(thing[propName])) {
const explained = explainChildren(thing[propName])
children[propName] = explained.children
explanations.push(...explained.explanations)
} else if (!forbidden.includes(propName)) {
children[propName] = thing[propName]
}
})
return { explanations, children }
Expand Down
6 changes: 5 additions & 1 deletion src/object/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as theredoc from 'theredoc'
import _ from '../wrap/lodash'
import log from '../log'
import tdFunction from '../function'
import store from '../store'

export default function proxy<T> (name: string, { excludeMethods } : { excludeMethods?: string[] } = {}) : T {
ensureProxySupport(name)
Expand Down Expand Up @@ -31,7 +32,10 @@ const generateHandler = (internalName, excludeMethods) => ({
const generateGet = (target, propKey, internalName, excludeMethods) => {
if (!target.hasOwnProperty(propKey) && !_.includes(excludeMethods, propKey)) {
const nameWithProp = `${internalName || ''}.${String(propKey)}`
target[propKey] = new Proxy(tdFunction(nameWithProp), generateHandler(nameWithProp, excludeMethods))
const tdFunc = tdFunction(nameWithProp)
const tdFuncProxy = new Proxy(tdFunc, generateHandler(nameWithProp, excludeMethods))
store.registerAlias(tdFunc, tdFuncProxy)
target[propKey] = tdFuncProxy
}
return target[propKey]
}
9 changes: 7 additions & 2 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EventEmitter } from 'events'
const storeEmitter = new EventEmitter()
let globalStore = []

export default {
const store = {
onReset (func) {
storeEmitter.on('reset', func)
},
Expand All @@ -15,7 +15,7 @@ export default {
},

for (testDouble, createIfNew = true) {
const entry = _.find(globalStore, { testDouble })
const entry = _.find(globalStore, e => testDouble === e.testDouble || testDouble === e.alias)
if (entry) {
return entry
} else if (createIfNew) {
Expand All @@ -28,5 +28,10 @@ export default {
return globalStore.push(newEntry)
})
}
},
registerAlias (testDouble, alias) {
store.for(testDouble).alias = alias
}
}

export default store

0 comments on commit 247b673

Please sign in to comment.