(function(context,$) { describe("CreateSession", function() { var css; var ajaxSpy; var appFake = { clientId: '12345', notify: function(){}, ajaxError: function() { console.debug("ajaxError"); } }; var selectors = { form: '#create-session-form', genres: '#create-session-form select[name="genres"]', description: '#create-session-form textarea[name="description"]' }; function makeValid() { var genre = ''; $(selectors.genres).html(genre); $(selectors.description).val('XYZ'); } 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("submitForm", function() { var fakeEvt = { preventDefault: $.noop }; var passedData = {}; beforeEach(function() { makeValid(); spyOn($, "ajax").andCallFake(function(opts) { opts.success(TestResponses.sessionPost); }); css.submitForm.call($(selectors.form), fakeEvt); passedData = JSON.parse($.ajax.mostRecentCall.args[0].data); }); it("should pass client_id", function() { expect(passedData.client_id).toEqual("12345"); }); it("should pass genres as non-empty list", function() { expect("genres" in passedData).toBeTruthy(); var isArray = $.isArray(passedData.genres); expect(isArray).toBeTruthy(); expect(passedData.genres.length).toBeGreaterThan(0); }); it("should pass tracks as non-empty list", function() { expect("tracks" in passedData).toBeTruthy(); var isArray = $.isArray(passedData.tracks); expect(isArray).toBeTruthy(); expect(passedData.tracks.length).toBeGreaterThan(0); }); it("should pass musician_access as boolean", function() { expect("musician_access" in passedData).toBeTruthy(); expect(typeof(passedData.musician_access)).toEqual("boolean"); }); it("should pass approval_required as boolean", function() { expect("approval_required" in passedData).toBeTruthy(); expect(typeof(passedData.approval_required)).toEqual("boolean"); }); it("should pass fan_access as boolean", function() { expect("fan_access" in passedData).toBeTruthy(); expect(typeof(passedData.fan_access)).toEqual("boolean"); }); it("should pass fan_chat as boolean", function() { expect("fan_chat" in passedData).toBeTruthy(); expect(typeof(passedData.fan_chat)).toEqual("boolean"); }); }); describe("validateForm", function() { beforeEach(function() { makeValid(); }); it("valid form", function() { var errs = css.validateForm(); expect(errs).toBeNull(); }); it("should fail with > 3 genres", function() { var htm = '' + '' + '' + ''; $(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);