Skip to content

Commit

Permalink
Added assertComponents test helper, fixed #8
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Nov 25, 2021
1 parent a497b57 commit 2ae6ed0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)

Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2ae6ed0

Please sign in to comment.