

How to write test-case for Scala.js application
source link: https://blog.knoldus.com/how-to-write-test-case-for-scala-js-application/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.
xxxxxxxxxx
// uTest settings
libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" % "test"
testFrameworks += new TestFramework("utest.runner.Framework")
Here is example of test-cases:
xxxxxxxxxx
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`.
Written by Ayush Mishra
Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a 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.
Post navigation
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK