Skip to content

Commit 6a97b60

Browse files
Added mockUiComponent test helper, fixed #6
1 parent 32cb801 commit 6a97b60

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

core/src/main/scala/scommons/react/ClassComponent.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scommons.react
22

33
import io.github.shogowada.scalajs.reactjs.React
4-
import io.github.shogowada.scalajs.reactjs.React.{Props, Self}
4+
import io.github.shogowada.scalajs.reactjs.React.Self
55

66
import scala.scalajs.js
77

@@ -11,8 +11,8 @@ trait ClassComponent[T] extends UiComponent[T] {
1111
render: Self[T, S] => ReactElement,
1212
getInitialState: Self[T, S] => S = null,
1313
componentDidMount: Self[T, S] => Unit = null,
14-
shouldComponentUpdate: (Self[T, S], Props[T], S) => Boolean = null,
15-
componentDidUpdate: (Self[T, S], Props[T], S) => Unit = null,
14+
shouldComponentUpdate: (Self[T, S], Props, S) => Boolean = null,
15+
componentDidUpdate: (Self[T, S], Props, S) => Unit = null,
1616
componentWillUnmount: Self[T, S] => Unit = null,
1717
componentDidCatch: (Self[T, S], js.Object, js.Dynamic) => Unit = null
1818
): ReactClass = {

core/src/main/scala/scommons/react/FunctionComponent.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import scala.scalajs.js
66

77
trait FunctionComponent[T] extends UiComponent[T] {
88

9-
type Props = reactjs.React.Props[T]
10-
119
protected def render(props: Props): ReactElement
1210

1311
override protected def create(): ReactClass = {

core/src/main/scala/scommons/react/UiComponent.scala

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

3+
import io.github.shogowada.scalajs.reactjs
4+
35
trait UiComponent[T] {
46

7+
type Props = reactjs.React.Props[T]
8+
59
def apply(): ReactClass = reactClass
610

711
private lazy val reactClass: ReactClass = create()

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package scommons.react.test.util
33
import io.github.shogowada.scalajs.reactjs.elements.ReactElement
44
import org.scalactic.source.Position
55
import org.scalatest.{Assertion, Succeeded}
6-
import scommons.react.UiComponent
6+
import scommons.react.{ReactClass, UiComponent}
77
import scommons.react.test.raw
88
import scommons.react.test.raw.{TestInstance, TestRenderer}
99
import scommons.react.test.util.RendererUtils.{testInstanceUtils => utils}
10+
import scommons.react.test.util.TestRendererUtils.UiComponentMock
1011

1112
import scala.scalajs.js
1213

1314
trait TestRendererUtils {
15+
16+
def mockUiComponent[T](name: String): UiComponent[T] = UiComponentMock[T](name)
1417

1518
def createTestRenderer(element: ReactElement,
1619
createMock: js.Function1[TestInstance, js.Any] = null): TestRenderer = {
@@ -73,3 +76,11 @@ trait TestRendererUtils {
7376
utils.assertNativeComponent(result, expectedElement, assertChildren)
7477
}
7578
}
79+
80+
object TestRendererUtils {
81+
82+
private case class UiComponentMock[T](name: String) extends UiComponent[T] {
83+
84+
protected def create(): ReactClass = name.asInstanceOf[ReactClass]
85+
}
86+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance]
1414

1515
protected def doRender(element: ReactElement): TestInstance = testRender(element)
1616

17+
TestComponent.childComp = mockUiComponent("SomeTestChild")
18+
19+
it should "render mockUiComponent" in {
20+
//when
21+
val result = testRender(<(TestComponent())()())
22+
23+
//then
24+
TestComponent.childComp.toString shouldBe "UiComponentMock(SomeTestChild)"
25+
26+
assertTestComponent(result, TestComponent.childComp) { props =>
27+
props shouldBe ()
28+
}
29+
}
30+
1731
it should "render mock reference" in {
1832
//given
1933
val comp = new FunctionComponent[Unit] {
@@ -88,6 +102,19 @@ class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance]
88102
}
89103

90104
object TestRendererUtilsSpec {
105+
106+
object TestComponent extends FunctionComponent[Unit] {
107+
108+
private[util] var childComp: UiComponent[Unit] = new FunctionComponent[Unit] {
109+
protected def render(props: Props): ReactElement = {
110+
<.>()()
111+
}
112+
}
113+
114+
protected def render(props: TestComponent.Props): ReactElement = {
115+
<(childComp())()()
116+
}
117+
}
91118

92119
def createHTMLInputElement(focusMock: () => Unit): js.Object = {
93120
js.Dynamic.literal(

0 commit comments

Comments
 (0)