Skip to content

Commit 2ae6ed0

Browse files
Added assertComponents test helper, fixed #8
1 parent a497b57 commit 2ae6ed0

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ class ReactFragmentDemoSpec extends TestSpec with TestRendererUtils {
1111
val comp = <(ReactFragmentDemo())(^.wrapped := props)()
1212

1313
//when
14-
val results = createTestRenderer(comp).root.children.toList
14+
val result = createTestRenderer(comp).root
1515

1616
//then
17-
inside(results) { case List(child1, child2, child3, child4) =>
18-
assertNativeComponent(child1, <.div()("Item #1"))
19-
assertNativeComponent(child2, <.div()("test 1"))
20-
assertNativeComponent(child3, <.div()("Item #2"))
21-
assertNativeComponent(child4, <.div()("test 2"))
22-
}
17+
assertComponents(result.children, List(
18+
<.div()("Item #1"),
19+
<.div()("test 1"),
20+
<.div()("Item #2"),
21+
<.div()("test 2")
22+
))
2323
}
2424

2525
it should "render as child component" in {

test/src/main/scala/scommons/react/test/util/TestRendererUtils.scala

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scommons.react.test.util
22

33
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
44
import org.scalactic.source.Position
5-
import org.scalatest.{Assertion, Succeeded}
5+
import org.scalatest.{Assertion, Assertions, Succeeded}
66
import scommons.react.{ReactClass, UiComponent}
77
import scommons.react.test.raw
88
import scommons.react.test.raw.{TestInstance, TestRenderer}
@@ -11,7 +11,7 @@ import scommons.react.test.util.TestRendererUtils.UiComponentMock
1111

1212
import scala.scalajs.js
1313

14-
trait TestRendererUtils {
14+
trait TestRendererUtils extends Assertions {
1515

1616
def mockUiComponent[T](name: String): UiComponent[T] = UiComponentMock[T](name)
1717

@@ -61,6 +61,24 @@ trait TestRendererUtils {
6161
utils.assertComponent(result, expectedComp)(assertProps, assertChildren)
6262
}
6363

64+
def assertComponents(results: js.Array[TestInstance],
65+
expectedList: List[ReactElement]
66+
)(implicit pos: Position): Assertion = {
67+
68+
val resultCount = results.length
69+
val expectedCount = expectedList.size
70+
if (resultCount != expectedCount) {
71+
fail(s"Result components count($resultCount) doesn't match expected count($expectedCount)")
72+
}
73+
74+
expectedList.foldLeft(0) { (i, expected) =>
75+
assertNativeComponent(results(i), expected)
76+
i + 1
77+
}
78+
79+
Succeeded
80+
}
81+
6482
def assertNativeComponent(result: TestInstance,
6583
expectedElement: ReactElement
6684
)(implicit pos: Position): Assertion = {

test/src/test/scala/scommons/react/test/util/TestRendererUtilsSpec.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package scommons.react.test.util
22

33
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
4+
import org.scalatest.exceptions.TestFailedException
45
import scommons.react._
56
import scommons.react.hooks._
67
import scommons.react.test.raw.TestInstance
@@ -99,6 +100,40 @@ class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance]
99100
})
100101
})
101102
}
103+
104+
it should "fail if wrong number of components when assertComponents" in {
105+
//given
106+
val comp = testRender(<(comp2Class)(^.wrapped := Comp2Props(true))())
107+
108+
//when
109+
val e = the[TestFailedException] thrownBy {
110+
assertComponents(comp.children, List(
111+
<(TestComp())(^.wrapped := Comp1Props(1))(
112+
<.p(^.className := "test1")("test2 child1")
113+
)
114+
))
115+
}
116+
117+
//then
118+
e.getMessage shouldBe "Result components count(2) doesn't match expected count(1)"
119+
}
120+
121+
it should "assert children when assertComponents" in {
122+
//given
123+
val result = createTestRenderer(<(comp2Class)(^.wrapped := Comp2Props(true))()).root
124+
125+
//when & then
126+
assertComponents(result.children, List(
127+
<.div(^.className := "test2")(
128+
<(TestComp())(^.wrapped := Comp1Props(1))(
129+
<.p(^.className := "test1")("test2 child1")
130+
),
131+
<(TestComp())(^.wrapped := Comp1Props(2))(
132+
<.p(^.className := "test1")("test2 child2")
133+
)
134+
)
135+
))
136+
}
102137
}
103138

104139
object TestRendererUtilsSpec {

0 commit comments

Comments
 (0)