Skip to content

Commit

Permalink
ReactTestUtils now accept plain old ReactElements.
Browse files Browse the repository at this point in the history
  • Loading branch information
japgolly committed Dec 9, 2014
1 parent 73561a3 commit b569877
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added `simulateKeyDownUp` and `simulateKeyDownPressUp` to `KeyboardEventData` in the test module.
* In rare circumstances, `Simulation.run` targets can go out of date. Targets are now stored by-name.
* Added `Sel.findFirstIn`.
* `ReactTestUtils` now accept plain old `ReactElement`s.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object ReactTestUtils extends Object {
def Simulate: Simulate = ???

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

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

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

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

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

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

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

/**
* Traverse all components in tree and accumulate all components where test(component) is true.
Expand Down
7 changes: 5 additions & 2 deletions test/src/test/scala/japgolly/scalajs/react/TestUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import utest._

object TestUtil {

def assertRender(comp: ReactComponentU_, expected: String): Unit = {
def assertRender(comp: ReactElement, expected: String): Unit = {
val rendered: String = React.renderToStaticMarkup(comp)
assert(rendered == expected)
}

implicit class ReactComponentUAS(val c: ReactComponentU_) extends AnyVal {
implicit class ReactComponentUAS(val c: ReactElement) extends AnyVal {
def shouldRender(expected: String) = assertRender(c, expected)
}

Expand Down Expand Up @@ -59,4 +59,7 @@ object TestUtil {
assert(a == e)
}
}

def removeReactDataAttr(s: String): String =
s.replaceAll("""\s+data-react\S+?".*?"""", "")
}
16 changes: 16 additions & 0 deletions test/src/test/scala/japgolly/scalajs/react/test/TestTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ object TestTest extends TestSuite {
assert(n.className == "BB")
}

'renderIntoDocument {
def test(c: ComponentM, exp: String): Unit = {
val h = removeReactDataAttr(c.getDOMNode().outerHTML)
h mustEqual exp
}
'plainElement {
val re: ReactElement = div("Good")
val c = ReactTestUtils.renderIntoDocument(re)
test(c, """<div>Good</div>""")
}
'component {
val c: ReactComponentM[Unit, Unit, Unit, TopNode] = ReactTestUtils.renderIntoDocument(B())
test(c, """<p class="BB">hehehe</p>""")
}
}

'Simulate {
'click {
val c = ReactTestUtils.renderIntoDocument(IC())
Expand Down

0 comments on commit b569877

Please sign in to comment.