Skip to content

Commit

Permalink
Cross-compile to Scala.js 1.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jul 29, 2021
1 parent 051e538 commit efe2feb
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ env:
- secure: Px1jYCcHrx4Eufto0lOK8QOciiZ72baECbZWYEtz9CZfjpmuFIovfSShNrni2rYCTmxdZmpl2Royq+BhbO7IbVKXXGP3Spq0cxPfVa1VQ+s9GRjbxY7QyXUbXq2Xcs/2xiieBXLihrZwIeRKGW3SXmwiZvypYyPknugezPgx2I0ULIZOJGD3LC2WAKfdY/a5Ok6ErxQj35U01ONif6ey2B97+s3w8D/P1Qw1f2p5dpU5QCr4iXfKQgC4Cje1/A4s+Qk2BIiQbUEmBRLeTWLsAcx501BCmMoFc+t64behU6wDflvcHdDk9pWUfii0ub4PM3QNYPydTBzadlr29rkrZOL/JtXpemcRh6q0aJLcLofmJd4ess7NIzAu7/oPVWe9sv1H989GKtuqDWrZo1fTcGPYT5nmjvssrYCVPl5HCJG/jtyN4EHtNnzpl51OA4p6K3itTqdSm1JwoLdz6ReGsQi30hdloxHTyOmJDYY5AvTz8utIdAnKr8EjSM5xFbN7bApWISDAeRNrk63hCUhJvCrmBWI04PLZrT+lRylhXTieDit09jZOpfbFRHUZCw2eFp7ddhUNZArnOZOn7phphgxqIXnaRpWYDgGaCqJo4d79URJGowssRh5Vs+ozOkSvZfUlE/crQDLwaXnVt25V6EZWrOOMHUSW/kLiWCg8Vqc=
- secure: a8GtpUc81dCfS2D/VKbNKxz3nztcnHyPatS8QHUr2nG2hTMZbL/7O4714EtI1QlBb7Ke64tAhAASaszJqDlWkI9xuMo6G+9U2Ui/FMG/XMe3CiBPoU56jx7bSOP7SG02wsE39hF/pqjuMDTNDSBz9+vSuyB1GnwLP15bej49ityWet/6yDAZac9atwNbLRfeuIfpQa8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AJVzeoiRZIjAS3KHjt8CokE2wetpvhTqbsym0zTI/zXaMZS58ioahrkR6pb+Fa7Pu9wjwhVU1+McZDcf/o9Gn7h0=
jobs:
- SCALAJS_VERSION=0.6.31
- SCALAJS_VERSION=1.1.0
before_install:
- nvm install 12 &&
nvm use 12
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
[![Build Status](https://travis-ci.com/scommons/scommons-react.svg?branch=master)](https://travis-ci.com/scommons/scommons-react)
[![Coverage Status](https://coveralls.io/repos/github/scommons/scommons-react/badge.svg?branch=master)](https://coveralls.io/github/scommons/scommons-react?branch=master)
[![scala-index](https://index.scala-lang.org/scommons/scommons-react/scommons-react-core/latest-by-scala-version.svg?targetType=Js)](https://index.scala-lang.org/scommons/scommons-react/scommons-react-core)
[![Scala.js](https://www.scala-js.org/assets/badges/scalajs-0.6.29.svg)](https://www.scala-js.org)
[![Scala.js 0.6](https://www.scala-js.org/assets/badges/scalajs-0.6.29.svg)](https://www.scala-js.org)
[![Scala.js 1.0](https://www.scala-js.org/assets/badges/scalajs-1.1.0.svg)](https://www.scala-js.org)

## Scala Commons React
[Scala.js](https://www.scala-js.org) facades for common [React.js](https://reactjs.org) utilities and components.
Expand Down
10 changes: 9 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
//resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin" % "0.5.0-SNAPSHOT").changing())
val scalaJSVersion =
Option(System.getenv("SCALAJS_VERSION")).getOrElse("0.6.31")

//addSbtPlugin("org.scommons.sbt" % "sbt-scommons-plugin" % "0.5.0")
if (scalaJSVersion.startsWith("0.6")) {
addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin-sjs06" % "0.5.0-SNAPSHOT").changing())
}
else {
addSbtPlugin(("org.scommons.sbt" % "sbt-scommons-plugin" % "0.5.0-SNAPSHOT").changing())
}

addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.5")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.0")
Expand Down
29 changes: 27 additions & 2 deletions project/src/main/scala/definitions/ScalaJsModule.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
package definitions

import org.scalajs.sbtplugin.ScalaJSPlugin
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
import sbt.Keys._
import sbt._
import scommons.sbtplugin.project.CommonClientModule

import scalajsbundler.sbtplugin.ScalaJSBundlerPlugin
import scommons.sbtplugin.project.CommonClientModule
import scoverage.ScoverageKeys.{coverageEnabled, coverageScalacPluginVersion}
import scoverage.ScoverageSbtPlugin._

trait ScalaJsModule extends ReactModule {

override def definition: Project = {
super.definition
.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)
.settings(CommonClientModule.settings: _*)
.settings(
//TODO: remove these temporal fixes for Scala.js 1.1+ and scoverage
coverageScalacPluginVersion := {
val current = coverageScalacPluginVersion.value
if (scalaJSVersion.startsWith("0.6")) current
else "1.4.2" //the only version that supports Scala.js 1.1+
},
libraryDependencies ~= { modules =>
if (scalaJSVersion.startsWith("0.6")) modules
else modules.filter(_.organization != OrgScoverage)
},
libraryDependencies ++= {
if (coverageEnabled.value) {
if (scalaJSVersion.startsWith("0.6")) Nil
else Seq(
OrgScoverage %% s"${ScalacRuntimeArtifact}_sjs1" % coverageScalacPluginVersion.value,
OrgScoverage %% ScalacPluginArtifact % coverageScalacPluginVersion.value % ScoveragePluginConfig.name
)
}
else Nil
}
)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package scommons.react.showcase

import org.scalajs.dom.raw.HTMLInputElement
import scommons.react.showcase.ReactRefDemoSpec._
import scommons.react.test._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportAll

class ReactRefDemoSpec extends TestSpec with TestRendererUtils {

it should "set focus to input element when onClick" in {
//given
val inputMock = mock[HTMLInputElementMock]
val focusMock = mockFunction[Unit]
val comp = testRender(<(ReactRefDemo())()(), { el =>
if (el.`type` == "input".asInstanceOf[js.Any]) inputMock.asInstanceOf[js.Any]
if (el.`type` == "input".asInstanceOf[js.Any]) {
createHTMLInputElement(focusMock)
}
else null
})

Expand All @@ -21,7 +23,7 @@ class ReactRefDemoSpec extends TestSpec with TestRendererUtils {
}

//then
(inputMock.focus _).expects()
focusMock.expects()

//when
button.props.onClick(null)
Expand All @@ -47,9 +49,9 @@ class ReactRefDemoSpec extends TestSpec with TestRendererUtils {

object ReactRefDemoSpec {

@JSExportAll
trait HTMLInputElementMock {

def focus(): Unit
def createHTMLInputElement(focusMock: () => Unit): HTMLInputElement = {
js.Dynamic.literal(
"focus" -> (focusMock: js.Function)
).asInstanceOf[HTMLInputElement]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scommons.react.showcase.app.counter

import io.github.shogowada.scalajs.reactjs.React.Props
import io.github.shogowada.scalajs.reactjs.redux.Redux.Dispatch
import scommons.react.showcase.app.ShowcaseStateDef
import scommons.react.showcase.app.ShowcaseState
import scommons.react.test.TestSpec

class CounterControllerSpec extends TestSpec {
Expand All @@ -23,8 +23,7 @@ class CounterControllerSpec extends TestSpec {
val controller = new CounterController(actions)
val dispatch = mock[Dispatch]
val counterState = mock[CounterState]
val state = mock[ShowcaseStateDef]
(state.counterState _).expects().returning(counterState)
val state = ShowcaseState(None, counterState)

//when
val result = controller.mapStateToProps(dispatch, state, props)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package scommons.react.showcase.app.counter

import io.github.shogowada.scalajs.reactjs.redux.Redux.Dispatch
import scommons.react.redux.task.FutureTask
import scommons.react.showcase.app.counter.CounterActions._
import scommons.react.test._
Expand All @@ -11,7 +12,11 @@ class CounterPanelSpec extends TestSpec with TestRendererUtils {
it should "dispatch CounterChangeAction when onClick on plus button" in {
//given
val dispatch = mockFunction[Any, Any]
val actions = mock[CounterActions]
val changeCounterMock = mockFunction[Dispatch, Int, Int, CounterChangeAction]
val actions = new CounterActions {
override def changeCounter(dispatch: Dispatch, counter: Int, dx: Int): CounterChangeAction =
changeCounterMock(dispatch, counter, dx)
}
val state = CounterState(123)
val props = CounterPanelProps(dispatch, actions, state)
val root = createTestRenderer(<(CounterPanel())(^.wrapped := props)()).root
Expand All @@ -23,7 +28,7 @@ class CounterPanelSpec extends TestSpec with TestRendererUtils {
)

//then
(actions.changeCounter _).expects(dispatch, state.value, 1).returning(action)
changeCounterMock.expects(dispatch, state.value, 1).returning(action)
dispatch.expects(action)

//when
Expand All @@ -33,7 +38,11 @@ class CounterPanelSpec extends TestSpec with TestRendererUtils {
it should "dispatch CounterChangeAction when onClick on minus button" in {
//given
val dispatch = mockFunction[Any, Any]
val actions = mock[CounterActions]
val changeCounterMock = mockFunction[Dispatch, Int, Int, CounterChangeAction]
val actions = new CounterActions {
override def changeCounter(dispatch: Dispatch, counter: Int, dx: Int): CounterChangeAction =
changeCounterMock(dispatch, counter, dx)
}
val state = CounterState(123)
val props = CounterPanelProps(dispatch, actions, state)
val root = createTestRenderer(<(CounterPanel())(^.wrapped := props)()).root
Expand All @@ -45,7 +54,7 @@ class CounterPanelSpec extends TestSpec with TestRendererUtils {
)

//then
(actions.changeCounter _).expects(dispatch, state.value, -1).returning(action)
changeCounterMock.expects(dispatch, state.value, -1).returning(action)
dispatch.expects(action)

//when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package scommons.react.showcase.hooks

import scommons.react.showcase.hooks.UseRefDemoSpec.MouseSyntheticEventMock
import org.scalajs.dom.raw.HTMLInputElement
import scommons.react.showcase.hooks.UseRefDemoSpec._
import scommons.react.test._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportAll

class UseRefDemoSpec extends TestSpec with TestRendererUtils {

it should "set focus to input element when onClick" in {
//given
val inputMock = mock[MouseSyntheticEventMock]
val focusMock = mockFunction[Unit]
val root = createTestRenderer(<(UseRefDemo())()(), { el =>
if (el.`type` == "input".asInstanceOf[js.Any]) inputMock.asInstanceOf[js.Any]
if (el.`type` == "input".asInstanceOf[js.Any]) {
createHTMLInputElement(focusMock)
}
else null
}).root
val button = inside(findComponents(root, <.button.name)) {
case List(b) => b
}

//then
(inputMock.focus _).expects()
focusMock.expects()

//when
button.props.onClick(null)
Expand All @@ -43,9 +45,9 @@ class UseRefDemoSpec extends TestSpec with TestRendererUtils {

object UseRefDemoSpec {

@JSExportAll
trait MouseSyntheticEventMock {

def focus(): Unit
def createHTMLInputElement(focusMock: () => Unit): HTMLInputElement = {
js.Dynamic.literal(
"focus" -> (focusMock: js.Function)
).asInstanceOf[HTMLInputElement]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import scommons.react.test.TestSpec
import scommons.react.test.raw.TestRendererSpec._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportAll

class TestRendererSpec extends TestSpec {

Expand All @@ -28,16 +27,16 @@ class TestRendererSpec extends TestSpec {
}
)
}
val inputMock = mock[HTMLInputElementMock]
val focusMock = mockFunction[Unit]
val compMock: js.Function1[TestInstance, js.Any] = { el =>
if (el.`type` == "input".asInstanceOf[js.Any]) {
inputMock.asInstanceOf[HTMLInputElement]
createHTMLInputElement(focusMock)
}
else null
}

//then
(inputMock.focus _).expects()
focusMock.expects()

//when
TestRenderer.create(<(comp())()(), new TestRendererOptions {
Expand Down Expand Up @@ -132,9 +131,9 @@ class TestRendererSpec extends TestSpec {

object TestRendererSpec {

@JSExportAll
trait HTMLInputElementMock {

def focus(): Unit
def createHTMLInputElement(focusMock: () => Unit): HTMLInputElement = {
js.Dynamic.literal(
"focus" -> (focusMock: js.Function)
).asInstanceOf[HTMLInputElement]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import scommons.react.test.util.RendererUtilsSpec._
import scommons.react.test.util.TestRendererUtilsSpec._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportAll

class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance]
with TestRendererUtils {
Expand All @@ -32,15 +31,15 @@ class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance]
)()
}
}
val inputMock = mock[HTMLInputElementMock]
val focusMock = mockFunction[Unit]

//then
(inputMock.focus _).expects()
focusMock.expects()

//when
testRender(<(comp())()(), { el =>
if (el.`type` == "input".asInstanceOf[js.Any]) {
inputMock.asInstanceOf[HTMLInputElement]
createHTMLInputElement(focusMock)
}
else null
})
Expand Down Expand Up @@ -91,9 +90,9 @@ class TestRendererUtilsSpec extends RendererUtilsSpec[TestInstance]

object TestRendererUtilsSpec {

@JSExportAll
trait HTMLInputElementMock {

def focus(): Unit
def createHTMLInputElement(focusMock: () => Unit): HTMLInputElement = {
js.Dynamic.literal(
"focus" -> (focusMock: js.Function)
).asInstanceOf[HTMLInputElement]
}
}

0 comments on commit efe2feb

Please sign in to comment.