jam-cloud/spec/javascripts/createSession.spec.js

101 lines
3.6 KiB
JavaScript

(function(context,$) {
describe("CreateSession", function() {
var css;
var ajaxSpy;
var appFake = {
clientId: '12345',
notify: function(){},
ajaxError: function() { console.debug("ajaxError"); }
};
var selectors = {
genres: '#create-session-form select[name="genres"]',
description: '#create-session-form textarea[name="description"]'
};
beforeEach(function() {
// Use the actual screen markup
jasmine.getFixtures().fixturesPath = '/app/views/clients';
loadFixtures('_createSession.html.erb');
spyOn(appFake, 'notify');
css = new context.JK.CreateSessionScreen(appFake);
});
describe("resetForm", function() {
it("description should be empty", function() {
$(selectors.description).val('XYZ');
css.resetForm();
expect($(selectors.description).val()).toEqual('');
});
});
describe("loadGenres", function() {
beforeEach(function() {
spyOn($, "ajax").andCallFake(function(opts) {
opts.success(TestResponses.loadGenres);
});
});
it("should populate genres select", function() {
css.loadGenres();
expect($(selectors.genres + ' option').length).toEqual(2);
});
});
describe("validateForm", function() {
function makeValid() {
var genre = '<option selected="selected" value="1">1</option>';
$(selectors.genres).html(genre);
$(selectors.description).val('XYZ');
}
beforeEach(function() {
makeValid();
});
it("valid form", function() {
var errs = css.validateForm();
expect(errs).toBeNull();
});
it("should fail with > 3 genres", function() {
var htm = '<option selected="selected" value="1">1</option>' +
'<option selected="selected" value="2">2</option>' +
'<option selected="selected" value="3">3</option>' +
'<option selected="selected" value="4">4</option>';
$(selectors.genres).html(htm);
var errs = css.validateForm();
// Verify that we have an error.
expect(errs).toBeTruthy();
// Verify that the error is a two-part list
expect(errs[0].length).toEqual(2);
// Verify that the first part is a selector for the problem.
expect(errs[0][0]).toEqual('select[name="genres"]');
});
it("should fail with 0 genres", function() {
$(selectors.genres).html('');
var errs = css.validateForm();
// Verify that we have an error.
expect(errs).toBeTruthy();
// Verify that the first part is a selector for the problem.
expect(errs[0][0]).toEqual('select[name="genres"]');
});
it("should fail with empty description", function() {
$(selectors.description).val('');
var errs = css.validateForm();
// Verify that we have an error.
expect(errs).toBeTruthy();
// Verify that the first part is a selector for the problem.
expect(errs[0][0]).toEqual('textarea[name="description"]');
});
});
});
})(window, jQuery);