Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/imitate/overwrite-children/copy-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ const MAX_GETTER_DEPTH = 3
let getterDepth = 0

export default (target, props, visitor, original) => {
Object.defineProperties(target, _.transform(props, (acc, descriptor, name) => {
const descriptors = {}
Reflect.ownKeys(props).forEach(name => {
const descriptor = props[name]
if (propOnTargetAndNotWritable(target, name, descriptor)) {
if (name === 'prototype') {
// Functions' prototype is not configurable but is assignable:
target.prototype = valueFor(name, descriptor, original, visitor)
}
} else {
acc[name] = {
descriptors[name] = {
configurable: true,
writable: true,
value: valueFor(name, descriptor, original, visitor),
enumerable: descriptor.enumerable
}
}
}))
})
Object.defineProperties(target, descriptors)
}

const propOnTargetAndNotWritable = (target, name, originalDescriptor) => {
Expand Down
2 changes: 1 addition & 1 deletion src/imitate/overwrite-children/gather-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import isNativePrototype from './is-native-prototype'
export default function gatherProps (thing) {
const props = {}
while (isFakeable(thing) && !isNativePrototype(thing)) {
Object.getOwnPropertyNames(thing).forEach((propName) => {
Reflect.ownKeys(thing).forEach((propName) => {
if (!props[propName] && propName !== 'constructor') {
props[propName] = Object.getOwnPropertyDescriptor(thing, propName)
}
Expand Down
7 changes: 4 additions & 3 deletions src/imitate/overwrite-children/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export default (original, target, overwriteChild) => {
target.push(overwriteChild(item, `[${index}]`))
)
} else {
copyProps(target, gatherProps(original), (name, originalValue) =>
chainPrototype(original, target, name, originalValue,
overwriteChild(originalValue, `.${name}`)), original)
copyProps(target, gatherProps(original), (name, originalValue) => {
const path = typeof name === 'symbol' ? `[${String(name)}]` : `.${name}`
return chainPrototype(original, target, name, originalValue, overwriteChild(originalValue, path))
}, original)
}
}
17 changes: 17 additions & 0 deletions test/safe/imitate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ module.exports = {
assert._isEqual(td.explain(result.shirt).name, 'pants.shirt')
assert._isEqual(td.explain(result.shirt.tie).name, 'pants.shirt.tie')
},
'imitates own and inherited symbol methods with the same description' () {
const own = Symbol('read')
const inherited = Symbol('read')
class Reader {
[inherited] () {}
}
const original = new Reader()
original[own] = () => {}

const result = td.imitate(original)

assert.strictEqual(td.explain(result[own]).isTestDouble, true)
assert.strictEqual(td.explain(result[inherited]).isTestDouble, true)
assert.strictEqual(td.explain(result[own]).name, '[Symbol(read)]')
assert.notStrictEqual(result[own], result[inherited])
assert.strictEqual(Object.getOwnPropertyDescriptor(result, inherited).enumerable, false)
},
'skips over generator functions, even their custom properties' () {
// This is currently unsupported, expect to kill this test someday
if (!ES_SUPPORT.GENERATORS) return
Expand Down