File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed
main/scala/scommons/react/test
test/scala/scommons/react/test Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ package scommons .react .test
2+
3+ import scommons .react ._
4+
5+ import scala .scalajs .js
6+
7+ object TestErrorBoundary extends ClassComponent [Unit ] {
8+
9+ private case class TestErrorBoundaryState (error : Option [js.Object ] = None )
10+
11+ protected def create (): ReactClass = createClass[TestErrorBoundaryState ](
12+ getInitialState = { _ =>
13+ TestErrorBoundaryState ()
14+ },
15+ componentDidCatch = { (self, error, _) =>
16+ self.setState(TestErrorBoundaryState (Option (error)))
17+ },
18+ render = { self =>
19+ self.state.error match {
20+ case None => self.props.children
21+ case Some (error) =>
22+ < .div()(
23+ s " $error"
24+ )
25+ }
26+ }
27+ )
28+ }
Original file line number Diff line number Diff line change 1+ package scommons .react .test
2+
3+ import scommons .react .{FunctionComponent , ReactElement }
4+
5+ import scala .scalajs .js
6+
7+ class TestErrorBoundarySpec extends TestSpec with TestRendererUtils {
8+
9+ it should " render children if no errors" in {
10+ // when
11+ val result = createTestRenderer(< (TestErrorBoundary ())()(
12+ " some child"
13+ )).root
14+
15+ // then
16+ inside(result.children.toList) { case List (child) =>
17+ child shouldBe " some child"
18+ }
19+ }
20+
21+ it should " render error details if error during render" in {
22+ // given
23+ val errorComp = new FunctionComponent [Unit ] {
24+ protected def render (props : Props ): ReactElement = {
25+ throw new IllegalArgumentException (" test error" )
26+ }
27+ }
28+
29+ // suppress intended error
30+ // see: https://github.com/facebook/react/issues/11098#issuecomment-412682721
31+ val savedConsoleError = js.Dynamic .global.console.error
32+ js.Dynamic .global.console.error = { _ : js.Any =>
33+ }
34+
35+ // when
36+ val result = testRender(< (TestErrorBoundary ())()(
37+ < (errorComp())()()
38+ ))
39+
40+ // then
41+ js.Dynamic .global.console.error = savedConsoleError
42+
43+ assertNativeComponent(result,
44+ < .div()(
45+ " java.lang.IllegalArgumentException: test error"
46+ )
47+ )
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments