The document discusses JavaScript testing for web applications using Jasmine. It introduces Jasmine as a testing framework that will be familiar to Rubyists. It then provides examples of testing asynchronous functions, stubbing network requests, testing events, and setting up tests to run across multiple browsers.
5. var set_result_html = function(data){
$('.result').html(data);
};
var set_notification_text = function(text){
$('.notification').text(text);
};
var my_cool_loader = function(post_data){
$.ajax({
type: 'POST',
url: 'load_some_data.html',
data: post_data,
success: function(data){
set_result_html(data);
},
error: function(){
set_notification_text('Something bad happened!');
}
});
};
6. var set_result_html = function(data){
$('.result').html(data);
};
var set_notification_text = function(text){
$('.notification').text(text);
};
var my_cool_loader = function(post_data){
$.ajax({
type: 'POST',
url: 'load_some_data.html',
data: post_data,
success: function(data){
set_result_html(data);
},
error: function(){
set_notification_text('Something bad happened!');
}
});
};
7. describe("calling a function", function() {
beforeEach(function() {
set_result_html('<p>Hello World</p>');
});
it("should set the text of the .result element", function() {
expect($('.result').html()).toEqual('<p>Hello World</p>');
});
});
22. describe("when successful", function() {
beforeEach(function() {
Mooch.stub_request('POST', 'load_some_data.html').
returns({ body: '<p>loaded html</p>' });
// create a new keyup event
var e = jQuery.Event("keyup");
// set the key that was pressed to the enter key
e.which = 13;
// trigger the event on the textarea element
$("#text-input").trigger(e);
});
it("should insert the received html into the result element", function() {
expect($('.result').html()).toEqual('<p>loaded html</p>');
});
});
23. describe("when successful", function() {
beforeEach(function() {
Mooch.stub_request('POST', 'load_some_data.html').
returns({ body: '<p>loaded html</p>' });
// create a new keyup event
var e = jQuery.Event("keyup");
// set the key that was pressed to the enter key
e.which = 13;
// trigger the event on the textarea element
$("#text-input").trigger(e);
});
it("should insert the received html into the result element", function() {
expect($('.result').html()).toEqual('<p>loaded html</p>');
});
});
24. describe("when successful", function() {
beforeEach(function() {
Mooch.stub_request('POST', 'load_some_data.html').
returns({ body: '<p>loaded html</p>' });
// create a new keyup event
var e = jQuery.Event("keyup");
// set the key that was pressed to the enter key
e.which = 13;
// trigger the event on the textarea element
$("#text-input").trigger(e);
});
it("should insert the received html into the result element", function() {
expect($('.result').html()).toEqual('<p>loaded html</p>');
});
});
25. describe("when successful", function() {
beforeEach(function() {
Mooch.stub_request('POST', 'load_some_data.html').
returns({ body: '<p>loaded html</p>' });
// create a new keyup event
var e = jQuery.Event("keyup");
// set the key that was pressed to the enter key
e.which = 13;
// trigger the event on the textarea element
$("#text-input").trigger(e);
});
it("should insert the received html into the result element", function() {
expect($('.result').html()).toEqual('<p>loaded html</p>');
});
});
26. Whats different with JS
testing?
• need to test in all your target browsers
• test suites don’t clean up (much) for you
• event driven programming can be hard to
test