This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #77
- Loading branch information
Showing
7 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from 'react' | ||
import { shallow } from 'enzyme' | ||
import { Dummy } from './utils' | ||
import { compose, flattenProps, pure } from '../' | ||
|
||
describe('flattenProps', () => { | ||
it('should flatten an object prop and spread it into the top-level props object', () => { | ||
const Counter = flattenProps('a')(Dummy) | ||
|
||
const wrapper = shallow( | ||
<Counter pass="through" a={{ z: 1 }} />, | ||
) | ||
|
||
expect(wrapper.equals( | ||
<Dummy pass="through" a={{ z: 1 }} z={1} />, | ||
)).toBe(true) | ||
|
||
wrapper.setProps({ | ||
pass: 'through', | ||
a: { a: 1 }, | ||
}) | ||
|
||
expect(wrapper.equals( | ||
<Dummy pass="through" a={1} />, | ||
)).toBe(true) | ||
}) | ||
|
||
it('should flatten several props', () => { | ||
const Counter = flattenProps(['a', 'b'])(Dummy) | ||
|
||
const wrapper = shallow( | ||
<Counter pass="through" a={{ z: 1 }} b={{ y: 2 }} />, | ||
) | ||
|
||
expect(wrapper.equals( | ||
<Dummy pass="through" a={{ z: 1 }} b={{ y: 2 }} z={1} y={2} />, | ||
)).toBe(true) | ||
|
||
wrapper.setProps({ | ||
pass: 'through', | ||
a: { a: 1 }, | ||
b: { b: 1 }, | ||
}) | ||
|
||
expect(wrapper.equals( | ||
<Dummy pass="through" a={1} b={1} />, | ||
)).toBe(true) | ||
}) | ||
|
||
it('should be merged with other hoc', () => { | ||
const Component = compose( | ||
flattenProps('foo'), | ||
pure, | ||
)(Dummy) | ||
|
||
const wrapper = shallow(<Component />) | ||
expect(wrapper.instance().constructor.displayName).toBe('flattenProps(pure(Dummy))') | ||
expect(wrapper.equals(<Dummy />)).toBe(true) | ||
}) | ||
}) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import createHelper from './createHelper' | ||
import mapProps from './mapProps' | ||
|
||
/** | ||
* Flattens one or several props so that its fields are spread out into the props object. | ||
* | ||
* @static | ||
* @category Higher-order-components | ||
* @alias flattenProp | ||
* @param {String|String[]} paths The property paths to flatten. | ||
* @returns {HigherOrderComponent} A function that takes a component and returns a new component. | ||
* @example | ||
* | ||
* const Button = flattenProps(['a', 'b'])('button'); | ||
* // Will render <button type="submit" className="btn" /> | ||
* <Button a={{type: 'submit'}} b={{className: 'btn'}} /> | ||
*/ | ||
const flattenProps = paths => mapProps((props) => { | ||
if (typeof paths === 'string') { | ||
return { ...props, ...props[paths] } | ||
} | ||
|
||
return paths.reduce((nextProps, path) => ({ ...nextProps, ...props[path] }), props) | ||
}) | ||
|
||
export default createHelper(flattenProps, 'flattenProps') |
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
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