Skip to content

Commit b569877

Browse files
committed
ReactTestUtils now accept plain old ReactElements.
1 parent 73561a3 commit b569877

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Added `simulateKeyDownUp` and `simulateKeyDownPressUp` to `KeyboardEventData` in the test module.
66
* In rare circumstances, `Simulation.run` targets can go out of date. Targets are now stored by-name.
77
* Added `Sel.findFirstIn`.
8+
* `ReactTestUtils` now accept plain old `ReactElement`s.
89

910
# 0.6.0 ([commit log](https://github.com/japgolly/scalajs-react/compare/v0.5.4...v0.6.0))
1011

test/src/main/scala/japgolly/scalajs/react/test/ReactTestUtils.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object ReactTestUtils extends Object {
1111
def Simulate: Simulate = ???
1212

1313
/** Render a component into a detached DOM node in the document. This function requires a DOM. */
14-
def renderIntoDocument(c: ReactComponentU_): ComponentM = ???
14+
def renderIntoDocument(c: ReactElement): ComponentM = ???
1515
def renderIntoDocument[P,S,B,N <: TopNode](c: ReactComponentU[P,S,B,N]): ReactComponentM[P,S,B,N] = ???
1616

1717
/**
@@ -22,19 +22,19 @@ object ReactTestUtils extends Object {
2222
def mockComponent(c: ComponentClass, tagName: String = ???): Object = ???
2323

2424
/** Returns true if instance is an instance of a React componentClass. */
25-
def isComponentOfType(instance: ReactComponentU_, c: ComponentClass): Boolean = ???
25+
def isComponentOfType(instance: ReactElement, c: ComponentClass): Boolean = ???
2626

2727
/** Returns true if instance is a DOM component (such as a &lt;div&gt; or &lt;span&gt;). */
28-
def isDOMComponent(instance: ReactComponentU_): Boolean = ???
28+
def isDOMComponent(instance: ReactElement): Boolean = ???
2929

3030
/** Returns true if instance is a composite component (created with React.createClass()) */
31-
def isCompositeComponent(instance: ReactComponentU_): Boolean = ???
31+
def isCompositeComponent(instance: ReactElement): Boolean = ???
3232

3333
/** The combination of isComponentOfType() and isCompositeComponent(). */
34-
def isCompositeComponentWithType(instance: ReactComponentU_, c: ComponentClass): Boolean = ???
34+
def isCompositeComponentWithType(instance: ReactElement, c: ComponentClass): Boolean = ???
3535

3636
/** Returns true if instance is a plain text component. */
37-
def isTextComponent(instance: ReactComponentU_): Boolean = ???
37+
def isTextComponent(instance: ReactElement): Boolean = ???
3838

3939
/**
4040
* Traverse all components in tree and accumulate all components where test(component) is true.

test/src/test/scala/japgolly/scalajs/react/TestUtil.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import utest._
88

99
object TestUtil {
1010

11-
def assertRender(comp: ReactComponentU_, expected: String): Unit = {
11+
def assertRender(comp: ReactElement, expected: String): Unit = {
1212
val rendered: String = React.renderToStaticMarkup(comp)
1313
assert(rendered == expected)
1414
}
1515

16-
implicit class ReactComponentUAS(val c: ReactComponentU_) extends AnyVal {
16+
implicit class ReactComponentUAS(val c: ReactElement) extends AnyVal {
1717
def shouldRender(expected: String) = assertRender(c, expected)
1818
}
1919

@@ -59,4 +59,7 @@ object TestUtil {
5959
assert(a == e)
6060
}
6161
}
62+
63+
def removeReactDataAttr(s: String): String =
64+
s.replaceAll("""\s+data-react\S+?".*?"""", "")
6265
}

test/src/test/scala/japgolly/scalajs/react/test/TestTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ object TestTest extends TestSuite {
4343
assert(n.className == "BB")
4444
}
4545

46+
'renderIntoDocument {
47+
def test(c: ComponentM, exp: String): Unit = {
48+
val h = removeReactDataAttr(c.getDOMNode().outerHTML)
49+
h mustEqual exp
50+
}
51+
'plainElement {
52+
val re: ReactElement = div("Good")
53+
val c = ReactTestUtils.renderIntoDocument(re)
54+
test(c, """<div>Good</div>""")
55+
}
56+
'component {
57+
val c: ReactComponentM[Unit, Unit, Unit, TopNode] = ReactTestUtils.renderIntoDocument(B())
58+
test(c, """<p class="BB">hehehe</p>""")
59+
}
60+
}
61+
4662
'Simulate {
4763
'click {
4864
val c = ReactTestUtils.renderIntoDocument(IC())

0 commit comments

Comments
 (0)