Skip to content

Commit 44bd85a

Browse files
committed
Wrap container components in render protection
1 parent 200b361 commit 44bd85a

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/core/plugins/view/root-injects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,5 @@ export const getComponent = (getSystem, getStore, getComponents, componentName,
120120
return makeContainer(getSystem, component, getStore())
121121

122122
// container == truthy
123-
return makeContainer(getSystem, component)
123+
return makeContainer(getSystem, wrapRender(component))
124124
}

test/core/system/system.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,116 @@ describe("bound system", function(){
571571
// Then
572572
expect(renderedComponent.text()).toEqual("This came from mapStateToProps and this came from the system and this came from my own props")
573573
})
574+
575+
it("should catch errors thrown inside of React Component Class render methods", function() {
576+
// Given
577+
// eslint-disable-next-line react/require-render-return
578+
class BrokenComponent extends React.Component {
579+
render() {
580+
throw new Error("This component is broken")
581+
}
582+
}
583+
const system = new System({
584+
plugins: [
585+
ViewPlugin,
586+
{
587+
components: {
588+
BrokenComponent
589+
}
590+
}
591+
]
592+
})
593+
594+
// When
595+
var Component = system.getSystem().getComponent("BrokenComponent")
596+
const renderedComponent = render(<Component />)
597+
598+
// Then
599+
expect(renderedComponent.text()).toEqual("😱 Could not render BrokenComponent, see the console.")
600+
})
601+
602+
it("should catch errors thrown inside of pure component render methods", function() {
603+
// Given
604+
// eslint-disable-next-line react/require-render-return
605+
class BrokenComponent extends PureComponent {
606+
render() {
607+
throw new Error("This component is broken")
608+
}
609+
}
610+
611+
const system = new System({
612+
plugins: [
613+
ViewPlugin,
614+
{
615+
components: {
616+
BrokenComponent
617+
}
618+
}
619+
]
620+
})
621+
622+
// When
623+
var Component = system.getSystem().getComponent("BrokenComponent")
624+
const renderedComponent = render(<Component />)
625+
626+
// Then
627+
expect(renderedComponent.text()).toEqual("😱 Could not render BrokenComponent, see the console.")
628+
})
629+
630+
it("should catch errors thrown inside of stateless component functions", function() {
631+
// Given
632+
// eslint-disable-next-line react/require-render-return
633+
let BrokenComponent = function BrokenComponent() { throw new Error("This component is broken") }
634+
const system = new System({
635+
plugins: [
636+
ViewPlugin,
637+
{
638+
components: {
639+
BrokenComponent
640+
}
641+
}
642+
]
643+
})
644+
645+
// When
646+
var Component = system.getSystem().getComponent("BrokenComponent")
647+
const renderedComponent = render(<Component />)
648+
649+
// Then
650+
expect(renderedComponent.text().startsWith("😱 Could not render")).toEqual(true)
651+
})
652+
653+
it("should catch errors thrown inside of container components", function() {
654+
// Given
655+
// eslint-disable-next-line react/require-render-return
656+
class BrokenComponent extends React.Component {
657+
render() {
658+
throw new Error("This component is broken")
659+
}
660+
}
661+
662+
const system = new System({
663+
plugins: [
664+
ViewPlugin,
665+
{
666+
components: {
667+
BrokenComponent
668+
}
669+
}
670+
]
671+
})
672+
673+
// When
674+
var Component = system.getSystem().getComponent("BrokenComponent", true)
675+
const renderedComponent = render(
676+
<Provider store={system.getStore()}>
677+
<Component />
678+
</Provider>
679+
)
680+
681+
// Then
682+
expect(renderedComponent.text()).toEqual("😱 Could not render BrokenComponent, see the console.")
683+
})
574684
})
575685

576686
})

0 commit comments

Comments
 (0)