Skip to content

Commit 53d95d7

Browse files
authored
Merge pull request #1126 from japgolly/legacy_test_util
Rename `{ => Legacy}ReactTestUtils` & `ReactTestUtils{2 => }`
2 parents 0c020e3 + f34c00c commit 53d95d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+764
-761
lines changed

doc/CONFIG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ object CustomConfig extends ScalaJsReactConfig.Defaults {
164164

165165
# `.test.warnings.react`
166166

167-
When using `ReactTestUtils`, this setting can be used to catch React warnings and turn them into exceptions.
167+
When using `LegacyReactTestUtils`, this setting can be used to catch React warnings and turn them into exceptions.
168168

169169
### Usage:
170170

@@ -196,7 +196,7 @@ object ExampleTest extends TestSuite {
196196
override def tests = Tests {
197197
"example" - {
198198
val comp = ScalaFnComponent[Int](i => <.p(<.td(s"i = $i")))
199-
ReactTestUtils.withRenderedIntoBody(comp(123)).withParent { m =>
199+
LegacyReactTestUtils.withRenderedIntoBody(comp(123)).withParent { m =>
200200
val html = m.outerHTML
201201
assert(html == "<p><td>i = 123</td></p>")
202202
}

doc/TESTING.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ for how to write tests for real-world scalajs-react applications.
1111
#### Contents
1212

1313
- [Setup](#setup)
14-
- [`ReactTestUtils2`](#reacttestutils2-since-300)
15-
- [`ReactTestUtils [DEPRECATED IN 3.0.0]`](#reacttestutils-deprecated-in-300)
14+
- [`ReactTestUtils`](#reacttestutils-since-300)
15+
- [`LegacyReactTestUtils [DEPRECATED IN 3.0.0]`](#legacyreacttestutils-deprecated-in-300)
1616
- [`Simulate` and `Simulation`](#simulate-and-simulation)
1717
- [`Testing props changes`](#testing-props-changes)
1818
- [`ReactTestVar`](#reacttestvar)
@@ -40,9 +40,12 @@ for how to write tests for real-world scalajs-react applications.
4040
commonJSName "ReactTestUtils"
4141
```
4242

43-
# `ReactTestUtils2 [SINCE 3.0.0]`
43+
# `ReactTestUtils [SINCE 3.0.0]`
4444

45-
Read through the following for how to test with `ReactTestUtils2`.
45+
`ReactTestUtils` has been rewritten for scalajs-react v3 and React v18.
46+
What used to be `ReactTestUtils` prior to scalajs-react v3 has been renamed to `LegacyReactTestUtils`.
47+
48+
Read through the following for how to test with `ReactTestUtils`.
4649

4750
```scala
4851
import japgolly.scalajs.react._
@@ -66,7 +69,7 @@ object TestUtilsDemo extends TestSuite {
6669
override def tests = Tests {
6770

6871
// First we render the component
69-
ReactTestUtils2.withRenderedSync(Component("Axe")) { t =>
72+
ReactTestUtils.withRenderedSync(Component("Axe")) { t =>
7073

7174
// We have a variety of ways to test the HTML
7275
t.outerHTML.assert("<div><p>Hi Axe. You clicked 0 times</p><button>Click me</button></div>")
@@ -86,9 +89,11 @@ object TestUtilsDemo extends TestSuite {
8689
}
8790
```
8891

89-
# `ReactTestUtils [DEPRECATED IN 3.0.0]`
92+
# `LegacyReactTestUtils [DEPRECATED IN 3.0.0]`
93+
94+
This used to be called `ReactTestUtils` prior to scalajs-react v3 and React v18.
9095

91-
The main bucket of testing utilities lies in `japgolly.scalajs.react.test.ReactTestUtils`.
96+
The main bucket of testing utilities lies in `japgolly.scalajs.react.test.LegacyReactTestUtils`.
9297

9398
Half of the methods delegate to React.JS's [React.addons.TestUtils](https://facebook.github.io/react/docs/test-utils.html)
9499
(for which there is a raw facade in `japgolly.scalajs.react.test.raw.ReactAddonsTestUtils` if you're interested).
@@ -116,7 +121,7 @@ The other half are new functions added specifically in scalajs-react.
116121

117122
There's only one magic implicit method this time around:
118123
Mounted components get `.outerHtmlScrubbed()` which is shorthand for
119-
`ReactTestUtils.removeReactInternals(m.getDOMNode.outerHTML)`.
124+
`LegacyReactTestUtils.removeReactInternals(m.getDOMNode.outerHTML)`.
120125

121126
# `Simulate` and `Simulation`
122127

@@ -161,8 +166,20 @@ s run component
161166

162167
# Testing props changes
163168

164-
When you want to simulate a parent component re-rendering a child component with different props,
165-
you can test the child directly using `ReactTestUtils.{modify,replace}Props`.
169+
If you're using scalajs-react v3 and React v18, simply call `.render` from your React root. Example:
170+
171+
```scala
172+
ReactTestUtils.withRendered(Carrot.Props("1").render) { t =>
173+
for {
174+
_ <- t.root.render(Carrot.Props("1").render)
175+
_ <- t.root.render(Carrot.Props("2").render)
176+
} yield ()
177+
}
178+
```
179+
180+
If you're not using scalajs-react v3 and React v18, then
181+
when you want to simulate a parent component re-rendering a child component with different props,
182+
you can test the child directly using `LegacyReactTestUtils.{modify,replace}Props`.
166183

167184
Example of code to test:
168185

@@ -181,13 +198,13 @@ val CP = ScalaComponent.builder[String]("asd")
181198
Example test case:
182199

183200
```scala
184-
ReactTestUtils.withRenderedIntoDocument(CP("start")) { m =>
201+
LegacyReactTestUtils.withRenderedIntoDocument(CP("start")) { m =>
185202
assert(m.outerHtmlScrubbed(), "<div>none → start</div>")
186203

187-
ReactTestUtils.modifyProps(CP, m)(_ + "ed")
204+
LegacyReactTestUtils.modifyProps(CP, m)(_ + "ed")
188205
assert(m.outerHtmlScrubbed(), "<div>start → started</div>")
189206

190-
ReactTestUtils.replaceProps(CP, m)("done!")
207+
LegacyReactTestUtils.replaceProps(CP, m)("done!")
191208
assert(m.outerHtmlScrubbed(), "<div>started → done!</div>")
192209
}
193210
```
@@ -227,7 +244,7 @@ object ExampleTest extends TestSuite {
227244
override def tests = TestSuite {
228245

229246
val nameVar = ReactTestVar("guy")
230-
ReactTestUtils.withRenderedIntoDocument(NameChanger(nameVar.stateSnapshot())) { m =>
247+
LegacyReactTestUtils.withRenderedIntoDocument(NameChanger(nameVar.stateSnapshot())) { m =>
231248
SimEvent.Change("bob").simulate(m)
232249
assert(nameVar.value() == "bob")
233250
}
@@ -245,7 +262,7 @@ via `.forceUpdate`.
245262
val component: ScalaComponent[StateAccessPure[Int], Unit, Unit] = ...
246263

247264
val testVar = ReactTestVar(1)
248-
ReactTestUtils.withRenderedIntoDocument(component(testVar.stateAccess)) { m =>
265+
LegacyReactTestUtils.withRenderedIntoDocument(component(testVar.stateAccess)) { m =>
249266
testVar.onUpdate(m.forceUpdate) // Update the component when it changes the state
250267

251268
assert(m.outerHtmlScrubbed() == "<div>1</div>")
@@ -273,7 +290,7 @@ for how to write tests for real-world scalajs-react applications.
273290

274291
# Fatal React warnings
275292

276-
The easiest way to make `ReactTestUtils` to turn React warnings into runtime exceptions,
293+
The easiest way to make `LegacyReactTestUtils` to turn React warnings into runtime exceptions,
277294
is via a [config option](./CONFIG.md#testwarningsreact).
278295

279296
Alternatively, you can do any of the following...
@@ -287,7 +304,7 @@ Alternatively, you can do any of the following...
287304
}
288305
```
289306

290-
- Installing for all `ReactTestUtils` usage
307+
- Installing for all `LegacyReactTestUtils` usage
291308

292309
```scala
293310
import japgolly.scalajs.react.test.ReactTestUtilsConfig

doc/changelog/3.0.0.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
- Monadic hooks! [See documentation here](../HOOKS.md).
2525

26-
- `ReactTestUtils2`: Replacement for `ReactTestUtils`. [See a demo here](../TESTING.md).
26+
- `ReactTestUtils` has been replaced. The old version has been renamed to `LegacyReactTestUtils`. [See a demo here](../TESTING.md), and see a migration script below.
2727

2828
- `Renderable` typeclass for anything that React can render
2929

@@ -41,7 +41,7 @@
4141

4242
- `.renderBackend` — see https://github.com/japgolly/scalajs-react/pull/1125 for how to replace it
4343

44-
- `ReactTestUtils` - Migrate to `ReactTestUtils2` instead
44+
- `ReactTestUtils` is now `LegacyReactTestUtils` and deprecated
4545

4646
### Upgrades
4747

@@ -54,7 +54,16 @@
5454
- ScalaJsDom 2.8.1
5555
- Sourcecode 0.4.4
5656

57-
### Upgrade notes
57+
### Migration
58+
59+
- Run this to switch from `{ => Legacy}ReactTestUtils`, and if you were on the betas `ReactTestUtils{2 => }`:
60+
61+
```sh
62+
find . -type f -name '*.scala' -exec perl -pi -e '
63+
s/\bReactTestUtils\b/LegacyReactTestUtils/g;
64+
s/\bReactTestUtils2\b/ReactTestUtils/g;
65+
' {} +
66+
```
5867

5968
- To upgrade when using `jsDependencies`, make your sbt config look like this (comments for clarity)
6069

downstream-tests/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
These are tests that rely on a locally-published version of scalajs-react.
2+
3+
### How does it work?
4+
5+
The tests are executed outside of sbt via the `./run` script.
6+
7+
Execute `./run -n` to see the test commands.
8+
9+
### What does it do?
10+
11+
For each test, sbt runs the custom `cleanTestAll` task which does the following:
12+
13+
* the `jvm` module tests:
14+
* the JS generated by the `main` branch (as opposed to `test`) of the `js` module, making sure certain bits of code and present or removed by ScalaJS and related compile-time macros (according to system props)
15+
16+
* the `js` module tests:
17+
* semver compatibility (aka MIMA)
18+
* other runtime tests (based on system props)
19+
20+
* the `jsCE` module tests:
21+
* Cats Effect as the default effect library
22+
23+
* the `jsCBIO` module tests:
24+
* scalajs-react's Callbacks as the default effect library

downstream-tests/build.sbt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ lazy val root = Project("root", file("."))
7979
cleanTestAll := (
8080
if (enableJSCE) // How to do this in a better way?
8181
Def.sequential(
82-
mima200 / clean,
82+
// mima200 / clean,
8383
macros / clean,
8484
jvm / clean,
8585
js / clean,
@@ -93,7 +93,7 @@ lazy val root = Project("root", file("."))
9393
).value
9494
else
9595
Def.sequential(
96-
mima200 / clean,
96+
// mima200 / clean,
9797
macros / clean,
9898
jvm / clean,
9999
js / clean,
@@ -133,20 +133,21 @@ lazy val jvm = project
133133
},
134134
)
135135

136-
lazy val mima200 = project
137-
.in(file("mima-2.0.0"))
138-
.enablePlugins(ScalaJSPlugin)
139-
.configure(commonSettings, utestSettings(Compile))
140-
.settings(
141-
libraryDependencies ++= Seq(
142-
"com.github.japgolly.scalajs-react" %%% "core" % "2.0.0" % Provided,
143-
"com.github.japgolly.scalajs-react" %%% "test" % "2.0.0" % Provided,
144-
),
145-
)
136+
// lazy val mima200 = project
137+
// .in(file("mima-2.0.0"))
138+
// .enablePlugins(ScalaJSPlugin)
139+
// .configure(commonSettings, utestSettings(Compile))
140+
// .settings(
141+
// libraryDependencies ++= Seq(
142+
// "com.github.japgolly.scalajs-react" %%% "core" % "2.0.0" % Provided,
143+
// "com.github.japgolly.scalajs-react" %%% "test" % "2.0.0" % Provided,
144+
// ),
145+
// )
146146

147147
lazy val js = project
148148
.enablePlugins(ScalaJSPlugin)
149-
.dependsOn(macros, mima200)
149+
.dependsOn(macros)
150+
// .dependsOn(mima200)
150151
.configure(commonSettings, utestSettings, addReactJsDependencies(Test))
151152
.settings(
152153
scalaJSStage := jsStage,

downstream-tests/js-cbio/src/test/scala/downstream/CBIOBundleTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package downstream
22

33
import japgolly.microlibs.testutil.TestUtil._
4-
import japgolly.scalajs.react.test.ReactTestUtils2
4+
import japgolly.scalajs.react.test.ReactTestUtils
55
import utest._
66
import cats.effect.testing.utest.EffectTestSuite
77
import cats.effect.IO
@@ -13,7 +13,7 @@ object CBIOBundleTests extends EffectTestSuite[IO] {
1313
Globals.clear()
1414

1515
"catnip" - {
16-
ReactTestUtils2.withRendered(Catnip.Component("omg")) { m =>
16+
ReactTestUtils.withRendered(Catnip.Component("omg")) { m =>
1717
IO.sleep(500.millis).map { _ =>
1818
assertEq(Globals.catnipMounts, List("omg"))
1919
m.outerHTML.assert("<div>Hello(1) omg</div>")

downstream-tests/js-ce/src/test/scala/downstream/CatsEffectBundleTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package downstream
22

33
import japgolly.microlibs.testutil.TestUtil._
4-
import japgolly.scalajs.react.test.ReactTestUtils2
4+
import japgolly.scalajs.react.test.ReactTestUtils
55
import utest._
66
import cats.effect.testing.utest.EffectTestSuite
77
import cats.effect.IO
@@ -12,7 +12,7 @@ object CatsEffectBundleTests extends EffectTestSuite[IO] {
1212
Globals.clear()
1313

1414
"catnip" - {
15-
ReactTestUtils2.withRendered_(Catnip.Component("omg")) { m =>
15+
ReactTestUtils.withRendered_(Catnip.Component("omg")) { m =>
1616
assertEq(Globals.catnipMounts, List("omg"))
1717
m.outerHTML.assert("<div>Hello omg</div>")
1818
}.map(_ =>

downstream-tests/js/src/test/scala/downstream/RuntimeTests.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import japgolly.microlibs.compiletime.CompileTimeInfo
55
import japgolly.microlibs.testutil.TestUtil._
66
import japgolly.scalajs.react._
77
import japgolly.scalajs.react.vdom.html_<^._
8-
import japgolly.scalajs.react.test.ReactTestUtils2
8+
import japgolly.scalajs.react.test.ReactTestUtils
99
import japgolly.scalajs.react.util.JsUtil
1010
import org.scalajs.dom.console
1111
import scala.scalajs.js
@@ -66,13 +66,13 @@ object RuntimeTests extends AsyncTestSuite {
6666
val io = IO(completePromise(Try(()))())
6767

6868
for {
69-
_ <- ReactTestUtils2.withRendered(Carrot.Props("1", io).render) { m =>
69+
_ <- ReactTestUtils.withRendered(Carrot.Props("1", io).render) { m =>
7070
for {
7171
_ <- m.root.render(Carrot.Props("1").render)
7272
_ <- m.root.render(Carrot.Props("2").render)
7373
} yield ()
7474
}
75-
_ <- ReactTestUtils2.withRendered(Pumpkin.Component("1")) { m =>
75+
_ <- ReactTestUtils.withRendered(Pumpkin.Component("1")) { m =>
7676
for {
7777
_ <- m.root.render(Pumpkin.Component("1"))
7878
_ <- m.root.render(Pumpkin.Component("2"))
@@ -95,14 +95,14 @@ object RuntimeTests extends AsyncTestSuite {
9595

9696
"react" - {
9797
val c = ScalaFnComponent[Int](i => <.p(<.td(s"i = $i")))
98-
ReactTestUtils2.withRendered_(c(123))(_ => ()).attemptTry.map { t =>
98+
ReactTestUtils.withRendered_(c(123))(_ => ()).attemptTry.map { t =>
9999
assertEq(t.isFailure, testWarningsReact.contains("react"))
100100
}
101101
}
102102

103103
"unlreated" - {
104104
val c = ScalaFnComponent[Int](i => <.p(s"i = $i"))
105-
ReactTestUtils2.withRendered_(c(123)) { _ =>
105+
ReactTestUtils.withRendered_(c(123)) { _ =>
106106
console.info(".")
107107
console.log(".")
108108
console.warn(".")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../../library/tests/src/test/scala/japgolly/scalajs/react/test/DomTester.scala

downstream-tests/mima-2.0.0/src/main/scala/downstream/mima200/DomTester.scala

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)