Skip to content

Commit 3bc2fdd

Browse files
committed
Use testing_library-dom in tests
1 parent 4e49107 commit 3bc2fdd

File tree

19 files changed

+187
-165
lines changed

19 files changed

+187
-165
lines changed

doc/TESTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Read through the following for how to test with `ReactTestUtils`.
4949

5050
```scala
5151
import japgolly.scalajs.react._
52-
import japgolly.scalajs.react.test._
52+
import japgolly.scalajs.react.test.ReactTestUtils
53+
import japgolly.scalajs.react.testing_library.dom._
5354
import japgolly.scalajs.react.vdom.html_<^._
5455
import utest._
5556

library/project/Build.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ object ScalaJsReact {
222222
lazy val tests = project
223223
.dependsOn(testUtil, coreExtCatsEffect, extraExtMonocle3)
224224
.dependsOn(coreBundleCallback) // Low priority
225+
.dependsOn(testingLibraryDom % Test)
225226
.configure(commonSettings, preventPublication, utestSettings, addReactJsDependencies(Test))
226227
.settings(
227228
Test / scalacOptions -= "-Xlint:adapted-args",
@@ -239,6 +240,7 @@ object ScalaJsReact {
239240
(ProvidedJS / "component-es6.js" dependsOn Dep.reactDom.dev) % Test,
240241
(ProvidedJS / "component-fn.js" dependsOn Dep.reactDom.dev) % Test,
241242
(ProvidedJS / "forward-ref.js" dependsOn Dep.reactDom.dev) % Test,
243+
Dep.testingLibraryDomJs.value % Test,
242244
),
243245
)
244246

library/testUtil/src/main/scala/japgolly/scalajs/react/test/ReactEventType.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ object ReactEventType {
2828
// val target : DOMEventTarget = js.native
2929
// val `type` : String = js.native
3030
js.Dynamic.literal(
31-
bubbles = false,
32-
cancelable = false,
31+
bubbles = true,
32+
cancelable = true,
3333
defaultPrevented = false,
3434
isTrusted = false,
3535
timeStamp = System.currentTimeMillis().toDouble,
@@ -172,4 +172,4 @@ object ReactEventType {
172172
deltaMode = 0, // The delta values are specified in pixels
173173
))
174174
}
175-
}
175+
}

library/testUtil/src/main/scala/japgolly/scalajs/react/test/SimEvent.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import scala.scalajs.js.Dynamic
77
// NOTE: Do not use UndefOr for arguments below; undefined causes Phantom-bloody-JS to crash.
88
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
99

10+
@deprecated("See the 3.0.0 changelog for migration details", "3.0.0 / React v18")
1011
object SimEvent {
1112

1213
case class Change(value : String = "",

library/testUtil/src/main/scala/japgolly/scalajs/react/test/Simulate.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import japgolly.scalajs.react.test.facade
55
import scala.scalajs.js
66

77
/** https://reactjs.org/docs/test-utils.html#simulate */
8+
@deprecated("See the 3.0.0 changelog for migration details", "3.0.0 / React v18")
89
object Simulate {
910
import ReactEventType._
1011

library/testUtil/src/main/scala/japgolly/scalajs/react/test/Simulation.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scala.scalajs.js
55
/**
66
* Allows composition and abstraction of `LegacyReactTestUtils.Simulate` procedures.
77
*/
8+
@deprecated("See the 3.0.0 changelog for migration details", "3.0.0 / React v18")
89
class Simulation(_run: (() => ReactOrDomNode) => Unit) {
910

1011
def run(n: => ReactOrDomNode): Unit =
@@ -20,6 +21,7 @@ class Simulation(_run: (() => ReactOrDomNode) => Unit) {
2021
cs foreach (run(_))
2122
}
2223

24+
@deprecated("See the 3.0.0 changelog for migration details", "3.0.0 / React v18")
2325
object Simulation {
2426

2527
def apply(run: (=> ReactOrDomNode) => Unit): Simulation =

library/tests/src/test/scala/japgolly/scalajs/react/MiscTest.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import scala.util.Try
1313
import utest._
1414

1515
object MiscTest extends AsyncTestSuite {
16+
import japgolly.scalajs.react.testing_library.dom._
1617

1718
lazy val CA = ScalaComponent.builder[Unit]("CA").render_C(c => <.div(c)).build
1819
lazy val CB = ScalaComponent.builder[Unit]("CB").render_C(c => <.span(c)).build

library/tests/src/test/scala/japgolly/scalajs/react/core/HooksTest.scala

Lines changed: 106 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import japgolly.scalajs.react.test.ReactTestUtils._
88
import japgolly.scalajs.react.test.TestUtil._
99
import japgolly.scalajs.react.test.{DomTester, TestReactRoot}
1010
import japgolly.scalajs.react.vdom.html_<^._
11-
import org.scalajs.dom.html.Input
1211
import scala.collection.mutable
1312
import scala.scalajs.js
1413
import utest._
@@ -1590,80 +1589,80 @@ object HooksTest extends AsyncTestSuite {
15901589
}
15911590
}
15921591

1593-
private def testUseRefVdom(): Unit = {
1594-
var text = "uninitialised"
1595-
val comp = ScalaFnComponent.withHooks[Unit]
1596-
.useRefToVdom[Input]
1597-
.useState("x")
1598-
.render { (_, inputRef, s) =>
1599-
def onChange(e: ReactEventFromInput): Callback =
1600-
s.setState(e.target.value)
1601-
1602-
def btn: Callback =
1603-
for {
1604-
i <- inputRef.get.asCBO
1605-
// _ <- Callback.log(s"i.value = [${i.value}]")
1606-
} yield {
1607-
text = i.value
1608-
}
1609-
1610-
<.div(
1611-
<.input.text.withRef(inputRef)(^.value := s.value, ^.onChange ==> onChange),
1612-
<.button(^.onClick --> btn)
1613-
)
1614-
}
1615-
1616-
test(comp()) { t =>
1617-
t.assertInputText("x")
1618-
for {
1619-
_ <- t.clickButton()
1620-
_ = assertEq(text, "x")
1621-
_ <- t.setInputText("hehe")
1622-
_ = t.assertInputText("hehe")
1623-
_ <- t.clickButton()
1624-
_ = assertEq(text, "hehe")
1625-
} yield ()
1626-
}
1627-
}
1592+
// private def testUseRefVdom(): Unit = {
1593+
// var text = "uninitialised"
1594+
// val comp = ScalaFnComponent.withHooks[Unit]
1595+
// .useRefToVdom[Input]
1596+
// .useState("x")
1597+
// .render { (_, inputRef, s) =>
1598+
// def onChange(e: ReactEventFromInput): Callback =
1599+
// s.setState(e.target.value)
16281600

1629-
private def testMonadicUseRefVdom(): Unit = {
1630-
var text = "uninitialised"
1631-
val comp = ScalaFnComponent[Unit] { _ =>
1632-
for {
1633-
inputRef <- useRefToVdom[Input]
1634-
s <- useState("x")
1635-
} yield {
1636-
1637-
def onChange(e: ReactEventFromInput): Callback =
1638-
s.setState(e.target.value)
1601+
// def btn: Callback =
1602+
// for {
1603+
// i <- inputRef.get.asCBO
1604+
// // _ <- Callback.log(s"i.value = [${i.value}]")
1605+
// } yield {
1606+
// text = i.value
1607+
// }
16391608

1640-
def btn: Callback =
1641-
for {
1642-
i <- inputRef.get.asCBO
1643-
// _ <- Callback.log(s"i.value = [${i.value}]")
1644-
} yield {
1645-
text = i.value
1646-
}
1609+
// <.div(
1610+
// <.input.text.withRef(inputRef)(^.value := s.value, ^.onChange ==> onChange),
1611+
// <.button(^.onClick --> btn)
1612+
// )
1613+
// }
1614+
1615+
// test(comp()) { t =>
1616+
// t.assertInputText("x")
1617+
// for {
1618+
// _ <- t.clickButton()
1619+
// _ = assertEq(text, "x")
1620+
// _ <- t.setInputText("hehe")
1621+
// _ = t.assertInputText("hehe")
1622+
// _ <- t.clickButton()
1623+
// _ = assertEq(text, "hehe")
1624+
// } yield ()
1625+
// }
1626+
// }
1627+
1628+
// private def testMonadicUseRefVdom(): Unit = {
1629+
// var text = "uninitialised"
1630+
// val comp = ScalaFnComponent[Unit] { _ =>
1631+
// for {
1632+
// inputRef <- useRefToVdom[Input]
1633+
// s <- useState("x")
1634+
// } yield {
1635+
1636+
// def onChange(e: ReactEventFromInput): Callback =
1637+
// s.setState(e.target.value)
1638+
1639+
// def btn: Callback =
1640+
// for {
1641+
// i <- inputRef.get.asCBO
1642+
// // _ <- Callback.log(s"i.value = [${i.value}]")
1643+
// } yield {
1644+
// text = i.value
1645+
// }
16471646

1648-
<.div(
1649-
<.input.text.withRef(inputRef)(^.value := s.value, ^.onChange ==> onChange),
1650-
<.button(^.onClick --> btn)
1651-
)
1652-
}
1653-
}
1647+
// <.div(
1648+
// <.input.text.withRef(inputRef)(^.value := s.value, ^.onChange ==> onChange),
1649+
// <.button(^.onClick --> btn)
1650+
// )
1651+
// }
1652+
// }
16541653

1655-
test(comp()) { t =>
1656-
t.assertInputText("x")
1657-
for {
1658-
_ <- t.clickButton()
1659-
_ = assertEq(text, "x")
1660-
_ <- t.setInputText("hehe")
1661-
_ = t.assertInputText("hehe")
1662-
_ <- t.clickButton()
1663-
_ = assertEq(text, "hehe")
1664-
} yield ()
1665-
}
1666-
}
1654+
// test(comp()) { t =>
1655+
// t.assertInputText("x")
1656+
// for {
1657+
// _ <- t.clickButton()
1658+
// _ = assertEq(text, "x")
1659+
// _ <- t.setInputText("hehe")
1660+
// _ = t.assertInputText("hehe")
1661+
// _ <- t.clickButton()
1662+
// _ = assertEq(text, "hehe")
1663+
// } yield ()
1664+
// }
1665+
// }
16671666

16681667
private def testUseReducer(): Unit = {
16691668
def add(n: Int): (Int, Int) => Int = _ + _ + n
@@ -2500,41 +2499,41 @@ object HooksTest extends AsyncTestSuite {
25002499
}
25012500
}
25022501

2503-
private def testRenderWithReuseAndUseRefToVdom(): Unit = {
2504-
var text = "uninitialised"
2505-
val comp = ScalaFnComponent.withHooks[Unit]
2506-
.useRefToVdom[Input]
2507-
.useState("x")
2508-
.renderWithReuse { (_, inputRef, s) =>
2509-
def onChange(e: ReactEventFromInput): Callback =
2510-
s.setState(e.target.value)
2511-
2512-
def btn: Callback =
2513-
for {
2514-
i <- inputRef.get.asCBO
2515-
// _ <- Callback.log(s"i.value = [${i.value}]")
2516-
} yield {
2517-
text = i.value
2518-
}
2502+
// private def testRenderWithReuseAndUseRefToVdom(): Unit = {
2503+
// var text = "uninitialised"
2504+
// val comp = ScalaFnComponent.withHooks[Unit]
2505+
// .useRefToVdom[Input]
2506+
// .useState("x")
2507+
// .renderWithReuse { (_, inputRef, s) =>
2508+
// def onChange(e: ReactEventFromInput): Callback =
2509+
// s.setState(e.target.value)
25192510

2520-
<.div(
2521-
<.input.text.withRef(inputRef)(^.value := s.value, ^.onChange ==> onChange),
2522-
<.button(^.onClick --> btn)
2523-
)
2524-
}
2511+
// def btn: Callback =
2512+
// for {
2513+
// i <- inputRef.get.asCBO
2514+
// // _ <- Callback.log(s"i.value = [${i.value}]")
2515+
// } yield {
2516+
// text = i.value
2517+
// }
25252518

2526-
test(comp()) { t =>
2527-
t.assertInputText("x")
2528-
for {
2529-
_ <- t.clickButton()
2530-
_ = assertEq(text, "x")
2531-
_ <- t.setInputText("hehe")
2532-
_ = t.assertInputText("hehe")
2533-
_ <- t.clickButton()
2534-
_ = assertEq(text, "hehe")
2535-
} yield ()
2536-
}
2537-
}
2519+
// <.div(
2520+
// <.input.text.withRef(inputRef)(^.value := s.value, ^.onChange ==> onChange),
2521+
// <.button(^.onClick --> btn)
2522+
// )
2523+
// }
2524+
2525+
// test(comp()) { t =>
2526+
// t.assertInputText("x")
2527+
// for {
2528+
// _ <- t.clickButton()
2529+
// _ = assertEq(text, "x")
2530+
// _ <- t.setInputText("hehe")
2531+
// _ = t.assertInputText("hehe")
2532+
// _ <- t.clickButton()
2533+
// _ = assertEq(text, "hehe")
2534+
// } yield ()
2535+
// }
2536+
// }
25382537

25392538
private def testUseReused(): Unit = {
25402539
implicit val reusePIByRounding: Reusability[PI] = Reusability.by(_.pi / 2)
@@ -2841,11 +2840,11 @@ object HooksTest extends AsyncTestSuite {
28412840
"useRef" - {
28422841
"manual" - testUseRefManual()
28432842
"manualBy" - testUseRefManualBy()
2844-
"vdom" - testUseRefVdom()
2843+
// "vdom" - testUseRefVdom()
28452844
}
28462845
"useRef (monadic)" - {
28472846
"manual" - testMonadicUseRefManual()
2848-
"vdom" - testMonadicUseRefVdom()
2847+
// "vdom" - testMonadicUseRefVdom()
28492848
}
28502849
"useReducer" - testUseReducer()
28512850
"useReducer (monadic)" - testMonadicUseReducer()
@@ -2910,7 +2909,7 @@ object HooksTest extends AsyncTestSuite {
29102909
"main" - testRenderWithReuse()
29112910
"never" - testRenderWithReuseNever()
29122911
"useRef" - testRenderWithReuseAndUseRef()
2913-
"useRefToVdom" - testRenderWithReuseAndUseRefToVdom()
2912+
// "useRefToVdom" - testRenderWithReuseAndUseRefToVdom()
29142913
}
29152914
"renderWithReuse (monadic alternative using Render.memo)" - {
29162915
"main" - testMonadicRenderWithReuse()

library/tests/src/test/scala/japgolly/scalajs/react/core/ReusabilityTest.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import sourcecode.Line
1212
import utest._
1313

1414
object ReusabilityTest extends TestSuite {
15+
import japgolly.scalajs.react.testing_library.dom._
16+
1517
japgolly.scalajs.react.test.InitTestEnv()
1618

1719
object SampleComponent1 {

library/tests/src/test/scala/japgolly/scalajs/react/core/ScalaComponentPTest.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package japgolly.scalajs.react.core
22

33
import japgolly.scalajs.react._
44
import japgolly.scalajs.react.test.TestUtil._
5-
import japgolly.scalajs.react.test.{InferenceHelpers, ReactTestUtils, Simulate}
5+
import japgolly.scalajs.react.test.{InferenceHelpers, ReactTestUtils}
6+
import japgolly.scalajs.react.testing_library.dom.Simulate
67
import japgolly.scalajs.react.vdom.ImplicitsFromRaw._
78
import utest._
89

910
object ScalaComponentPTest extends TestSuite {
11+
1012
japgolly.scalajs.react.test.InitTestEnv()
1113

1214
private case class BasicProps(name: String)

0 commit comments

Comments
 (0)