How to write test-case for Scala.js application

Knoldus Inc.
Knoldus - Technical Insights
1 min readFeb 29, 2016

Few months ago, I created a web application ‘Weather Report’, based on Scala.js. In this blog, I have explained how we can test our Scala.js application.
Since I am still exploring it, so I took help from scalajs-tutorial. I have used µTest as testing library.

[code]
// uTest settings
libraryDependencies += “com.lihaoyi” %%% “utest” % “0.3.0” % “test”
testFrameworks += new TestFramework(“utest.runner.Framework”)
[/code]

Here is example of test-cases:

[code language=”java”]
def tests = TestSuite {
“Page should contain search text box with button.” — {
assert(jQuery(“#name”).value() == “Delhi”)
val button = jQuery(“#submit”)
assert(button.length == 1)
}
“Page should display weather report for default city.” — {
val button = jQuery(“#submit”)
assert(jQuery(“#cityName:contains(‘Delhi,IN’)”).length == 0)
button.click()
assert(jQuery(“#cityName:contains(‘Delhi,IN’)”).length == 1)
assert(jQuery(“#geocoords:contains(‘[77.22, 28.67]’)”).length == 1)
}
“Page should clean before search weather report for a new city.” — {
jQuery(“#name”).value(“Newyork”)
val button = jQuery(“#submit”)
assert(jQuery(“#cityName:contains(‘New York,US’)”).length ==0)
button.click()
assert(jQuery(“#cityName:contains(‘Delhi,IN’)”).length ==0)
assert(jQuery(“#cityName:contains(‘New York,US’)”).length ==1)
assert(jQuery(“#geocoords”).value() != “[-74.01, 40.71]”)
}
}

[/code]

In the above example, we can see verified triggered event on button click. This is a sample test-case. We can add more scenario depends on application. You can find complete working and tested code here:- ScalaJs_Weather_Report

Now pull the code and run `sbt test`.

Screenshot from 2016-02-29 14-01-01

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com