Skip to content

Commit

Permalink
Added assertWrapped prop test helper, fixed #7
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Nov 25, 2021
1 parent 5c1a9af commit a497b57
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class ReactFragmentDemoSpec extends TestSpec with TestRendererUtils {
val result = testRender(comp)

//then
assertTestComponent(result, ReactFragmentDemo)({ resProps =>
resProps shouldBe props
}, { case List(child1, child2) =>
assertNativeComponent(child1, <.div()("Item #1"))
assertNativeComponent(child2, <.div()("test"))
})
assertNativeComponent(result,
<(ReactFragmentDemo())(^.wrapped := props)(
<.div()("Item #1"),
<.div()("test")
)
)
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/src/main/scala/scommons/react/test/package.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package scommons.react

import io.github.shogowada.scalajs.reactjs.VirtualDOM._
import io.github.shogowada.statictags._

package object test {

@deprecated("Will be removed soon, use TestInstance instead", "0.5.1")
Expand All @@ -11,4 +14,20 @@ package object test {
lazy val TestRenderer = test.raw.TestRenderer
type TestInstance = test.raw.TestInstance
type TestRendererUtils = test.util.TestRendererUtils

object TestVirtualDOMAttributes {

import VirtualDOMAttributes.Type._

case class AssertPropAttributeSpec(name: String) extends AttributeSpec {
def apply(f: Any => Any): Attribute[Any => Any] = Attribute(name, f, AS_IS)
}
}

implicit class TestVirtualDOMAttributes(attributes: VirtualDOMAttributes) {

import TestVirtualDOMAttributes._

lazy val assertWrapped = AssertPropAttributeSpec("assertWrapped")
}
}
40 changes: 24 additions & 16 deletions test/src/main/scala/scommons/react/test/util/RendererUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,31 @@ sealed trait RendererUtils[Instance <: RenderedInstance] extends Matchers {
}
}

for (attr <- js.Object.keys(expectedInstance.props).filter(_ != "children")) {
val resultValue = result.props.selectDynamic(attr)
val expectedValue = expectedInstance.props.selectDynamic(attr).asInstanceOf[js.Any]
js.typeOf(expectedValue) match {
case "object" =>
if (js.Array.isArray(expectedValue)) {
assertArray(s"$expectedType.$attr",
resultValue, expectedValue.asInstanceOf[js.Array[_]])
}
else {
assertObject(s"$expectedType.$attr",
resultValue, expectedValue.asInstanceOf[js.Object with js.Dynamic])
}
case _ =>
assertAttrValue(s"$expectedType.$attr", resultValue, expectedValue)
}
val assertWrapped = expectedInstance.props.selectDynamic("assertWrapped")
if (!js.isUndefined(assertWrapped)) {
val resultWrapped = result.props.selectDynamic("wrapped")
assertWrapped.asInstanceOf[Any => Any].apply(resultWrapped)
}

js.Object.keys(expectedInstance.props)
.filter(p => p != "children" && p != "assertWrapped")
.foreach { attr =>
val resultValue = result.props.selectDynamic(attr)
val expectedValue = expectedInstance.props.selectDynamic(attr).asInstanceOf[js.Any]
js.typeOf(expectedValue) match {
case "object" =>
if (js.Array.isArray(expectedValue)) {
assertArray(s"$expectedType.$attr",
resultValue, expectedValue.asInstanceOf[js.Array[_]])
}
else {
assertObject(s"$expectedType.$attr",
resultValue, expectedValue.asInstanceOf[js.Object with js.Dynamic])
}
case _ =>
assertAttrValue(s"$expectedType.$attr", resultValue, expectedValue)
}
}

val children = getComponentChildren(result)

Expand Down
40 changes: 40 additions & 0 deletions test/src/test/scala/scommons/react/test/TestAttributesSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package scommons.react.test

import scommons.react._
import scommons.react.test.TestAttributesSpec._

class TestAttributesSpec extends TestSpec with TestRendererUtils {

TestAttributesSpec.someChildComp = mockUiComponent("TestChild")

it should "render and assertWrapped" in {
//when
val result = testRender(<(TestComponent())()())

//then
assertNativeComponent(result,
<.div()(
<(someChildComp())(^.assertWrapped(inside(_) {
case TestComponentProps("test name", age) =>
age shouldBe 123
}))()
)
)
}
}

object TestAttributesSpec {

case class TestComponentProps(name: String, age: Int)

private var someChildComp: UiComponent[TestComponentProps] = TestComponent

object TestComponent extends FunctionComponent[TestComponentProps] {

protected def render(props: Props): ReactElement = {
<.div()(
<(someChildComp())(^.wrapped := TestComponentProps("test name", 123))()
)
}
}
}

0 comments on commit a497b57

Please sign in to comment.