Skip to content

Commit

Permalink
Test the thing that creates the root fakes
Browse files Browse the repository at this point in the history
These are just the node-level replacement, their properties (or for
arrays, elements) will be replaced by overwriteChildren

Simplied somewhat, when I learned through the test that underscore's
clone works well enough for primitives (one would trust, efficiently
enough)
  • Loading branch information
searls committed Jun 24, 2017
1 parent 37ccdc8 commit f3145e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
10 changes: 3 additions & 7 deletions src/imitate/create-imitation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ import _ from '../wrap/lodash'
import tdFunction from '../function'

export default (original, names) => {
let target
if (_.isArray(original) || _.isArguments(original)) {
target = []
return []
} else if (_.isFunction(original)) {
target = tdFunction(_.compact(names).join('') || '(anonymous function)')
} else if (_.isObject(original)) {
target = _.clone(original)
return tdFunction(names.join('') || '(anonymous function)')
} else {
target = original
return _.clone(original)
}
return target
}
47 changes: 47 additions & 0 deletions test/unit/imitate/create-imitation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
let tdFunction, subject
module.exports = {
beforeEach: () => {
tdFunction = td.replace('../../../src/function').default
subject = require('../../../src/imitate/create-imitation').default
},
'an array type': () => {
assert.deepEqual(subject([], []), [])
},
'an arguments type': () => {
let args = (function () { return arguments })()
assert.deepEqual(subject(args, []), [])
},
'a function without names': () => {
td.when(tdFunction('(anonymous function)')).thenReturn('fake thing')

const result = subject(() => {}, [])

assert.deepEqual(result, 'fake thing')
},
'a function with names': () => {
td.when(tdFunction('AOK')).thenReturn('fake thing')

const result = subject(() => {}, ['A', 'OK'])

assert.deepEqual(result, 'fake thing')
},
'other instances': () => {
const original = {a: 'b'}

const result = subject(original)

assert.deepEqual(result, original)
assert.notStrictEqual(result, original)
},
'primitives': () => {
assert.strictEqual(subject(true), true)
assert.strictEqual(subject(5), 5)
assert.strictEqual(subject('hi'), 'hi')
assert.strictEqual(subject(null), null)
assert.strictEqual(subject(undefined), undefined)
},
'symbols': () => {
if (!global.Symbol) return
assert.strictEqual(subject(Symbol.species), Symbol.species)
}
}
7 changes: 0 additions & 7 deletions test/unit/imitate/initialize-names.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ module.exports = {
'passed a non-null names': () => {
assert.equal(subject(null, 'foo'), 'foo')
},
'an array type': () => {
assert.deepEqual(subject(['lol']), [])
},
'an arguments type': () => {
let args = (function () { return arguments })(1,2,3)
assert.deepEqual(subject(args), [])
},
'an anon function': () => {
assert.deepEqual(subject(function () {}), [])
},
Expand Down

0 comments on commit f3145e5

Please sign in to comment.