From 297ada5cf49e269c44b5cb255150f3cb9c4effe1 Mon Sep 17 00:00:00 2001 From: Viktor Podzigun Date: Thu, 16 Apr 2020 15:27:34 +0200 Subject: [PATCH] Added scommons.react.test and test.dom package objects, fixed #1 --- .../showcase/ClassComponentDemoSpec.scala | 5 ++-- .../showcase/ErrorBoundaryDemoSpec.scala | 5 ++-- .../showcase/FunctionComponentDemoSpec.scala | 5 ++-- .../showcase/ReactFragmentDemoSpec.scala | 8 +++---- .../react/showcase/ReactMemoDemoSpec.scala | 4 +--- .../react/showcase/ReactRefDemoSpec.scala | 5 ++-- .../showcase/app/ShowcaseReactAppSpec.scala | 4 ++-- .../app/counter/CounterPanelSpec.scala | 11 ++++----- .../showcase/dom/ReactPortalDemoSpec.scala | 24 +++++++++---------- .../showcase/hooks/UseContextDemoSpec.scala | 6 ++--- .../showcase/hooks/UseEffectDemoSpec.scala | 6 ++--- .../hooks/UseLayoutEffectDemoSpec.scala | 6 ++--- .../showcase/hooks/UseMemoDemoSpec.scala | 5 ++-- .../showcase/hooks/UseReducerDemoSpec.scala | 5 ++-- .../react/showcase/hooks/UseRefDemoSpec.scala | 13 +++++----- .../showcase/hooks/UseStateDemoSpec.scala | 5 ++-- .../scommons/react/test/dom/package.scala | 8 +++++++ .../scala/scommons/react/test/package.scala | 11 +++++++++ 18 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 test-dom/src/main/scala/scommons/react/test/dom/package.scala create mode 100644 test/src/main/scala/scommons/react/test/package.scala diff --git a/showcase/src/test/scala/scommons/react/showcase/ClassComponentDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/ClassComponentDemoSpec.scala index 8c3d521..8dbead2 100644 --- a/showcase/src/test/scala/scommons/react/showcase/ClassComponentDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/ClassComponentDemoSpec.scala @@ -1,9 +1,8 @@ package scommons.react.showcase import scommons.react._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ import scala.scalajs.js diff --git a/showcase/src/test/scala/scommons/react/showcase/ErrorBoundaryDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/ErrorBoundaryDemoSpec.scala index 5ca334f..fe6538e 100644 --- a/showcase/src/test/scala/scommons/react/showcase/ErrorBoundaryDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/ErrorBoundaryDemoSpec.scala @@ -2,9 +2,8 @@ package scommons.react.showcase import org.scalajs.dom.{Event, window} import scommons.react._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ import scala.scalajs.js diff --git a/showcase/src/test/scala/scommons/react/showcase/FunctionComponentDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/FunctionComponentDemoSpec.scala index bfb9977..52533c8 100644 --- a/showcase/src/test/scala/scommons/react/showcase/FunctionComponentDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/FunctionComponentDemoSpec.scala @@ -1,9 +1,8 @@ package scommons.react.showcase import scommons.react._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ import scala.scalajs.js diff --git a/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala index dab35f4..c3c88a5 100644 --- a/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/ReactFragmentDemoSpec.scala @@ -1,9 +1,9 @@ package scommons.react.showcase import scommons.react._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.raw.React.{Fragment => ReactFragment} +import scommons.react.test._ +import scommons.react.test.dom._ class ReactFragmentDemoSpec extends TestSpec with TestDOMUtils @@ -38,7 +38,7 @@ class ReactFragmentDemoSpec extends TestSpec val result = shallowRender(comp) //then - result.`type` shouldBe raw.React.Fragment + result.`type` shouldBe ReactFragment assertNativeComponent(result, <.>()( diff --git a/showcase/src/test/scala/scommons/react/showcase/ReactMemoDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/ReactMemoDemoSpec.scala index 2a472bc..4c46dc4 100644 --- a/showcase/src/test/scala/scommons/react/showcase/ReactMemoDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/ReactMemoDemoSpec.scala @@ -1,8 +1,6 @@ package scommons.react.showcase -import scommons.react.test.TestSpec -import scommons.react.test.raw.TestRenderer -import scommons.react.test.util.TestRendererUtils +import scommons.react.test._ class ReactMemoDemoSpec extends TestSpec with TestRendererUtils { diff --git a/showcase/src/test/scala/scommons/react/showcase/ReactRefDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/ReactRefDemoSpec.scala index b4cccb1..2cf76a7 100644 --- a/showcase/src/test/scala/scommons/react/showcase/ReactRefDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/ReactRefDemoSpec.scala @@ -1,9 +1,8 @@ package scommons.react.showcase import org.scalajs.dom.document -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class ReactRefDemoSpec extends TestSpec with TestDOMUtils diff --git a/showcase/src/test/scala/scommons/react/showcase/app/ShowcaseReactAppSpec.scala b/showcase/src/test/scala/scommons/react/showcase/app/ShowcaseReactAppSpec.scala index 61ad78f..e1b6608 100644 --- a/showcase/src/test/scala/scommons/react/showcase/app/ShowcaseReactAppSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/app/ShowcaseReactAppSpec.scala @@ -1,8 +1,8 @@ package scommons.react.showcase.app import org.scalajs.dom -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils +import scommons.react.test._ +import scommons.react.test.dom._ class ShowcaseReactAppSpec extends TestSpec with TestDOMUtils { diff --git a/showcase/src/test/scala/scommons/react/showcase/app/counter/CounterPanelSpec.scala b/showcase/src/test/scala/scommons/react/showcase/app/counter/CounterPanelSpec.scala index 45ad1d3..6c23853 100644 --- a/showcase/src/test/scala/scommons/react/showcase/app/counter/CounterPanelSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/app/counter/CounterPanelSpec.scala @@ -1,12 +1,11 @@ package scommons.react.showcase.app.counter -import org.scalajs.dom +import org.scalajs.dom.document import scommons.react._ import scommons.react.redux.task.FutureTask import scommons.react.showcase.app.counter.CounterActions._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.ShallowRendererUtils +import scommons.react.test._ +import scommons.react.test.dom._ import scala.concurrent.Future @@ -21,7 +20,7 @@ class CounterPanelSpec extends TestSpec val state = CounterState(123) val props = CounterPanelProps(dispatch, actions, state) domRender(<(CounterPanel())(^.wrapped := props)()) - val button = dom.document.body.querySelectorAll("button").item(0) + val button = document.body.querySelectorAll("button").item(0) val action = CounterChangeAction( FutureTask("Changing Counter", Future.successful(0)) ) @@ -41,7 +40,7 @@ class CounterPanelSpec extends TestSpec val state = CounterState(123) val props = CounterPanelProps(dispatch, actions, state) domRender(<(CounterPanel())(^.wrapped := props)()) - val button = dom.document.body.querySelectorAll("button").item(1) + val button = document.body.querySelectorAll("button").item(1) val action = CounterChangeAction( FutureTask("Changing Counter", Future.successful(0)) ) diff --git a/showcase/src/test/scala/scommons/react/showcase/dom/ReactPortalDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/dom/ReactPortalDemoSpec.scala index b3f106e..db214fe 100644 --- a/showcase/src/test/scala/scommons/react/showcase/dom/ReactPortalDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/dom/ReactPortalDemoSpec.scala @@ -1,9 +1,9 @@ package scommons.react.showcase.dom -import org.scalajs.dom +import org.scalajs.dom.{Element, document} import scommons.react._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils +import scommons.react.test._ +import scommons.react.test.dom._ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { @@ -14,7 +14,7 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { )) assertDOMElement(domContainer, <.div()()) - assertDOMElement(dom.document.body.querySelector(".portal-content"), + assertDOMElement(document.body.querySelector(".portal-content"), <.div(^("class") := "portal-content")( "Portal Child" ) @@ -23,7 +23,7 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { //when & then domRender(<(ReactPortalDemo())(^.wrapped := ReactPortalDemoProps(show = false))()) - dom.document.body.querySelectorAll(".portal-content").length shouldBe 0 + document.body.querySelectorAll(".portal-content").length shouldBe 0 } it should "re-render portal content" in { @@ -32,7 +32,7 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { "Portal Child" )) assertDOMElement(domContainer, <.div()()) - assertDOMElement(dom.document.body.querySelector(".portal-content"), + assertDOMElement(document.body.querySelector(".portal-content"), <.div(^("class") := "portal-content")( "Portal Child" ) @@ -42,7 +42,7 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { domRender(<(ReactPortalDemo())(^.wrapped := ReactPortalDemoProps(show = true))( "Portal Updated" )) - assertDOMElement(dom.document.body.querySelector(".portal-content"), + assertDOMElement(document.body.querySelector(".portal-content"), <.div(^("class") := "portal-content")( "Portal Updated" ) @@ -50,7 +50,7 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { //when & then domRender(<(ReactPortalDemo())(^.wrapped := ReactPortalDemoProps(show = false))()) - dom.document.body.querySelectorAll(".portal-content").length shouldBe 0 + document.body.querySelectorAll(".portal-content").length shouldBe 0 } it should "render several portals" in { @@ -81,13 +81,13 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { assertDOMElement(domContainer, <.div()()) - val portals = dom.document.body.querySelectorAll(".portal-content") - assertDOMElement(portals.item(0).asInstanceOf[dom.Element], + val portals = document.body.querySelectorAll(".portal-content") + assertDOMElement(portals.item(0).asInstanceOf[Element], <.div(^("class") := "portal-content")( "Portal 1" ) ) - assertDOMElement(portals.item(1).asInstanceOf[dom.Element], + assertDOMElement(portals.item(1).asInstanceOf[Element], <.div(^("class") := "portal-content")( "Portal 2" ) @@ -95,6 +95,6 @@ class ReactPortalDemoSpec extends TestSpec with TestDOMUtils { //when & then domRender(<(comp())(^.wrapped := Tuple2(false, false))()) - dom.document.body.querySelectorAll(".portal-content").length shouldBe 0 + document.body.querySelectorAll(".portal-content").length shouldBe 0 } } diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseContextDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseContextDemoSpec.scala index 9e0007b..c49a309 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseContextDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseContextDemoSpec.scala @@ -2,10 +2,8 @@ package scommons.react.showcase.hooks import scommons.react._ import scommons.react.showcase.hooks.UseContextDemoApp._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.raw.{ShallowInstance, TestRenderer} -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseContextDemoSpec extends TestSpec with TestDOMUtils diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseEffectDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseEffectDemoSpec.scala index d78e57f..5a33580 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseEffectDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseEffectDemoSpec.scala @@ -1,9 +1,7 @@ package scommons.react.showcase.hooks -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.raw.TestRenderer -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseEffectDemoSpec extends TestSpec with TestDOMUtils diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseLayoutEffectDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseLayoutEffectDemoSpec.scala index 7ee6719..5aa6e09 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseLayoutEffectDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseLayoutEffectDemoSpec.scala @@ -1,9 +1,7 @@ package scommons.react.showcase.hooks -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.raw.TestRenderer -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseLayoutEffectDemoSpec extends TestSpec with TestDOMUtils diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseMemoDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseMemoDemoSpec.scala index 8aef383..6e7e4a2 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseMemoDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseMemoDemoSpec.scala @@ -1,8 +1,7 @@ package scommons.react.showcase.hooks -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseMemoDemoSpec extends TestSpec with TestDOMUtils diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseReducerDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseReducerDemoSpec.scala index 45e3989..5f62d87 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseReducerDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseReducerDemoSpec.scala @@ -1,8 +1,7 @@ package scommons.react.showcase.hooks -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseReducerDemoSpec extends TestSpec with TestDOMUtils diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseRefDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseRefDemoSpec.scala index 3b7b241..2f629c0 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseRefDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseRefDemoSpec.scala @@ -1,10 +1,9 @@ package scommons.react.showcase.hooks -import org.scalajs.dom +import org.scalajs.dom.document import scommons.react._ -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseRefDemoSpec extends TestSpec with TestDOMUtils @@ -30,14 +29,14 @@ class UseRefDemoSpec extends TestSpec domRender(<(UseRefDemo())()()) val button = domContainer.querySelector("button") - dom.document.hasFocus() shouldBe false + document.hasFocus() shouldBe false //when fireDomEvent(Simulate.click(button)) //then - domContainer.querySelector("input") shouldBe dom.document.activeElement - dom.document.hasFocus() shouldBe true + domContainer.querySelector("input") shouldBe document.activeElement + document.hasFocus() shouldBe true } it should "shallow render component" in { diff --git a/showcase/src/test/scala/scommons/react/showcase/hooks/UseStateDemoSpec.scala b/showcase/src/test/scala/scommons/react/showcase/hooks/UseStateDemoSpec.scala index cd09e46..7c974d1 100644 --- a/showcase/src/test/scala/scommons/react/showcase/hooks/UseStateDemoSpec.scala +++ b/showcase/src/test/scala/scommons/react/showcase/hooks/UseStateDemoSpec.scala @@ -1,8 +1,7 @@ package scommons.react.showcase.hooks -import scommons.react.test.TestSpec -import scommons.react.test.dom.util.TestDOMUtils -import scommons.react.test.util.{ShallowRendererUtils, TestRendererUtils} +import scommons.react.test._ +import scommons.react.test.dom._ class UseStateDemoSpec extends TestSpec with TestDOMUtils diff --git a/test-dom/src/main/scala/scommons/react/test/dom/package.scala b/test-dom/src/main/scala/scommons/react/test/dom/package.scala new file mode 100644 index 0000000..84fddd6 --- /dev/null +++ b/test-dom/src/main/scala/scommons/react/test/dom/package.scala @@ -0,0 +1,8 @@ +package scommons.react.test + +package object dom { + + lazy val ReactTestUtils = dom.raw.ReactTestUtils + lazy val TestReactDOM = dom.raw.TestReactDOM + type TestDOMUtils = dom.util.TestDOMUtils +} diff --git a/test/src/main/scala/scommons/react/test/package.scala b/test/src/main/scala/scommons/react/test/package.scala new file mode 100644 index 0000000..75d0c9c --- /dev/null +++ b/test/src/main/scala/scommons/react/test/package.scala @@ -0,0 +1,11 @@ +package scommons.react + +package object test { + + type ShallowInstance = test.raw.ShallowInstance + type ShallowRendererUtils = test.util.ShallowRendererUtils + + lazy val TestRenderer = test.raw.TestRenderer + type TestInstance = test.raw.TestInstance + type TestRendererUtils = test.util.TestRendererUtils +}