Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for recent tooling versions - Scala 3 #20

Open
wants to merge 7 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
enablePlugins(ScalaJSPlugin)

name := "Scala.js Tutorial"
scalaVersion := "3.3.3"

// This is an application with a main method
scalaJSUseMainModuleInitializer := true

libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.8.0"

// Add support for the DOM in `run` and `test`
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()

// uTest settings
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.8.2" % "test"
testFrameworks += new TestFramework("utest.runner.Framework")
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.9
3 changes: 3 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")

libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.1.0"
11 changes: 11 additions & 0 deletions scalajs-tutorial-fastopt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The Scala.js Tutorial</title>
</head>
<body>
<!-- Include Scala.js compiled code -->
<script type="text/javascript" src="./target/scala-2.13/scala-js-tutorial-fastopt/main.js"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions scalajs-tutorial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>The Scala.js Tutorial</title>
</head>
<body>
<!-- Include Scala.js compiled code -->
<script type="text/javascript" src="./target/scala-2.13/scala-js-tutorial-opt/main.js"></script>
</body>
</html>
33 changes: 33 additions & 0 deletions src/main/scala/tutorial/webapp/TutorialApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tutorial.webapp

import org.scalajs.dom
import org.scalajs.dom.document

object TutorialApp {
def main(args: Array[String]): Unit = {
document.addEventListener("DOMContentLoaded", { (e: dom.Event) =>
setupUI()
})
}

def setupUI(): Unit = {
val button = document.createElement("button")
button.textContent = "Click me!"
button.addEventListener("click", { (e: dom.MouseEvent) =>
addClickedMessage()
})
document.body.appendChild(button)

appendPar(document.body, "Hello World")
}

def appendPar(targetNode: dom.Node, text: String): Unit = {
val parNode = document.createElement("p")
parNode.textContent = text
targetNode.appendChild(parNode)
}

def addClickedMessage(): Unit = {
appendPar(document.body, "You clicked the button!")
}
}
35 changes: 35 additions & 0 deletions src/test/scala/tutorial/webapp/TutorialTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tutorial.webapp

import utest._

import scala.scalajs.js

import org.scalajs.dom
import org.scalajs.dom.document
import org.scalajs.dom.ext._

object TutorialTest extends TestSuite {

// Initialize App
TutorialApp.setupUI()

def tests = Tests {
test("HelloWorld") {
assert(document.querySelectorAll("p").count(_.textContent == "Hello World") == 1)
}

test("ButtonClick") {
def messageCount =
document.querySelectorAll("p").count(_.textContent == "You clicked the button!")

val button = document.querySelector("button").asInstanceOf[dom.html.Button]
assert(button != null && button.textContent == "Click me!")
assert(messageCount == 0)

for (c <- 1 to 5) {
button.click()
assert(messageCount == c)
}
}
}
}