Skip to content

jQuery wrapper tests - this closes #2 #4

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

Merged
merged 1 commit into from
Feb 8, 2016
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ scala:
- 2.11.7

script:
- sbt ++$TRAVIS_SCALA_VERSION compile
- sbt ++$TRAVIS_SCALA_VERSION test
11 changes: 9 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ scalacOptions in ThisBuild ++= Seq(
"-Xlint:_,-missing-interpolator,-adapted-args"
)

libraryDependencies +=
"org.scala-js" %%% "scalajs-dom" % "0.8.2"
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-dom" % "0.8.2",
"org.scalatest" %%% "scalatest" % "3.0.0-M15" % Test,
"com.lihaoyi" %%% "scalatags" % "0.5.4" % Test
)

jsDependencies +=
"org.webjars" % "jquery" % "2.2.0" / "2.2.0/jquery.js" minified "2.2.0/jquery.min.js"

requiresDOM in Test := true
persistLauncher in Test := false
scalaJSUseRhino in Test := false

lazy val root = project.in(file("."))
.enablePlugins(ScalaJSPlugin)
93 changes: 42 additions & 51 deletions src/main/scala/io/udash/wrappers/jquery/JQuery.scala

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/main/scala/io/udash/wrappers/jquery/JQueryStatic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,16 @@ object JQueryStatic {

/** Return a collection of matched elements either found in the DOM based on passed argument(s) or created by passing an HTML string. <br/>
* See: <a href="http://api.jquery.com/jQuery/">jQuery Docs</a> */
def apply(elementArray: Seq[Element]): JQuery = jQueryStatic(elementArray.toJSArray)
def apply(elementArray: Element*): JQuery = jQueryStatic(elementArray.toJSArray)

/** Binds a function to be executed when the DOM has finished loading. <br/>
* See: <a href="http://api.jquery.com/jQuery/#jQuery-callback">jQuery Docs</a> */
def apply(callback: () => js.Any): JQuery = jQueryStatic(callback)

/** Binds a function to be executed when the DOM has finished loading. <br/>
* See: <a href="http://api.jquery.com/jQuery/#jQuery-callback">jQuery Docs</a> */
def apply(option: Option[Any]): JQuery = option match {
case Some(element) => jQueryStatic.apply(element.asInstanceOf[js.Any])
def apply(option: Option[js.Any]): JQuery = option match {
case Some(element) => jQueryStatic.apply(element)
case None => jQueryStatic.apply()
}

Expand Down
241 changes: 241 additions & 0 deletions src/test/scala/io/udash/wrappers/jquery_test/DomManipulationTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package io.udash.wrappers.jquery_test

import org.scalatest.{Matchers, WordSpec}

class DomManipulationTest extends WordSpec with Matchers {
import io.udash.wrappers.jquery._
import scalatags.JsDom.all._

"jQuery" should {
"set text content of DOM element" in {
val text = "test 123"
val dom = div().render

jQ(dom).text() should be("")
dom.textContent should be("")

jQ(dom).text(text)

jQ(dom).text() should be(text)
dom.textContent should be(text)
}

"set content of DOM element" in {
val text = "test 123"
val dom = div().render
val content = span(ul(li(text))).render

jQ(dom).html() should be("")
dom.textContent should be("")

jQ(dom).html(content)

jQ(dom).html() should be(s"<span><ul><li>$text</li></ul></span>")
jQ(dom).text() should be(text)
dom.textContent should be(text)
}

"set DOM element attribute" in {
val attrName = "attr"
val attrValue = "val"
val dom = div().render

jQ(dom).attr(attrName) should be(None)

jQ(dom).attr(attrName, attrValue)

jQ(dom).attr(attrName) should be(Some(attrValue))
dom.getAttribute(attrName) should be(attrValue)
}

"manage DOM element CSS classes" in {
val c1 = "c1"
val c2 = "c2"
val dom = div().render

jQ(dom).addClass(c1)
jQ(dom).addClass(c2)

dom.className.split(" ") should contain(c1)
dom.className.split(" ") should contain(c2)

jQ(dom).removeClass(c2)

dom.className.split(" ") should contain(c1)
dom.className.split(" ") shouldNot contain(c2)

jQ(dom).toggleClass(c1)
jQ(dom).toggleClass(c2)

dom.className.split(" ") shouldNot contain(c1)
dom.className.split(" ") should contain(c2)

jQ(dom).toggleClass(c1)
jQ(dom).toggleClass(c2)

dom.className.split(" ") should contain(c1)
dom.className.split(" ") shouldNot contain(c2)
}

"show/hide DOM elements" in {
val dom = div(
h1("a"),
span("b")
).render

val h = jQ(dom).find("h1")
val s = jQ(dom).find("span")

h.hide()
h.css("display") should be("none")
s.css("display") shouldNot be("none")

s.hide()
h.css("display") should be("none")
s.css("display") should be("none")

h.show()
s.show()
h.css("display") shouldNot be("none")
s.css("display") shouldNot be("none")
}

"remove DOM elements" in {
val dom = div(
h1("a"),
span(cls :="c1")("b"),
h1("a"),
span("c"),
h1("a"),
span(cls :="c1")("d"),
h1("a"),
span("e", span("f"), "g")
).render

val h = jQ(dom).find("h1")
val s = jQ(dom).find("span")
val c = jQ(dom).find(".c1")

dom.childElementCount should be(8)
dom.textContent should be("abacadaefg")

h.remove()

dom.childElementCount should be(4)
dom.textContent should be("bcdefg")

c.remove()

dom.childElementCount should be(2)
dom.textContent should be("cefg")

s.remove()

dom.childElementCount should be(0)
dom.textContent should be("")
}

"remove childrens of DOM elements" in {
val dom = div(
h1("a"),
span(cls :="c1")("b"),
h1("a"),
span("c"),
h1("a"),
span(cls :="c1")("d"),
h1("a"),
span("e", span("f"), "g")
).render

val h = jQ(dom).find("h1")
val s = jQ(dom).find("span")
val c = jQ(dom).find(".c1")

dom.childElementCount should be(8)
dom.textContent should be("abacadaefg")

h.empty()

dom.childElementCount should be(8)
dom.textContent should be("bcdefg")

c.empty()

dom.childElementCount should be(8)
dom.textContent should be("cefg")

s.empty()

dom.childElementCount should be(8)
dom.textContent should be("")
}

"append/prepend DOM elements" in {
val dom = div(
span(), span(), span(), span()
).render

val s = jQ(dom).find("span")

dom.childElementCount should be(4)
dom.textContent should be("")

s.append("a")

dom.childElementCount should be(4)
dom.textContent should be("aaaa")

s.prepend("b")

dom.childElementCount should be(4)
dom.textContent should be("babababa")

s.filter(":nth-child(2n)").append("c")

dom.childElementCount should be(4)
dom.textContent should be("babacbabac")

s.filter(":nth-child(2n+1)").prepend("d")

dom.childElementCount should be(4)
dom.textContent should be("dbabacdbabac")
}

"append/prepend DOM element into another element" in {
val dom = div().render

jQ(span("a").render).appendTo(dom)

dom.childElementCount should be(1)
dom.textContent should be("a")

jQ(span("b").render).appendTo(jQ(dom))

dom.childElementCount should be(2)
dom.textContent should be("ab")

jQ(span("x").render).prependTo(jQ(dom))

dom.childElementCount should be(3)
dom.textContent should be("xab")

jQ(span("y").render).prependTo(dom)

dom.childElementCount should be(4)
dom.textContent should be("yxab")
}

"manage input values" in {
val dom = input().render

jQ(dom).value("abc")
jQ(dom).value() should be("abc")
dom.value should be("abc")

jQ(dom).value("cba")
jQ(dom).value() should be("cba")
dom.value should be("cba")
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import org.scalajs.dom.Element
import org.scalajs.dom.html.Input
import org.scalatest.{Matchers, WordSpec}

class EventsHandlingTest extends WordSpec with Matchers {
import io.udash.wrappers.jquery._

import scalatags.JsDom.all._

class C(i: Int)

"jQuery" should {
"handle JS events" in {
val i = jQ(input().render)

var event: JQueryEvent = null

val callback: (Element, JQueryEvent) => Unit =
(el: Element, ev: JQueryEvent) =>
event = ev

i.on("change", callback)

i.trigger("change")
event shouldNot be(null)

i.off("change", callback)

event = null
i.trigger("change")
event should be(null)
}

"handle JS events (inlined)" in {
val i: Input = input().render
var event: JQueryEvent = null

val callback: (Element, JQueryEvent) => Unit =
(el: Element, ev: JQueryEvent) =>
event = ev

jQ(i).on("change", callback)

jQ(i).trigger("change")
event shouldNot be(null)

jQ(i).off("change", callback)

event = null
jQ(i).trigger("change")
event should be(null)
}

"handle JS events (one)" in {
val i: Input = input().render
var event: JQueryEvent = null

val callback: (Element, JQueryEvent) => Unit =
(el: Element, ev: JQueryEvent) =>
event = ev

jQ(i).one("change", callback)

jQ(i).trigger("change")
event shouldNot be(null)

event = null
jQ(i).trigger("change")
event should be(null)
}
}

}
Loading