Skip to content

Commit b84ec18

Browse files
committed
Merge pull request #4 from UdashFramework/tests
jQuery wrapper tests - this closes #2
2 parents 576e30f + 660429b commit b84ec18

File tree

9 files changed

+643
-57
lines changed

9 files changed

+643
-57
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ scala:
44
- 2.11.7
55

66
script:
7-
- sbt ++$TRAVIS_SCALA_VERSION compile
7+
- sbt ++$TRAVIS_SCALA_VERSION test

build.sbt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ scalacOptions in ThisBuild ++= Seq(
1515
"-Xlint:_,-missing-interpolator,-adapted-args"
1616
)
1717

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

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

27+
requiresDOM in Test := true
28+
persistLauncher in Test := false
29+
scalaJSUseRhino in Test := false
30+
2431
lazy val root = project.in(file("."))
2532
.enablePlugins(ScalaJSPlugin)

src/main/scala/io/udash/wrappers/jquery/JQuery.scala

Lines changed: 42 additions & 51 deletions
Large diffs are not rendered by default.

src/main/scala/io/udash/wrappers/jquery/JQueryStatic.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,16 @@ object JQueryStatic {
188188

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

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

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

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
package io.udash.wrappers.jquery_test
2+
3+
import org.scalatest.{Matchers, WordSpec}
4+
5+
class DomManipulationTest extends WordSpec with Matchers {
6+
import io.udash.wrappers.jquery._
7+
import scalatags.JsDom.all._
8+
9+
"jQuery" should {
10+
"set text content of DOM element" in {
11+
val text = "test 123"
12+
val dom = div().render
13+
14+
jQ(dom).text() should be("")
15+
dom.textContent should be("")
16+
17+
jQ(dom).text(text)
18+
19+
jQ(dom).text() should be(text)
20+
dom.textContent should be(text)
21+
}
22+
23+
"set content of DOM element" in {
24+
val text = "test 123"
25+
val dom = div().render
26+
val content = span(ul(li(text))).render
27+
28+
jQ(dom).html() should be("")
29+
dom.textContent should be("")
30+
31+
jQ(dom).html(content)
32+
33+
jQ(dom).html() should be(s"<span><ul><li>$text</li></ul></span>")
34+
jQ(dom).text() should be(text)
35+
dom.textContent should be(text)
36+
}
37+
38+
"set DOM element attribute" in {
39+
val attrName = "attr"
40+
val attrValue = "val"
41+
val dom = div().render
42+
43+
jQ(dom).attr(attrName) should be(None)
44+
45+
jQ(dom).attr(attrName, attrValue)
46+
47+
jQ(dom).attr(attrName) should be(Some(attrValue))
48+
dom.getAttribute(attrName) should be(attrValue)
49+
}
50+
51+
"manage DOM element CSS classes" in {
52+
val c1 = "c1"
53+
val c2 = "c2"
54+
val dom = div().render
55+
56+
jQ(dom).addClass(c1)
57+
jQ(dom).addClass(c2)
58+
59+
dom.className.split(" ") should contain(c1)
60+
dom.className.split(" ") should contain(c2)
61+
62+
jQ(dom).removeClass(c2)
63+
64+
dom.className.split(" ") should contain(c1)
65+
dom.className.split(" ") shouldNot contain(c2)
66+
67+
jQ(dom).toggleClass(c1)
68+
jQ(dom).toggleClass(c2)
69+
70+
dom.className.split(" ") shouldNot contain(c1)
71+
dom.className.split(" ") should contain(c2)
72+
73+
jQ(dom).toggleClass(c1)
74+
jQ(dom).toggleClass(c2)
75+
76+
dom.className.split(" ") should contain(c1)
77+
dom.className.split(" ") shouldNot contain(c2)
78+
}
79+
80+
"show/hide DOM elements" in {
81+
val dom = div(
82+
h1("a"),
83+
span("b")
84+
).render
85+
86+
val h = jQ(dom).find("h1")
87+
val s = jQ(dom).find("span")
88+
89+
h.hide()
90+
h.css("display") should be("none")
91+
s.css("display") shouldNot be("none")
92+
93+
s.hide()
94+
h.css("display") should be("none")
95+
s.css("display") should be("none")
96+
97+
h.show()
98+
s.show()
99+
h.css("display") shouldNot be("none")
100+
s.css("display") shouldNot be("none")
101+
}
102+
103+
"remove DOM elements" in {
104+
val dom = div(
105+
h1("a"),
106+
span(cls :="c1")("b"),
107+
h1("a"),
108+
span("c"),
109+
h1("a"),
110+
span(cls :="c1")("d"),
111+
h1("a"),
112+
span("e", span("f"), "g")
113+
).render
114+
115+
val h = jQ(dom).find("h1")
116+
val s = jQ(dom).find("span")
117+
val c = jQ(dom).find(".c1")
118+
119+
dom.childElementCount should be(8)
120+
dom.textContent should be("abacadaefg")
121+
122+
h.remove()
123+
124+
dom.childElementCount should be(4)
125+
dom.textContent should be("bcdefg")
126+
127+
c.remove()
128+
129+
dom.childElementCount should be(2)
130+
dom.textContent should be("cefg")
131+
132+
s.remove()
133+
134+
dom.childElementCount should be(0)
135+
dom.textContent should be("")
136+
}
137+
138+
"remove childrens of DOM elements" in {
139+
val dom = div(
140+
h1("a"),
141+
span(cls :="c1")("b"),
142+
h1("a"),
143+
span("c"),
144+
h1("a"),
145+
span(cls :="c1")("d"),
146+
h1("a"),
147+
span("e", span("f"), "g")
148+
).render
149+
150+
val h = jQ(dom).find("h1")
151+
val s = jQ(dom).find("span")
152+
val c = jQ(dom).find(".c1")
153+
154+
dom.childElementCount should be(8)
155+
dom.textContent should be("abacadaefg")
156+
157+
h.empty()
158+
159+
dom.childElementCount should be(8)
160+
dom.textContent should be("bcdefg")
161+
162+
c.empty()
163+
164+
dom.childElementCount should be(8)
165+
dom.textContent should be("cefg")
166+
167+
s.empty()
168+
169+
dom.childElementCount should be(8)
170+
dom.textContent should be("")
171+
}
172+
173+
"append/prepend DOM elements" in {
174+
val dom = div(
175+
span(), span(), span(), span()
176+
).render
177+
178+
val s = jQ(dom).find("span")
179+
180+
dom.childElementCount should be(4)
181+
dom.textContent should be("")
182+
183+
s.append("a")
184+
185+
dom.childElementCount should be(4)
186+
dom.textContent should be("aaaa")
187+
188+
s.prepend("b")
189+
190+
dom.childElementCount should be(4)
191+
dom.textContent should be("babababa")
192+
193+
s.filter(":nth-child(2n)").append("c")
194+
195+
dom.childElementCount should be(4)
196+
dom.textContent should be("babacbabac")
197+
198+
s.filter(":nth-child(2n+1)").prepend("d")
199+
200+
dom.childElementCount should be(4)
201+
dom.textContent should be("dbabacdbabac")
202+
}
203+
204+
"append/prepend DOM element into another element" in {
205+
val dom = div().render
206+
207+
jQ(span("a").render).appendTo(dom)
208+
209+
dom.childElementCount should be(1)
210+
dom.textContent should be("a")
211+
212+
jQ(span("b").render).appendTo(jQ(dom))
213+
214+
dom.childElementCount should be(2)
215+
dom.textContent should be("ab")
216+
217+
jQ(span("x").render).prependTo(jQ(dom))
218+
219+
dom.childElementCount should be(3)
220+
dom.textContent should be("xab")
221+
222+
jQ(span("y").render).prependTo(dom)
223+
224+
dom.childElementCount should be(4)
225+
dom.textContent should be("yxab")
226+
}
227+
228+
"manage input values" in {
229+
val dom = input().render
230+
231+
jQ(dom).value("abc")
232+
jQ(dom).value() should be("abc")
233+
dom.value should be("abc")
234+
235+
jQ(dom).value("cba")
236+
jQ(dom).value() should be("cba")
237+
dom.value should be("cba")
238+
}
239+
}
240+
241+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import org.scalajs.dom.Element
2+
import org.scalajs.dom.html.Input
3+
import org.scalatest.{Matchers, WordSpec}
4+
5+
class EventsHandlingTest extends WordSpec with Matchers {
6+
import io.udash.wrappers.jquery._
7+
8+
import scalatags.JsDom.all._
9+
10+
class C(i: Int)
11+
12+
"jQuery" should {
13+
"handle JS events" in {
14+
val i = jQ(input().render)
15+
16+
var event: JQueryEvent = null
17+
18+
val callback: (Element, JQueryEvent) => Unit =
19+
(el: Element, ev: JQueryEvent) =>
20+
event = ev
21+
22+
i.on("change", callback)
23+
24+
i.trigger("change")
25+
event shouldNot be(null)
26+
27+
i.off("change", callback)
28+
29+
event = null
30+
i.trigger("change")
31+
event should be(null)
32+
}
33+
34+
"handle JS events (inlined)" in {
35+
val i: Input = input().render
36+
var event: JQueryEvent = null
37+
38+
val callback: (Element, JQueryEvent) => Unit =
39+
(el: Element, ev: JQueryEvent) =>
40+
event = ev
41+
42+
jQ(i).on("change", callback)
43+
44+
jQ(i).trigger("change")
45+
event shouldNot be(null)
46+
47+
jQ(i).off("change", callback)
48+
49+
event = null
50+
jQ(i).trigger("change")
51+
event should be(null)
52+
}
53+
54+
"handle JS events (one)" in {
55+
val i: Input = input().render
56+
var event: JQueryEvent = null
57+
58+
val callback: (Element, JQueryEvent) => Unit =
59+
(el: Element, ev: JQueryEvent) =>
60+
event = ev
61+
62+
jQ(i).one("change", callback)
63+
64+
jQ(i).trigger("change")
65+
event shouldNot be(null)
66+
67+
event = null
68+
jQ(i).trigger("change")
69+
event should be(null)
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)