How to write test-case for Scala.js application


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.

// uTest settings
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" % "test"
testFrameworks += new TestFramework("utest.runner.Framework")

Here is example of test-cases:

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]")
}
}

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

Advertisements

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in JavaScript, Scala, Test, Web, web application and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s