Skip to content

Commit 66bc956

Browse files
authored
Modify default print behavior (#59)
Modify default print behavior
1 parent 9ef784f commit 66bc956

File tree

7 files changed

+111
-56
lines changed

7 files changed

+111
-56
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import loader from 'hoc-react-loader'
4242

4343
const Component = ({ data }) => <div>Component {JSON.stringify(data)}</div>
4444

45-
export default loader(Component, { print: true })
45+
export default loader(Component)
4646
```
4747
In this example, the loader component doesn't wait for props. `this.props.load` is called once, but the `LoadingIndicator` component isn't displayed.
4848

@@ -56,7 +56,7 @@ export default loader(Component, { load: () => console.log('here') })
5656
```
5757
In this case, the loader calls `this.props.load` if it exists *AND* the `load` parameter, resulting in `here` to be printed.
5858

59-
The default `print` parameter value is `false`. It means that in this example the `LoadingIndicator` isn't displayed.
59+
The default `print` parameter value is `true`. It means that in this example the `LoadingIndicator` isn't displayed.
6060

6161
### Load as a string parameter
6262
```es6
@@ -68,7 +68,7 @@ export default loader(Component, { load: 'myLoader' })
6868
```
6969
In this case, the loader calls `this.props.myLoader` if it exists.
7070

71-
The default `print` parameter value is `false`. It means that in this example the `LoadingIndicator` isn't displayed.
71+
The default `print` parameter value is `true`. It means that in this example the `LoadingIndicator` isn't displayed.
7272

73-
### Wait as a function
73+
### Print as a function
7474
The `print` parameter can also be a function. Then the `props` and `context` are given to it (in this order), and it should return a boolean indicating wether or not the actual component should be displayed.

build/core.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
2020

2121
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
2222

23-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
23+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* eslint react/prop-types: 0 */
24+
2425

2526
var getTypeOf = function getTypeOf(something) {
2627
var getType = {};
@@ -47,10 +48,8 @@ exports.default = function (ComposedComponent) {
4748

4849
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
4950
LoadingIndicator = _ref.LoadingIndicator,
50-
_ref$print = _ref.print,
51-
print = _ref$print === undefined ? ['loaded'] : _ref$print,
52-
_ref$load = _ref.load,
53-
load = _ref$load === undefined ? undefined : _ref$load;
51+
print = _ref.print,
52+
load = _ref.load;
5453

5554
var loadFunctionName = isString(load) ? load : 'load';
5655

@@ -71,6 +70,15 @@ exports.default = function (ComposedComponent) {
7170
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref2, [this].concat(args))), _this), _this.state = {
7271
props: {}
7372
}, _this.isLoaded = function () {
73+
// Print is undefined,
74+
// we rely on 'props.loaded' if present
75+
// if not, we directly print the component
76+
if (print === undefined) {
77+
var loaded = _this.props.loaded;
78+
79+
return loaded === undefined ? true : !!loaded;
80+
}
81+
7482
// Print is an array
7583
// Implicitly meaning that this is an array of props
7684
if (Array.isArray(print)) {

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"webpack-dev-server": "~1.12.1"
4949
},
5050
"dependencies": {
51-
"hoc-react-loader": "../",
51+
"hoc-react-loader": "file:../",
5252
"lodash": "^4.15.0",
5353
"normalize.css": "~4.2.0",
5454
"react": "15.3.0",

examples/src/components/Examples/Examples.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ const Examples = ({ style, className }) => (
2222
example={<Base />}
2323
>
2424
<div className={styles.doc}>
25-
<h3>Base: defaults parameters</h3>
25+
<h3>Base: `this.props.loaded` is defined</h3>
2626
<p className={styles.description}>
27-
In this example, the loader wraps a Component with its default parameters.
27+
In this example, the loader wraps a Component with
28+
&nbsp;<pre>this.props.loaded</pre>&nbsp;defined.
2829
It means that the loader calls <pre>this.props.load</pre>
2930
&nbsp;if presents, then waits for <pre>this.props.loaded</pre>
3031
&nbsp;to be truthy, and finally displays the wrapped components.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hoc-react-loader",
3-
"version": "4.0.0",
3+
"version": "5.0.0",
44
"description": "Higher order component to call a load function from props at mount.",
55
"main": "build/index.js",
66
"peerDependencies": {

src/core.jsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint react/prop-types: 0 */
12
import React, { Component } from 'react'
23

34
const getTypeOf = (something) => {
@@ -22,8 +23,8 @@ export default (
2223
ComposedComponent,
2324
{
2425
LoadingIndicator,
25-
print = ['loaded'],
26-
load = undefined,
26+
print,
27+
load,
2728
} = {},
2829
) => {
2930
const loadFunctionName = isString(load) ? load : 'load'
@@ -36,6 +37,14 @@ export default (
3637
}
3738

3839
isLoaded = () => {
40+
// Print is undefined,
41+
// we rely on 'props.loaded' if present
42+
// if not, we directly print the component
43+
if (print === undefined) {
44+
const { loaded } = this.props
45+
return loaded === undefined ? true : !!loaded
46+
}
47+
3948
// Print is an array
4049
// Implicitly meaning that this is an array of props
4150
if (Array.isArray(print)) {

src/index.spec.js

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -63,95 +63,132 @@ const isLoadedTwice = (load, loaded) => {
6363
}
6464

6565
describe('react-loader', () => {
66-
it('should wait for a `data` props [readme]', () => {
66+
/*
67+
* The `loaded` default props is not set.
68+
* There is not `print` option.
69+
*/
70+
it('should print the wrapped component by default', () => {
71+
const load = spy(() => {})
72+
const wrappedComponent = getWrapped(undefined, { load })
73+
74+
isLoaded(load, wrappedComponent)
75+
})
76+
77+
/*
78+
* `print` option is set as an array of one element.
79+
*/
80+
it('should wait for `data` in props to be truthy', () => {
6781
// Mount
6882
const load = spy(() => {})
69-
const loaded = getWrapped({ print: ['data'] }, { load })
83+
const wrappedComponent = getWrapped({ print: ['data'] }, { load })
7084

71-
isLoading(load, loaded)
85+
isLoading(load, wrappedComponent)
7286

7387
// Change `data` value
74-
loaded.setProps({ data: true })
88+
wrappedComponent.setProps({ data: true })
7589

76-
isLoaded(load, loaded)
90+
isLoaded(load, wrappedComponent)
7791
})
7892

79-
it('should wait for a `loaded` props [default]', () => {
93+
/*
94+
* `print` option is not set, but the `loaded` parameter is.
95+
*/
96+
it('should wait for the default props (`loaded`) to be truthy', () => {
8097
// Mount
8198
const load = spy(() => {})
82-
const loaded = getWrapped(undefined, { load })
99+
const wrappedComponent = getWrapped(undefined, { loaded: false, load })
83100

84-
isLoading(load, loaded)
101+
isLoading(load, wrappedComponent)
85102

86103
// Change `loaded` value
87-
loaded.setProps({ loaded: true })
104+
wrappedComponent.setProps({ loaded: true })
88105

89-
isLoaded(load, loaded)
106+
isLoaded(load, wrappedComponent)
90107
})
91108

92-
it('should wait for an array of props', () => {
109+
/*
110+
* `print` option is set to an array of two elements.
111+
*/
112+
it('should wait for all props defined in an array of props', () => {
93113
// Mount
94114
const load = spy(() => {})
95-
const loaded = getWrapped({ print: ['prop1', 'prop2'] }, { load })
115+
const wrappedComponent = getWrapped({ print: ['prop1', 'prop2'] }, { load })
96116

97-
isLoading(load, loaded)
117+
isLoading(load, wrappedComponent)
98118

99119
// Change `prop1` value
100-
loaded.setProps({ prop1: true })
120+
wrappedComponent.setProps({ prop1: true })
101121

102-
isLoading(load, loaded)
122+
isLoading(load, wrappedComponent)
103123

104124
// Change `prop3` value
105-
loaded.setProps({ prop3: true })
125+
wrappedComponent.setProps({ prop3: true })
106126

107-
isLoading(load, loaded)
127+
isLoading(load, wrappedComponent)
108128

109129
// Change `prop2` value
110-
loaded.setProps({ prop2: true })
130+
wrappedComponent.setProps({ prop2: true })
111131

112-
isLoaded(load, loaded)
132+
isLoaded(load, wrappedComponent)
113133
})
114134

115-
it('should handle `print` parameter to be a function', () => {
135+
/*
136+
* `print` option is a function.
137+
*/
138+
it('should wait `print` to return a truthy value', () => {
116139
// Mount (false case)
117140
const load = spy(() => {})
118-
let loaded = getWrapped({ print: () => false }, { load })
141+
let wrappedComponent = getWrapped({ print: () => false }, { load })
119142

120-
isLoading(load, loaded)
143+
isLoading(load, wrappedComponent)
121144

122145
// Mount (true case)
123-
loaded = getWrapped({ print: () => true }, { load })
146+
wrappedComponent = getWrapped({ print: () => true }, { load })
124147

125-
isLoadedTwice(load, loaded)
148+
isLoadedTwice(load, wrappedComponent)
126149
})
127150

128-
it('should handle `print` parameter to be a boolean', () => {
129-
// Mount (false case)
151+
/*
152+
* `print` value is harcoded (to true).
153+
*/
154+
it('should handle a hardcoded `print` value (truthy)', () => {
155+
// Mount
156+
const load = spy(() => {})
157+
const wrappedComponent = getWrapped({ print: true }, { load })
158+
159+
isLoaded(load, wrappedComponent)
160+
})
161+
162+
/*
163+
* `print` value is harcoded (to false).
164+
*/
165+
it('should handle a hardcoded `print` value (falsey)', () => {
166+
// Mount
130167
const load = spy(() => {})
131-
const loaded = getWrapped({ print: true }, { load })
168+
const wrappedComponent = getWrapped({ print: false }, { load })
132169

133-
isLoaded(load, loaded)
170+
isLoading(load, wrappedComponent)
134171
})
135172

136173
it('should print a different LoadingIndicator component', () => {
137174
// Mount
138175
const load = spy(() => {})
139-
const loaded = getWrapped({ LoadingIndicator, print: ['data'] }, { load })
176+
const wrappedComponent = getWrapped({ LoadingIndicator, print: ['data'] }, { load })
140177

141-
isLoadingCustomLoader(load, loaded)
178+
isLoadingCustomLoader(load, wrappedComponent)
142179

143180
// Change `data` value
144-
loaded.setProps({ data: true })
181+
wrappedComponent.setProps({ data: true })
145182

146-
isLoadedCustomLoader(load, loaded)
183+
isLoadedCustomLoader(load, wrappedComponent)
147184
})
148185

149186
it('should call the `load` function parameter if present', () => {
150187
// Mount
151188
const loadProp = spy(() => {})
152189
const loadParam = spy(() => {})
153190
const props = { prop1: 'prop1', load: loadProp }
154-
const loaded = getWrapped({ load: loadParam }, props)
191+
const wrappedComponent = getWrapped({ load: loadParam }, props)
155192

156193
// Load function is called
157194
// Graphic component isn't called
@@ -160,34 +197,34 @@ describe('react-loader', () => {
160197
loadProp.should.have.been.called.with(props)
161198
loadParam.should.have.been.called.once
162199
loadParam.should.have.been.called.with(props)
163-
expect(loaded.find(Component).node).to.be.undefined
164-
loaded.find(TailSpin).node.should.exists
200+
expect(wrappedComponent.find(TailSpin).node).to.be.undefined
201+
wrappedComponent.find(Component).node.should.exists
165202
})
166203

167204
it('should call matching props if the load parameter is a string', () => {
168205
// Mount
169206
const loadProp = spy(() => {})
170207
const loadPropName = 'customLoadFunction'
171208
const props = { prop1: 'prop1', [loadPropName]: loadProp }
172-
const loaded = getWrapped({ load: loadPropName }, props)
209+
const wrappedComponent = getWrapped({ load: loadPropName }, props)
173210

174211
// Load function is called
175212
// Graphic component isn't called
176213
// Loader should be Dots
177214
loadProp.should.have.been.called.once
178215
loadProp.should.have.been.called.with(props)
179-
expect(loaded.find(Component).node).to.be.undefined
180-
loaded.find(TailSpin).node.should.exists
216+
expect(wrappedComponent.find(TailSpin).node).to.be.undefined
217+
wrappedComponent.find(Component).node.should.exists
181218
})
182219

183220
it('should handle a `null` `load` props/parameter', () => {
184221
// Mount
185-
const loaded = getWrapped()
222+
const wrappedComponent = getWrapped()
186223

187224
// Graphic component isn't called
188225
// Dots should be printed
189-
expect(loaded.find(Component).node).to.be.undefined
190-
loaded.find(TailSpin).node.should.exists
226+
expect(wrappedComponent.find(TailSpin).node).to.be.undefined
227+
wrappedComponent.find(Component).node.should.exists
191228
})
192229
})
193230

0 commit comments

Comments
 (0)