spec/javascripts/mercury/modal_spec.js.coffee in mercury-rails-0.2.3 vs spec/javascripts/mercury/modal_spec.js.coffee in mercury-rails-0.3.1

- old
+ new

@@ -1,16 +1,18 @@ -require '/assets/mercury.js' - describe "Mercury.modal", -> template 'mercury/modal.html' beforeEach -> $.fx.off = true Mercury.displayRect = {fullHeight: 200} + Mercury.determinedLocale = + top: {'hello world!': 'bork! bork!'} + sub: {'foo': 'Bork!'} afterEach -> + Mercury.config.localization.enabled = false Mercury.modal.initialized = false Mercury.modal.visible = false $(window).unbind('mercury:refresh') $(window).unbind('mercury:resize') $(document).unbind('keydown') @@ -117,16 +119,11 @@ Mercury.modal.options = {appendTo: $('#modal_container')} Mercury.modal.build() expect($('#modal_container .mercury-modal').length).toEqual(1) expect($('#modal_container .mercury-modal-overlay').length).toEqual(1) - it "updates the title to reflect what was passed in the options", -> - Mercury.modal.options.title = 'title' - Mercury.modal.build() - expect($('#test .mercury-modal-title span').html()).toEqual('title') - describe "observed events", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) Mercury.modal('/foo', {appendTo: $('#test')}) @@ -144,17 +141,25 @@ it "calls position", -> spy = spyOn(Mercury.modal, 'position').andCallFake(=>) Mercury.trigger('resize') expect(spy.callCount).toEqual(1) - describe "clicking on the overlay", -> + describe "clicking on the overlay (options.allowHideUsingOverlay = true)", -> it "calls hide", -> + Mercury.modal.options.allowHideUsingOverlay = true spy = spyOn(Mercury.modal, 'hide').andCallFake(=>) jasmine.simulate.click($('.mercury-modal-overlay').get(0)) expect(spy.callCount).toEqual(1) + describe "clicking on the overlay (options.allowHideUsingOverlay = false)", -> + + it "doesn't call hide", -> + spy = spyOn(Mercury.modal, 'hide').andCallFake(=>) + jasmine.simulate.click($('.mercury-modal-overlay').get(0)) + expect(spy.callCount).toEqual(0) + describe "clicking on the close button", -> it "calls hide", -> spy = spyOn(Mercury.modal, 'hide').andCallFake(=>) jasmine.simulate.click($('.mercury-modal-title a').get(0)) @@ -168,19 +173,30 @@ it "calls hide", -> spy = spyOn(Mercury.modal, 'hide').andCallFake(=>) jasmine.simulate.keydown(document, {keyCode: 27}) expect(spy.callCount).toEqual(1) + describe "ajax:beforeSend", -> + it "sets a success that will load the contents of the response", -> + options = {} + spy = spyOn(Mercury.modal, 'loadContent').andCallFake(=>) + Mercury.modal.element.trigger('ajax:beforeSend', [null, options]) + expect(options.success).toBeDefined() + options.success('new content') + expect(spy.callCount).toEqual(1) + expect(spy.argsForCall[0]).toEqual(['new content']) + + describe "#appear", -> beforeEach -> Mercury.modal.visible = true spyOn(Mercury.modal, 'update').andCallFake(=>) @loadSpy = spyOn(Mercury.modal, 'load').andCallFake(=>) @positionSpy = spyOn(Mercury.modal, 'position').andCallFake(=>) - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test')}) + Mercury.modal('/blank.html', {appendTo: $('#test')}) it "calls position", -> Mercury.modal.appear() expect(@positionSpy.callCount).toEqual(1) @@ -221,11 +237,11 @@ describe "#resize", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test')}) + Mercury.modal('/blank.html', {appendTo: $('#test')}) Mercury.modal.contentPane = $() it "will keep the content element visible if asked to do so", -> $('.mercury-modal-content').css('visibility', 'visible') Mercury.modal.resize(true) @@ -275,11 +291,11 @@ describe "#load", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) @ajaxSpy = spyOn($, 'ajax') - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test')}) + Mercury.modal('/blank.html', {appendTo: $('#test')}) it "does nothing if there's no url", -> Mercury.modal.url = null $('.mercury-modal').removeClass('loading') Mercury.modal.load() @@ -295,12 +311,12 @@ expect(spy.callCount).toEqual(1) describe "on a preloaded view", -> beforeEach -> - @setTimeoutSpy = spyOn(window, 'setTimeout').andCallFake((callback) => callback()) - Mercury.preloadedViews = {'/evergreen/responses/blank.html': 'this is the preloaded content'} + @setTimeoutSpy = spyOn(window, 'setTimeout').andCallFake((timeout, callback) => callback()) + Mercury.preloadedViews = {'/blank.html': 'this is the preloaded content'} afterEach -> Mercury.preloadedViews = {} it "calls loadContent with the content in the preloaded view", -> @@ -311,12 +327,14 @@ describe "when not a preloaded view", -> it "makes an ajax request", -> @ajaxSpy.andCallFake(=>) + spyOn(Mercury, 'ajaxHeaders').andCallFake(=> {'X-CSRFToken': 'f00'}) Mercury.modal.load() expect(@ajaxSpy.callCount).toEqual(1) + expect(@ajaxSpy.argsForCall[0][1]['headers']).toEqual({'X-CSRFToken': 'f00'}) describe "on success", -> beforeEach -> @ajaxSpy.andCallFake((url, options) => options.success('return value')) @@ -341,19 +359,19 @@ it "alerts an error message", -> spyOn(Mercury.modal, 'hide').andCallFake(=>) spy = spyOn(window, 'alert').andCallFake(=>) Mercury.modal.load() expect(spy.callCount).toEqual(1) - expect(spy.argsForCall[0]).toEqual(['Mercury was unable to load /evergreen/responses/blank.html for the modal.']) + expect(spy.argsForCall[0]).toEqual(['Mercury was unable to load /blank.html for the modal.']) describe "#loadContent", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) @resizeSpy = spyOn(Mercury.modal, 'resize').andCallFake(=>) - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test'), title: 'title'}) + Mercury.modal('/blank.html', {appendTo: $('#test'), title: 'title'}) it "accepts options and sets them to the instance options", -> Mercury.modal.loadContent('content', {title: 'title'}) expect(Mercury.modal.options).toEqual({title: 'title'}) @@ -376,23 +394,23 @@ $('.mercury-modal').addClass('loading') Mercury.modal.loadContent('content') expect($('.mercury-modal').hasClass('loading')).toEqual(false) it "sets the content elements html to whatever was passed", -> - Mercury.modal.loadContent('content') - expect($('.mercury-modal-content').html()).toEqual('content') + Mercury.modal.loadContent('<span>content</span>') + expect($('.mercury-modal-content').html()).toEqual('<span>content</span>') it "hides the contentElement", -> $('.mercury-modal-content').css('display', 'block') Mercury.modal.loadContent('content') expect($('.mercury-modal-content').css('display')).toEqual('none') expect($('.mercury-modal-content').css('visibility')).toEqual('hidden') it "finds the content panes and control elements in case they were added with the content", -> - Mercury.modal.loadContent('<div class="mercury-modal-pane-container"></div><div class="mercury-modal-controls"></div>') - expect(Mercury.modal.contentPane.get(0)).toEqual($('#test .mercury-modal-pane-container').get(0)) - expect(Mercury.modal.contentControl.get(0)).toEqual($('#test .mercury-modal-controls').get(0)) + Mercury.modal.loadContent('<div class="mercury-display-pane-container"></div><div class="mercury-display-controls"></div>') + expect(Mercury.modal.contentPane.get(0)).toEqual($('#test .mercury-display-pane-container').get(0)) + expect(Mercury.modal.contentControl.get(0)).toEqual($('#test .mercury-display-controls').get(0)) it "calls an afterLoad callback (if provided in options)", -> callCount = 0 Mercury.modal.loadContent('content', {afterLoad: => callCount += 1}) expect(callCount).toEqual(1) @@ -401,32 +419,44 @@ callCount = 0 Mercury.modalHandlers['foo'] = => callCount += 1 Mercury.modal.loadContent('content', {handler: 'foo'}) expect(callCount).toEqual(1) + it "translates the content if configured", -> + Mercury.config.localization.enabled = true + Mercury.modal.loadContent('<span>foo</span>') + expect($('.mercury-modal-content').html()).toEqual('<span>Bork!</span>') + it "calls resize", -> Mercury.modal.loadContent('content') expect(@resizeSpy.callCount).toEqual(1) describe "#setTitle", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test'), title: 'title'}) + Mercury.modal('/blank.html', {appendTo: $('#test'), title: 'title'}) it "sets the the title contents to what was provided in the options", -> Mercury.modal.options = {title: 'new title'} Mercury.modal.setTitle() expect($('.mercury-modal-title span').html()).toEqual('new title') + expect($('.mercury-modal-title a').css('display')).toEqual('inline') + it "hides the close button if the options.closeButton is false", -> + Mercury.modal.options = {title: 'new title', closeButton: false} + Mercury.modal.setTitle() + expect($('.mercury-modal-title a').css('display')).toEqual('none') + + describe "#reset", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test'), title: 'title'}) + Mercury.modal('/blank.html', {appendTo: $('#test'), title: 'title'}) it "clears the title and content elements", -> $('.mercury-modal-content').html('content') Mercury.modal.reset() expect($('.mercury-modal-content').html()).toEqual('') @@ -435,10 +465,10 @@ describe "#hide", -> beforeEach -> spyOn(Mercury.modal, 'appear').andCallFake(=>) - Mercury.modal('/evergreen/responses/blank.html', {appendTo: $('#test')}) + Mercury.modal('/blank.html', {appendTo: $('#test')}) it "triggers the focus:frame event", -> spy = spyOn(Mercury, 'trigger').andCallFake(=>) Mercury.modal.hide() expect(spy.callCount).toEqual(1)