Skip to content

Commit 595ae8f

Browse files
Andaristtimdorr
authored andcommitted
Fix issue with isValidElementType invariant check crashing on valid type with circular structure (#1122)
1 parent 638ac5e commit 595ae8f

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ npm run test
4242

4343
To run in explicit React versions (the number is the version, so `test:16.3` will run in React version `16.3`):
4444
```
45-
REACT=16.4 npm run test:ci
45+
REACT=16.4 npm run test
4646
```
4747

4848
To run tests in all supported React versions, `16.4`, 16.5`,
4949
```
50-
REACT=all npm run test:ci
50+
REACT=all npm run test
5151
```
5252

5353
To continuously watch and run tests, run the following:

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/connectAdvanced.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { isValidElementType } from 'react-is'
55

66
import { ReactReduxContext } from './Context'
77

8+
const stringifyComponent = Comp => {
9+
try {
10+
return JSON.stringify(Comp)
11+
} catch (err) {
12+
return String(Comp)
13+
}
14+
}
15+
816
export default function connectAdvanced(
917
/*
1018
selectorFactory is a func that is responsible for returning the selector function used to
@@ -86,7 +94,9 @@ export default function connectAdvanced(
8694
invariant(
8795
isValidElementType(WrappedComponent),
8896
`You must pass a component to the function returned by ` +
89-
`${methodName}. Instead received ${JSON.stringify(WrappedComponent)}`
97+
`${methodName}. Instead received ${stringifyComponent(
98+
WrappedComponent
99+
)}`
90100
)
91101
}
92102

test/components/connect.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,5 +2604,19 @@ describe('React', () => {
26042604
)(Comp)
26052605
}).toThrow(/renderCountProp is removed/)
26062606
})
2607+
2608+
it('should not error on valid component with circular structure', () => {
2609+
const createComp = Tag => {
2610+
const Comp = React.forwardRef(function Comp(props) {
2611+
return <Tag>{props.count}</Tag>
2612+
})
2613+
Comp.__real = Comp
2614+
return Comp
2615+
}
2616+
2617+
expect(() => {
2618+
connect()(createComp('div'))
2619+
}).not.toThrow()
2620+
})
26072621
})
26082622
})

test/run-tests.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ if (version.toLowerCase() === 'all') {
2727
}
2828
}
2929

30-
npmRun.execSync(`jest -c '${JSON.stringify(jestConfig)}'`, { stdio: 'inherit' })
30+
npmRun.execSync(
31+
`jest -c '${JSON.stringify(jestConfig)}' ${process.argv.slice(2).join(' ')}`,
32+
{ stdio: 'inherit' }
33+
)

0 commit comments

Comments
 (0)