diff --git a/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala index c644d94..bec34c3 100644 --- a/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala @@ -11,15 +11,15 @@ class ReactFragmentDemoSpec extends TestSpec with TestRendererUtils { val comp = <(ReactFragmentDemo())(^.wrapped := props)() //when - val results = createTestRenderer(comp).root.children.toList + val result = createTestRenderer(comp).root //then - inside(results) { case List(child1, child2, child3, child4) => - assertNativeComponent(child1, <.div()("Item #1")) - assertNativeComponent(child2, <.div()("test 1")) - assertNativeComponent(child3, <.div()("Item #2")) - assertNativeComponent(child4, <.div()("test 2")) - } + assertComponents(result.children, List( + <.div()("Item #1"), + <.div()("test 1"), + <.div()("Item #2"), + <.div()("test 2") + )) } it should "render as child component" in { diff --git a/test/src/main/scala/scommons/react/test/util/TestRendererUtils.scala b/test/src/main/scala/scommons/react/test/util/TestRendererUtils.scala index ef436c7..155cebc 100644 --- a/test/src/main/scala/scommons/react/test/util/TestRendererUtils.scala +++ b/test/src/main/scala/scommons/react/test/util/TestRendererUtils.scala @@ -2,7 +2,7 @@ package scommons.react.test.util import io.github.shogowada.scalajs.reactjs.elements.ReactElement import org.scalactic.source.Position -import org.scalatest.{Assertion, Succeeded} +import org.scalatest.{Assertion, Assertions, Succeeded} import scommons.react.{ReactClass, UiComponent} import scommons.react.test.raw import scommons.react.test.raw.{TestInstance, TestRenderer} @@ -11,7 +11,7 @@ import scommons.react.test.util.TestRendererUtils.UiComponentMock import scala.scalajs.js -trait TestRendererUtils { +trait TestRendererUtils extends Assertions { def mockUiComponent[T](name: String): UiComponent[T] = UiComponentMock[T](name) @@ -61,6 +61,24 @@ trait TestRendererUtils { utils.assertComponent(result, expectedComp)(assertProps, assertChildren) } + def assertComponents(results: js.Array[TestInstance], + expectedList: List[ReactElement] + )(implicit pos: Position): Assertion = { + + val resultCount = results.length + val expectedCount = expectedList.size + if (resultCount != expectedCount) { + fail(s"Result components count($resultCount) doesn't match expected count($expectedCount)") + } + + expectedList.foldLeft(0) { (i, expected) => + assertNativeComponent(results(i), expected) + i + 1 + } + + Succeeded + } + def assertNativeComponent(result: TestInstance, expectedElement: ReactElement )(implicit pos: Position): Assertion = { diff --git a/test/src/test/scala/scommons/react/test/util/TestRendererUtilsSpec.scala b/test/src/test/scala/scommons/react/test/util/TestRendererUtilsSpec.scala index bbc3e42..8213f2c 100644 --- a/test/src/test/scala/scommons/react/test/util/TestRendererUtilsSpec.scala +++ b/test/src/test/scala/scommons/react/test/util/TestRendererUtilsSpec.scala @@ -1,6 +1,7 @@ package scommons.react.test.util import io.github.shogowada.scalajs.reactjs.elements.ReactElement +import org.scalatest.exceptions.TestFailedException import scommons.react._ import scommons.react.hooks._ import scommons.react.test.raw.TestInstance @@ -99,6 +100,40 @@ class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance] }) }) } + + it should "fail if wrong number of components when assertComponents" in { + //given + val comp = testRender(<(comp2Class)(^.wrapped := Comp2Props(true))()) + + //when + val e = the[TestFailedException] thrownBy { + assertComponents(comp.children, List( + <(TestComp())(^.wrapped := Comp1Props(1))( + <.p(^.className := "test1")("test2 child1") + ) + )) + } + + //then + e.getMessage shouldBe "Result components count(2) doesn't match expected count(1)" + } + + it should "assert children when assertComponents" in { + //given + val result = createTestRenderer(<(comp2Class)(^.wrapped := Comp2Props(true))()).root + + //when & then + assertComponents(result.children, List( + <.div(^.className := "test2")( + <(TestComp())(^.wrapped := Comp1Props(1))( + <.p(^.className := "test1")("test2 child1") + ), + <(TestComp())(^.wrapped := Comp1Props(2))( + <.p(^.className := "test1")("test2 child2") + ) + ) + )) + } } object TestRendererUtilsSpec {