mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
Add tests for chainFn
This commit is contained in:
parent
2336b3e6ea
commit
9c39c128f2
@ -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) {
|
||||
|
@ -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)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user