From d6c033d6751829e1db0cdb89dc6331db2dd142da Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Thu, 5 Jul 2012 19:42:10 -0400 Subject: [PATCH] main.coffee/CMS module tested --- cms/static/coffee/spec/helpers.coffee | 9 +++ cms/static/coffee/spec/main_spec.coffee | 89 +++++++++++++++++++++++++ cms/static/coffee/src/main.coffee | 2 +- 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 cms/static/coffee/spec/helpers.coffee create mode 100644 cms/static/coffee/spec/main_spec.coffee diff --git a/cms/static/coffee/spec/helpers.coffee b/cms/static/coffee/spec/helpers.coffee new file mode 100644 index 0000000000..a5e24ddf81 --- /dev/null +++ b/cms/static/coffee/spec/helpers.coffee @@ -0,0 +1,9 @@ +# Stub jQuery.cookie +@stubCookies = + csrftoken: 'stubCSRFToken' + +jQuery.cookie = (key, value) => + if value? + @stubCookies[key] = value + else + @stubCookies[key] diff --git a/cms/static/coffee/spec/main_spec.coffee b/cms/static/coffee/spec/main_spec.coffee new file mode 100644 index 0000000000..006fdaa2ad --- /dev/null +++ b/cms/static/coffee/spec/main_spec.coffee @@ -0,0 +1,89 @@ +describe "CMS", -> + beforeEach -> + CMS.unbind() + + it "should iniitalize Models", -> + expect(CMS.Models).toBeDefined() + + it "should initialize Views", -> + expect(CMS.Views).toBeDefined() + + describe "start", -> + beforeEach -> + spyOn(CMS.Views, 'Course').andReturn(jasmine.createSpyObj("Course", ["render"])) + CMS.start() + + it "create the Course", -> + expect(CMS.Views.Course).toHaveBeenCalled() + expect(CMS.Views.Course().render).toHaveBeenCalled() + + describe "view stack", -> + beforeEach -> + @currentView = jasmine.createSpy("currentView") + CMS.viewStack = [@currentView] + + describe "replaceView", -> + beforeEach -> + @newView = jasmine.createSpy("newView") + CMS.on "content.show", (@expectedView) => + CMS.replaceView(@newView) + + it "replace the views on the viewStack", -> + expect(CMS.viewStack).toEqual([@newView]) + + it "trigger content.show on CMS", -> + expect(@expectedView).toEqual(@newView) + + describe "pushView", -> + beforeEach -> + @newView = jasmine.createSpy("newView") + CMS.on "content.show", (@expectedView) => + CMS.pushView(@newView) + + it "push new view onto viewStack", -> + expect(CMS.viewStack).toEqual([@currentView, @newView]) + + it "trigger content.show on CMS", -> + expect(@expectedView).toEqual(@newView) + + describe "popView", -> + it "remove the current view from the viewStack", -> + CMS.popView() + expect(CMS.viewStack).toEqual([]) + + describe "when there's no view on the viewStack", -> + beforeEach -> + CMS.viewStack = [@currentView] + CMS.on "content.hide", => @eventTriggered = true + CMS.popView() + + it "trigger content.hide on CMS", -> + expect(@eventTriggered).toBeTruthy + + describe "when there's previous view on the viewStack", -> + beforeEach -> + @parentView = jasmine.createSpyObj("parentView", ["delegateEvents"]) + CMS.viewStack = [@parentView, @currentView] + CMS.on "content.show", (@expectedView) => + CMS.popView() + + it "trigger content.show with the previous view on CMS", -> + expect(@expectedView).toEqual @parentView + + it "re-bind events on the view", -> + expect(@parentView.delegateEvents).toHaveBeenCalled() + +describe "main helper", -> + beforeEach -> + @previousAjaxSettings = $.extend(true, {}, $.ajaxSettings) + window.stubCookies["csrftoken"] = "stubCSRFToken" + $(document).ready() + + afterEach -> + $.ajaxSettings = @previousAjaxSettings + + it "turn on Backbone emulateHTTP", -> + expect(Backbone.emulateHTTP).toBeTruthy() + + it "setup AJAX CSRF token", -> + expect($.ajaxSettings.headers['X-CSRFToken']).toEqual("stubCSRFToken") diff --git a/cms/static/coffee/src/main.coffee b/cms/static/coffee/src/main.coffee index 5da9e9dfab..14433e7468 100644 --- a/cms/static/coffee/src/main.coffee +++ b/cms/static/coffee/src/main.coffee @@ -30,6 +30,6 @@ $ -> Backbone.emulateHTTP = true $.ajaxSetup - headers : { 'X-CSRFToken': $.cookie 'csrftoken' } + headers : { 'X-CSRFToken': $.cookie 'csrftoken' } CMS.start()