Add tests for chainFn

This commit is contained in:
Ivan Nikulin 2017-08-23 00:50:27 +02:00
parent 2336b3e6ea
commit 9c39c128f2
No known key found for this signature in database
GPG Key ID: F800F559F11A1A10
2 changed files with 64 additions and 13 deletions

View File

@ -72,20 +72,22 @@ export function chainFn (base, fn) {
if (!(fn instanceof Function)) {
return
}
if (typeof base !== 'function') {
return fn.apply(this, arguments)
return function () {
if (typeof base !== 'function') {
return fn.apply(this, arguments)
}
let baseResult = base.apply(this, arguments)
// Allow function to mutate the first argument instead of returning the result
if (baseResult === undefined) {
baseResult = arguments[0]
}
let fnResult = fn.call(this, baseResult, ...Array.prototype.slice.call(arguments, 1))
// Return mutated argument if no result was returned
if (fnResult === undefined) {
return baseResult
}
return fnResult
}
let baseResult = base.apply(this, arguments)
// Allow function to mutate the first argument instead of returning the result
if (baseResult === undefined) {
baseResult = arguments[0]
}
let fnResult = fn.call(this, baseResult, ...arguments.slice(1))
// Return mutated argument if no result was returned
if (fnResult === undefined) {
return baseResult
}
return fnResult
}
export function isPureObject (o) {

View File

@ -94,3 +94,52 @@ test('promisifyRoute (fn(cb) with result)', t => {
t.is(res, array)
})
})
test('chainFn (mutate, mutate)', t => {
// Pass more than one argument to test that they're actually taken into account
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
obj.bar = count + 2
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (mutate, return)', t => {
const firstFn = function (obj, count) {
obj.foo = count + 1
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (return, mutate)', t => {
const firstFn = function (obj, count) {
return Object.assign({}, obj, { foo: count + 1 })
}
const secondFn = function (obj, count) {
obj.bar = count + 2
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})
test('chainFn (return, return)', t => {
const firstFn = function (obj, count) {
return Object.assign({}, obj, { foo: count + 1 })
}
const secondFn = function (obj, count) {
return Object.assign({}, obj, { bar: count + 2 })
}
const chainedFn = Utils.chainFn(firstFn, secondFn)
const expectedResult = { foo: 11, bar: 12 }
t.deepEqual(chainedFn({}, 10), expectedResult)
})