Last active
December 19, 2016 20:53
-
-
Save brianneisler/9f14811a610b5388de81edb7d6d93fbf to your computer and use it in GitHub Desktop.
Use recompose core as test enhancer (using Mocha)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createFactory = baseFunc => { | |
return (props, ...rest) => baseFunc(props, ...rest) | |
} | |
export default createFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getDisplayName = result => { | |
if (typeof result === 'string') { | |
return result | |
} | |
if (!result) { | |
return undefined | |
} | |
return result.displayName || result.name || 'Test' | |
} | |
export default getDisplayName |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const method = (arg1, arg2, arg3) => arg1 + arg2 + arg3 | |
export default method |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function someTest(testMethod) { | |
return ({ name, inputs, expected }) => { | |
it(name, function() { | |
const result = testMethod(inputs) | |
expect(result).to.deep.equal(expected) | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}, []) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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