Of the material I read on Jasmine, they test only a .js
file. But what if the test is on a webpage, say, with the code:
try.html:
<input id="the-input"></input>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$("#the-input").focus(function() {
$(this).css("background", "#ff9");
});
$("#the-input").blur(function() {
$(this).css("background", "#cff");
});
</script>
which is just to say, if the user clicks on (or tab to) the input box, make the box's background yellow, and when focus out, then make it light blue. In such case, how do we test the above behavior in the .html file? (where should this html file be -- can it be any where outside of the Jasmine lib file and SpecRunner.html, and how does the SpecRunner.html include it somehow to test the page?
A jsfiddle is in: http://jsfiddle.net/MspUF/
Update: it is fine to separate the HTML content and the JavaScript code, but still, what ARE THE SPECIFICS as to be able to test it.
describe('Working with transaction details component', function() { beforeEach(function() { spyOnEvent(document, 'startViewDetail'); $(document). trigger('startViewDetail', mockDataObject. transactionId); }); it('test startViewTransaction', function() { spyOn(document, 'startViewTransaction').
Jasmine is a very popular JavaScript behavior-driven development (In BDD, you write tests before writing actual code) framework for unit testing JavaScript applications. It provides utilities that can be used to run automated tests for both synchronous and asynchronous code.
Jasmine is an open-source testing framework for JavaScript. It aims to run on any JavaScript-enabled platform, to not intrude on the application nor the IDE, and to have easy-to-read syntax. It is heavily influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.
After some research, I found a simple way to test JavaScript together with HTML, CSS, and JSON data. It is Jasmine-jQuery and those are called HTML fixtures, etc. So not a full, self-contained page, but possibly enough to tell your widgets works fine if the mouse is over it, or what should happen if a user clicks on something. (by using jQuery .mouseover()
or .click()
, etc.)
It is generally, best practice to move scripts to external js
files in web browsers as well.
You can move your code to a .js
file and then include it in your HTML by doing:
<script src='myfile.js'>
Which will include it, this will allow you to easily use Jasmine to test the file. You can mock your DOM for unit testing outside the DOM.
If you need general testing instead of unit testing, I suggest something like PhantomJS
that would actually simulate a browser and enable you to test the complete user experience. (Other good alternatives include Selenium which I had success with)
Also, here is a nice article about testing jQuery with QUnit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With