Skip to content
This repository has been archived by the owner on Nov 26, 2018. It is now read-only.

Commit

Permalink
feat: add debug hoc
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Jan 26, 2017
1 parent 2193bdd commit c778a77
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
## `Higher-order-components`
* <a href="#branchtest-left-rightidentity">`branch`</a>
* <a href="#connectobsobsmapper">`connectObs`</a>
* <a href="#debuglabel-selector">`debug`</a>
* <a href="#defaultpropsdefaultprops">`defaultProps`</a>
* <a href="#flattenproppropname">`flattenProp`</a>
* <a href="#getcontextcontexttypes">`getContext`</a>
Expand Down Expand Up @@ -161,6 +162,34 @@ connectObs(({change$, value$}) => ({

<!-- div -->

<h3 id="debuglabel-selector"><code>debug(label, selector)</code></h3>
[&#x24C8;](https://github.com/neoziro/recompact/blob/1.0.2/src/debug.js#L23 "View in source") [&#x24C9;][1]

Display the flow of props.
Very useful for debugging higher-order component stack.

#### Arguments
1. `label` *(&#42;)*: A label displayed in console.
2. `selector` *(Function)*: A props selector.

#### Returns
*(HigherOrderComponent)*: Returns a function that take a Component.

#### Example
```js
recompact.compose(
recompact.withProps({ foo: 'bar' }),
recompact.debug(),
recompact.renameProp('foo', 'className'),
recompact.debug(),
)('input')
```
---

<!-- /div -->

<!-- div -->

<h3 id="defaultpropsdefaultprops"><code>defaultProps(defaultProps)</code></h3>
[&#x24C8;](https://github.com/neoziro/recompact/blob/1.0.2/src/defaultProps.js#L19 "View in source") [&#x24C9;][1]

Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/debug.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable no-console */
import React from 'react'
import { shallow } from 'enzyme'
import { Dummy } from './utils'
import { compose, debug, withProps } from '../'

describe('debug', () => {
let consoleLog

beforeEach(() => {
consoleLog = console.log
console.log = jest.fn()
})

afterEach(() => {
console.log = consoleLog
})

it('should log compose', () => {
const Component = compose(
withProps({ a: 'b' }),
debug(),
withProps({ c: 'd' }),
debug(),
)(Dummy)

shallow(<Component />).find(Dummy)
expect(console.log).toHaveBeenCalledTimes(2)
expect(console.log.mock.calls[0]).toEqual(['withProps(Component)', { a: 'b' }])
expect(console.log.mock.calls[1]).toEqual(['Dummy', { a: 'b', c: 'd' }])
})
})
32 changes: 32 additions & 0 deletions src/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable no-param-reassign, no-console */
import createEagerFactory from './createEagerFactory'
import getDisplayName from './getDisplayName'

/**
* Display the flow of props.
* Very useful for debugging higher-order component stack.
*
* @static
* @category Higher-order-components
* @param {*} label A label displayed in console.
* @param {Function} selector A props selector.
* @returns {HigherOrderComponent} Returns a function that take a Component.
* @example
*
* recompact.compose(
* recompact.withProps({ foo: 'bar' }),
* recompact.debug(),
* recompact.renameProp('foo', 'className'),
* recompact.debug(),
* )('input')
*/
const debug = (label, selector = x => x) => (BaseComponent) => {
const factory = createEagerFactory(BaseComponent)
label = label || getDisplayName(BaseComponent)
return (props) => {
console.log(label, selector(props))
return factory(props)
}
}

export default debug
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import createEagerFactory from './createEagerFactory'
import createEventHandler from './createEventHandler'
import createHelper from './createHelper'
import createSink from './createSink'
import debug from './debug'
import defaultProps from './defaultProps'
import flattenProp from './flattenProp'
import getContext from './getContext'
Expand Down Expand Up @@ -59,6 +60,7 @@ export {
createEventHandler,
createHelper,
createSink,
debug,
defaultProps,
flattenProp,
getContext,
Expand Down Expand Up @@ -110,6 +112,7 @@ export default {
createEventHandler,
createHelper,
createSink,
debug,
defaultProps,
flattenProp,
getContext,
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
],
},
Expand Down

0 comments on commit c778a77

Please sign in to comment.