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,6 +72,7 @@ export function chainFn (base, fn) {
|
|||||||
if (!(fn instanceof Function)) {
|
if (!(fn instanceof Function)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
return function () {
|
||||||
if (typeof base !== 'function') {
|
if (typeof base !== 'function') {
|
||||||
return fn.apply(this, arguments)
|
return fn.apply(this, arguments)
|
||||||
}
|
}
|
||||||
@ -80,13 +81,14 @@ export function chainFn (base, fn) {
|
|||||||
if (baseResult === undefined) {
|
if (baseResult === undefined) {
|
||||||
baseResult = arguments[0]
|
baseResult = arguments[0]
|
||||||
}
|
}
|
||||||
let fnResult = fn.call(this, baseResult, ...arguments.slice(1))
|
let fnResult = fn.call(this, baseResult, ...Array.prototype.slice.call(arguments, 1))
|
||||||
// Return mutated argument if no result was returned
|
// Return mutated argument if no result was returned
|
||||||
if (fnResult === undefined) {
|
if (fnResult === undefined) {
|
||||||
return baseResult
|
return baseResult
|
||||||
}
|
}
|
||||||
return fnResult
|
return fnResult
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function isPureObject (o) {
|
export function isPureObject (o) {
|
||||||
return !Array.isArray(o) && typeof o === 'object'
|
return !Array.isArray(o) && typeof o === 'object'
|
||||||
|
@ -94,3 +94,52 @@ test('promisifyRoute (fn(cb) with result)', t => {
|
|||||||
t.is(res, array)
|
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