Skip to content

Commit

Permalink
Extract proxy to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Feb 3, 2019
1 parent dff624b commit 6ef9db2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 33 deletions.
35 changes: 2 additions & 33 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from './wrap/lodash'
import log from './log'
import tdFunction from './function'
import imitate from './imitate'
import proxy from './object/proxy'

const DEFAULT_OPTIONS = { excludeMethods: ['then'] }

Expand All @@ -17,7 +18,7 @@ var fakeObject = function (nameOrType, config, argCount) {
} else if (_.isObjectLike(nameOrType)) {
return imitate(nameOrType)
} else if (_.isString(nameOrType) || argCount === 0) {
return createTestDoubleViaProxy(nameOrType, withDefaults(config))
return proxy(nameOrType, withDefaults(config))
} else if (_.isFunction(nameOrType)) {
ensureFunctionIsNotPassed()
} else {
Expand All @@ -30,38 +31,6 @@ var createTestDoublesForFunctionNames = (names) =>
acc[funcName] = tdFunction(`.${String(funcName)}`)
})

var createTestDoubleViaProxy = (name, config) => {
ensureProxySupport(name)

const generateHandler = (internalName) => ({
get: (target, propKey) => generateGet(target, propKey, internalName)
})

const generateGet = (target, propKey, internalName) => {
if (!target.hasOwnProperty(propKey) && !_.includes(config.excludeMethods, propKey)) {
const nameWithProp = `${nameOf(internalName)}.${String(propKey)}`
target[propKey] = new Proxy(tdFunction(nameWithProp), generateHandler(nameWithProp))
}
return target[propKey]
}

return new Proxy({}, generateHandler(name))
}

var ensureProxySupport = (name) => {
if (typeof Proxy === 'undefined') {
log.error('td.object', `\
The current runtime does not have Proxy support, which is what
testdouble.js depends on when a string name is passed to \`td.object()\`.
More details here:
https://github.com/testdouble/testdouble.js/blob/master/docs/4-creating-test-doubles.md#objectobjectname
Did you mean \`td.object(['${name}'])\`?\
`)
}
}

var ensureFunctionIsNotPassed = () =>
log.error('td.object', `Functions are not valid arguments to \`td.object\` (as of testdouble@2.0.0). Please use \`td.function()\` or \`td.constructor()\` instead for creating fake functions.`)

Expand Down
37 changes: 37 additions & 0 deletions src/object/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as theredoc from 'theredoc'
import _ from '../wrap/lodash'
import log from '../log'
import tdFunction from '../function'

export default function proxy (name, { excludeMethods }) {
ensureProxySupport(name)
return new Proxy({}, generateHandler(name, excludeMethods))
}

const ensureProxySupport = (name) => {
if (typeof Proxy === 'undefined') {
log.error('td.object', theredoc`\
The current runtime does not have Proxy support, which is what
testdouble.js depends on when a string name is passed to \`td.object()\`.
More details here:
https://github.com/testdouble/testdouble.js/blob/master/docs/4-creating-test-doubles.md#objectobjectname
Did you mean \`td.object(['${name}'])\`?
`)
}
}

const generateHandler = (internalName, excludeMethods) => ({
get (target, propKey) {
return generateGet(target, propKey, 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))
}
return target[propKey]
}

0 comments on commit 6ef9db2

Please sign in to comment.