spec_app/spec/javascripts/up/modal_spec.js.coffee in unpoly-rails-0.27.3 vs spec_app/spec/javascripts/up/modal_spec.js.coffee in unpoly-rails-0.28.0
- old
+ new
@@ -316,18 +316,29 @@
expect($modal).toBeInDOM()
expect($dialog.attr('style')).toBeBlank()
describe 'up.modal.close', ->
- it 'closes a currently open modal'
+ it 'closes a currently open modal', (done) ->
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>')
- it 'does nothing if no modal is open'
+ modalContent = $('.modal')
+ expect(modalContent).toBeInDOM()
- describe 'up.modal.source', ->
+ up.modal.close().then ->
+ expect(modalContent).not.toBeInDOM()
+ done()
- it 'should have tests'
+ it 'does nothing if no modal is open', (done) ->
+ wasClosed = false
+ up.on 'up:modal:close', ->
+ wasClosed = true
+ up.modal.close().then ->
+ expect(wasClosed).toBe(false)
+ done()
+
describe 'unobtrusive behavior', ->
describe 'a[up-modal]', ->
beforeEach ->
@@ -440,9 +451,82 @@
up.on 'up:modal:close', ->
wasClosed = true
$link.click()
expect(wasClosed).toBe(false)
expect(wasDefaultPrevented).toBe(false)
+
+ describe 'template behavior', ->
+
+ it 'closes the modal on close icon click', (done) ->
+ wasClosed = false
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>', animation: false)
+
+ closeIcon = $('.up-modal-close')
+ up.on 'up:modal:close', ->
+ wasClosed = true
+
+ closeIcon.click()
+ u.nextFrame ->
+ expect(wasClosed).toBe(true)
+ done()
+
+ it 'closes the modal on backdrop click', (done) ->
+ wasClosed = false
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>', animation: false)
+
+ backdrop = $('.up-modal-backdrop')
+ up.on 'up:modal:close', ->
+ wasClosed = true
+
+ backdrop.click()
+ u.nextFrame ->
+ expect(wasClosed).toBe(true)
+ done()
+
+ it 'closes the modal when the user presses the escape key', (done) ->
+ wasClosed = false
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>', animation: false)
+ up.on 'up:modal:close', ->
+ wasClosed = true
+
+ escapeEvent = $.Event('keydown', keyCode: 27)
+ $('body').trigger(escapeEvent)
+ u.nextFrame ->
+ expect(wasClosed).toBe(true)
+ done()
+
+ describe 'when opened with { closable: false }', ->
+
+ it 'does not render a close icon', ->
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>', animation: false, closable: false)
+
+ modal = $('.up-modal')
+ expect(modal).not.toContainElement('.up-modal-close')
+
+ it 'does not close the modal on backdrop click', (done) ->
+ wasClosed = false
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>', animation: false, closable: false)
+
+ backdrop = $('.up-modal-backdrop')
+ up.on 'up:modal:close', ->
+ wasClosed = true
+
+ backdrop.click()
+ u.nextFrame ->
+ expect(wasClosed).toBe(false)
+ done()
+
+ it 'does not close the modal when the user presses the escape key', (done) ->
+ wasClosed = false
+ up.modal.extract('.modal', '<div class="modal">Modal content</div>', animation: false, closable: false)
+ up.on 'up:modal:close', ->
+ wasClosed = true
+
+ escapeEvent = $.Event('keydown', keyCode: 27)
+ $('body').trigger(escapeEvent)
+ u.nextFrame ->
+ expect(wasClosed).toBe(false)
+ done()
describe 'when replacing content', ->
beforeEach ->
up.motion.config.enabled = false