Skip to content

Commit 6e0a106

Browse files
authored
Updated: Support React-Hot-Loader compatibility (#1168)
* fix: react-hot-loader compatibility * Fix test runner breaking The test runner script tried to pass the Jest config as stringified JSON, but that was breaking for me somehow. Sidestepped the problem by writing the settings to disk instead and pointing Jest to that file.
1 parent 63af52f commit 6e0a106

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ test/**/lcov.info
1010
test/**/lcov-report
1111
test/react/*/test/**/*.spec.js
1212
test/react/**/src
13+
test/jest-config.json
1314
lcov.info
1415

1516
lib/core/metadata.js

src/components/connectAdvanced.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ export default function connectAdvanced(
120120
const { pure } = connectOptions
121121

122122
let OuterBaseComponent = Component
123-
let FinalWrappedComponent = WrappedComponent
124123

125124
if (pure) {
126125
OuterBaseComponent = PureComponent
@@ -131,15 +130,25 @@ export default function connectAdvanced(
131130
let lastState
132131
let lastDerivedProps
133132
let lastStore
133+
let lastSelectorFactoryOptions
134134
let sourceSelector
135135

136-
return function selectDerivedProps(state, props, store) {
136+
return function selectDerivedProps(
137+
state,
138+
props,
139+
store,
140+
selectorFactoryOptions
141+
) {
137142
if (pure && lastProps === props && lastState === state) {
138143
return lastDerivedProps
139144
}
140145

141-
if (store !== lastStore) {
146+
if (
147+
store !== lastStore ||
148+
lastSelectorFactoryOptions !== selectorFactoryOptions
149+
) {
142150
lastStore = store
151+
lastSelectorFactoryOptions = selectorFactoryOptions
143152
sourceSelector = selectorFactory(
144153
store.dispatch,
145154
selectorFactoryOptions
@@ -157,14 +166,23 @@ export default function connectAdvanced(
157166
}
158167

159168
function makeChildElementSelector() {
160-
let lastChildProps, lastForwardRef, lastChildElement
169+
let lastChildProps, lastForwardRef, lastChildElement, lastComponent
161170

162-
return function selectChildElement(childProps, forwardRef) {
163-
if (childProps !== lastChildProps || forwardRef !== lastForwardRef) {
171+
return function selectChildElement(
172+
WrappedComponent,
173+
childProps,
174+
forwardRef
175+
) {
176+
if (
177+
childProps !== lastChildProps ||
178+
forwardRef !== lastForwardRef ||
179+
lastComponent !== WrappedComponent
180+
) {
164181
lastChildProps = childProps
165182
lastForwardRef = forwardRef
183+
lastComponent = WrappedComponent
166184
lastChildElement = (
167-
<FinalWrappedComponent {...childProps} ref={forwardRef} />
185+
<WrappedComponent {...childProps} ref={forwardRef} />
168186
)
169187
}
170188

@@ -182,7 +200,14 @@ export default function connectAdvanced(
182200
)
183201
this.selectDerivedProps = makeDerivedPropsSelector()
184202
this.selectChildElement = makeChildElementSelector()
185-
this.renderWrappedComponent = this.renderWrappedComponent.bind(this)
203+
this.indirectRenderWrappedComponent = this.indirectRenderWrappedComponent.bind(
204+
this
205+
)
206+
}
207+
208+
indirectRenderWrappedComponent(value) {
209+
// calling renderWrappedComponent on prototype from indirectRenderWrappedComponent bound to `this`
210+
return this.renderWrappedComponent(value)
186211
}
187212

188213
renderWrappedComponent(value) {
@@ -206,10 +231,15 @@ export default function connectAdvanced(
206231
let derivedProps = this.selectDerivedProps(
207232
storeState,
208233
wrapperProps,
209-
store
234+
store,
235+
selectorFactoryOptions
210236
)
211237

212-
return this.selectChildElement(derivedProps, forwardedRef)
238+
return this.selectChildElement(
239+
WrappedComponent,
240+
derivedProps,
241+
forwardedRef
242+
)
213243
}
214244

215245
render() {
@@ -222,7 +252,7 @@ export default function connectAdvanced(
222252

223253
return (
224254
<ContextToUse.Consumer>
225-
{this.renderWrappedComponent}
255+
{this.indirectRenderWrappedComponent}
226256
</ContextToUse.Consumer>
227257
)
228258
}

test/run-tests.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const npmRun = require('npm-run')
2+
const fs = require('fs')
3+
const path = require('path')
24
const LATEST_VERSION = '16.6'
35
const version = process.env.REACT || LATEST_VERSION
46

@@ -27,7 +29,13 @@ if (version.toLowerCase() === 'all') {
2729
}
2830
}
2931

32+
const configFilePath = path.join(__dirname, 'jest-config.json')
33+
34+
fs.writeFileSync(configFilePath, JSON.stringify(jestConfig))
35+
36+
const commandLine = `jest -c "${configFilePath}" ${process.argv.slice(2).join(' ')}`
37+
3038
npmRun.execSync(
31-
`jest -c '${JSON.stringify(jestConfig)}' ${process.argv.slice(2).join(' ')}`,
39+
commandLine,
3240
{ stdio: 'inherit' }
3341
)

0 commit comments

Comments
 (0)