Skip to content

Instantly share code, notes, and snippets.

@brianneisler
Last active December 19, 2016 20:53
Show Gist options
  • Save brianneisler/9f14811a610b5388de81edb7d6d93fbf to your computer and use it in GitHub Desktop.
Save brianneisler/9f14811a610b5388de81edb7d6d93fbf to your computer and use it in GitHub Desktop.
Use recompose core as test enhancer (using Mocha)
export default function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
const last = funcs[funcs.length - 1]
const rest = funcs.slice(0, -1)
return (...args) => rest.reduceRight((composed, func) => func(composed), last(...args))
}
const createFactory = baseFunc => {
return (props, ...rest) => baseFunc(props, ...rest)
}
export default createFactory
import compose from '../util/compose'
import wrapDisplayName from './wrapDisplayName'
const createHelper = (func, helperName, setDisplayName = true, noArgs = false) => {
if (process.env.NODE_ENV !== 'production' && setDisplayName) {
if (noArgs) {
return compose(
func,
wrapDisplayName(helperName)
)
}
return (...args) => compose(
func(...args),
wrapDisplayName(helperName)
)
}
return func
}
export default createHelper
const getDisplayName = result => {
if (typeof result === 'string') {
return result
}
if (!result) {
return undefined
}
return result.displayName || result.name || 'Test'
}
export default getDisplayName
const method = (arg1, arg2, arg3) => arg1 + arg2 + arg3
export default method
import method from './method'
import withMultipleTests from './withMultipleTests'
import someTest from './someTest'
import compose from './compose'
const enhance = compose(
withMultipleTests
)
const testMethod = enhance(someTest(({ arg1, arg2, arg3 }) => method(arg1, arg2, arg3)))
describe('method:', function() {
const tests = {
'test 1': {
arg1: 'a',
arg2: 'b',
arg3: 'c',
expected: 'abc'
},
'test 2': {
arg1: 1,
arg2: 2,
arg3: 3,
expected 6
}
}
testMethod(test)
})
export default function someTest(testMethod) {
return ({ name, inputs, expected }) => {
it(name, function() {
const result = testMethod(inputs)
expect(result).to.deep.equal(expected)
})
}
}
import _ from 'lodash'
import createFactory from './createFactory'
import createHelper from './createHelper'
const withMultipleTests = baseFunc => {
const factory = createFactory(baseFunc)
return (props, ...rest) => factoryMultipleTests(factory, props, ...rest)
}
export default createHelper(withMultipleTests, 'withMultipleTests', true, true)
function factoryMultipleTests(factory, props, ...rest) {
return _.reduce(props, (tests, test, name) => {
test = {
inputs: _.omit(props, ['expected'])
expected: test.expected,
name
}
tests.push(factory(test, ...rest))
return tests
}, [])
}
import createFactory from './createFactory'
import getDisplayName from './getDisplayName'
const wrapDisplayName = displayName => baseFunc => {
const factory = createFactory(baseFunc)
return (props, ...rest) => {
const result = factory(props, ...rest)
if (result) {
result.displayName = `${displayName}(${getDisplayName(result)})`
}
return result
}
}
export default wrapDisplayName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment