Skip to content

Commit

Permalink
Improved error messages and source position for test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed May 29, 2019
1 parent f9ecac0 commit 7258924
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
30 changes: 21 additions & 9 deletions test/src/main/scala/scommons/react/test/util/RendererUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scommons.react.test.util

import io.github.shogowada.scalajs.reactjs.classes.ReactClass
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
import org.scalactic.source.Position
import org.scalatest.{Assertion, Matchers, Succeeded}
import scommons.react.UiComponent
import scommons.react.test.raw.RenderedInstance
Expand All @@ -11,17 +12,22 @@ import scala.scalajs.js

sealed trait RendererUtils[Instance <: RenderedInstance] extends Matchers {

private[util] val expectNoChildren: List[Instance] => Assertion = { children =>
children.map {
private[util] def expectNoChildren(implicit pos: Position): List[Instance] => Assertion = { children =>
val resultChildren = children.map {
case i if !scalajs.js.isUndefined(i.`type`) => i.`type`.toString
case i => i.toString
} shouldBe Nil
}

assert(resultChildren.isEmpty, ": Expected no children")
}

def findComponentProps[T](renderedComp: Instance, searchComp: UiComponent[T]): T = {
def findComponentProps[T](renderedComp: Instance,
searchComp: UiComponent[T]
)(implicit pos: Position): T = {

findProps[T](renderedComp, searchComp).headOption match {
case Some(comp) => comp
case None => throw new IllegalStateException(s"UiComponent $searchComp not found")
case None => fail(s"UiComponent $searchComp not found")
}
}

Expand Down Expand Up @@ -54,7 +60,8 @@ sealed trait RendererUtils[Instance <: RenderedInstance] extends Matchers {

def assertComponent[T](result: Instance, expectedComp: UiComponent[T])
(assertProps: T => Assertion,
assertChildren: List[Instance] => Assertion): Assertion = {
assertChildren: List[Instance] => Assertion
)(implicit pos: Position): Assertion = {

result.`type` shouldBe expectedComp.apply()

Expand All @@ -64,7 +71,8 @@ sealed trait RendererUtils[Instance <: RenderedInstance] extends Matchers {

def assertNativeComponent(result: Instance,
expectedElement: ReactElement,
assertChildren: List[Instance] => Assertion): Assertion = {
assertChildren: List[Instance] => Assertion
)(implicit pos: Position): Assertion = {

val expectedInstance = expectedElement.asInstanceOf[Instance]

Expand Down Expand Up @@ -145,7 +153,10 @@ sealed trait RendererUtils[Instance <: RenderedInstance] extends Matchers {
}
}

protected def assertAttrValue(name: String, resultValue: Any, expectedValue: Any): Unit = {
protected def assertAttrValue(name: String,
resultValue: Any,
expectedValue: Any)(implicit pos: Position): Unit = {

if (resultValue != expectedValue) {
fail(s"Attribute value doesn't match for $name" +
s"\n\texpected: $expectedValue" +
Expand Down Expand Up @@ -183,7 +194,8 @@ object RendererUtils {

override def assertNativeComponent(result: ShallowInstance,
expectedElement: ReactElement,
assertChildren: List[ShallowInstance] => Assertion): Assertion = {
assertChildren: List[ShallowInstance] => Assertion
)(implicit pos: Position): Assertion = {

val expectedInstance = expectedElement.asInstanceOf[ShallowInstance]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scommons.react.test.util

import io.github.shogowada.scalajs.reactjs.classes.ReactClass
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
import org.scalactic.source.Position
import org.scalatest.Assertion
import scommons.react.UiComponent
import scommons.react.test.raw.{ShallowInstance, ShallowRenderer}
Expand All @@ -20,7 +21,9 @@ trait ShallowRendererUtils {
renderer.getRenderOutput()
}

def findComponentProps[T](renderedComp: ShallowInstance, searchComp: UiComponent[T]): T = {
def findComponentProps[T](renderedComp: ShallowInstance,
searchComp: UiComponent[T])(implicit pos: Position): T = {

utils.findComponentProps(renderedComp, searchComp)
}

Expand All @@ -38,19 +41,23 @@ trait ShallowRendererUtils {

def assertComponent[T](result: ShallowInstance, expectedComp: UiComponent[T])
(assertProps: T => Assertion,
assertChildren: List[ShallowInstance] => Assertion = utils.expectNoChildren): Assertion = {
assertChildren: List[ShallowInstance] => Assertion = utils.expectNoChildren
)(implicit pos: Position): Assertion = {

utils.assertComponent(result, expectedComp)(assertProps, assertChildren)
}

def assertNativeComponent(result: ShallowInstance, expectedElement: ReactElement): Assertion = {
def assertNativeComponent(result: ShallowInstance,
expectedElement: ReactElement
)(implicit pos: Position): Assertion = {

assertNativeComponent(result, expectedElement, utils.expectNoChildren)
}

def assertNativeComponent(result: ShallowInstance,
expectedElement: ReactElement,
assertChildren: List[ShallowInstance] => Assertion
): Assertion = {
)(implicit pos: Position): Assertion = {

utils.assertNativeComponent(result, expectedElement, assertChildren)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scommons.react.test.util

import io.github.shogowada.scalajs.reactjs.classes.ReactClass
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
import org.scalactic.source.Position
import org.scalatest.{Assertion, Succeeded}
import scommons.react.UiComponent
import scommons.react.test.raw
Expand Down Expand Up @@ -36,7 +37,9 @@ trait TestRendererUtils {
root.children(0)
}

def findComponentProps[T](renderedComp: TestInstance, searchComp: UiComponent[T]): T = {
def findComponentProps[T](renderedComp: TestInstance,
searchComp: UiComponent[T])(implicit pos: Position): T = {

utils.findComponentProps(renderedComp, searchComp)
}

Expand All @@ -54,18 +57,23 @@ trait TestRendererUtils {

def assertTestComponent[T](result: TestInstance, expectedComp: UiComponent[T])
(assertProps: T => Assertion,
assertChildren: List[TestInstance] => Assertion = _ => Succeeded): Assertion = {
assertChildren: List[TestInstance] => Assertion = _ => Succeeded
)(implicit pos: Position): Assertion = {

utils.assertComponent(result, expectedComp)(assertProps, assertChildren)
}

def assertNativeComponent(result: TestInstance, expectedElement: ReactElement): Assertion = {
def assertNativeComponent(result: TestInstance,
expectedElement: ReactElement
)(implicit pos: Position): Assertion = {

assertNativeComponent(result, expectedElement, utils.expectNoChildren)
}

def assertNativeComponent(result: TestInstance,
expectedElement: ReactElement,
assertChildren: List[TestInstance] => Assertion): Assertion = {
assertChildren: List[TestInstance] => Assertion
)(implicit pos: Position): Assertion = {

utils.assertNativeComponent(result, expectedElement, assertChildren)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.github.shogowada.scalajs.reactjs.VirtualDOM._
import io.github.shogowada.scalajs.reactjs.classes.ReactClass
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
import io.github.shogowada.statictags.{Attribute, AttributeSpec}
import org.scalactic.source.Position
import org.scalatest.exceptions.TestFailedException
import org.scalatest.{Assertion, Failed, OutcomeOf}
import scommons.react.UiComponent
import scommons.react.test.TestSpec
Expand All @@ -18,19 +20,19 @@ abstract class RendererUtilsSpec[Instance <: RenderedInstance] extends TestSpec

protected def doRender(element: ReactElement): Instance

def findComponentProps[T](renderedComp: Instance, searchComp: UiComponent[T]): T
def findComponentProps[T](renderedComp: Instance, searchComp: UiComponent[T])(implicit pos: Position): T
def findProps[T](renderedComp: Instance, searchComp: UiComponent[T]): List[T]
def getComponentProps[T](component: Instance): T
def findComponents(component: Instance, componentClass: ReactClass): List[Instance]
def assertNativeComponent(result: Instance, expectedElement: ReactElement): Assertion
def assertNativeComponent(result: Instance, expectedElement: ReactElement)(implicit pos: Position): Assertion

it should "fail if comp not found when findComponentProps" in {
//given
val comp = doRender(<(emptyComp)()())
val searchComp = TestComp

//when
val e = the[IllegalStateException] thrownBy {
val e = the[TestFailedException] thrownBy {
findComponentProps(comp, searchComp)
}

Expand Down Expand Up @@ -167,7 +169,7 @@ abstract class RendererUtilsSpec[Instance <: RenderedInstance] extends TestSpec
}

//then
e.getMessage should include ("""List("test1 child") was not equal to List()""")
e.getMessage should include ("""List("test1 child") was not empty : Expected no children""")
}

it should "assert props and children when assertNativeComponent" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ShallowRendererUtilsSpec extends RendererUtilsSpec[ShallowInstance]
}

//then
e.getMessage should include ("""List("test2 child1") was not equal to List()""")
e.getMessage should include ("""List("test2 child1") was not empty : Expected no children""")
})
}

Expand Down

0 comments on commit 7258924

Please sign in to comment.