2015-10-24 18:46:33 +05:30
|
|
|
#= require behaviors/quick_submit
|
|
|
|
|
|
|
|
describe 'Quick Submit behavior', ->
|
|
|
|
fixture.preload('behaviors/quick_submit.html')
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
fixture.load('behaviors/quick_submit.html')
|
|
|
|
|
|
|
|
# Prevent a form submit from moving us off the testing page
|
|
|
|
$('form').submit (e) -> e.preventDefault()
|
|
|
|
|
|
|
|
@spies = {
|
|
|
|
submit: spyOnEvent('form', 'submit')
|
|
|
|
}
|
|
|
|
|
|
|
|
it 'does not respond to other keyCodes', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(keyCode: 32))
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).not.toHaveBeenTriggered()
|
|
|
|
|
|
|
|
it 'does not respond to Enter alone', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(ctrlKey: false, metaKey: false))
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).not.toHaveBeenTriggered()
|
|
|
|
|
|
|
|
it 'does not respond to repeated events', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(repeat: true))
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).not.toHaveBeenTriggered()
|
|
|
|
|
|
|
|
it 'disables submit buttons', ->
|
|
|
|
$('textarea').trigger(keydownEvent())
|
|
|
|
|
|
|
|
expect($('input[type=submit]')).toBeDisabled()
|
|
|
|
expect($('button[type=submit]')).toBeDisabled()
|
|
|
|
|
|
|
|
# We cannot stub `navigator.userAgent` for CI's `rake teaspoon` task, so we'll
|
|
|
|
# only run the tests that apply to the current platform
|
|
|
|
if navigator.userAgent.match(/Macintosh/)
|
|
|
|
it 'responds to Meta+Enter', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent())
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).toHaveBeenTriggered()
|
|
|
|
|
|
|
|
it 'excludes other modifier keys', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(altKey: true))
|
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(ctrlKey: true))
|
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(shiftKey: true))
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).not.toHaveBeenTriggered()
|
|
|
|
else
|
|
|
|
it 'responds to Ctrl+Enter', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent())
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).toHaveBeenTriggered()
|
|
|
|
|
|
|
|
it 'excludes other modifier keys', ->
|
2016-06-16 23:09:34 +05:30
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(altKey: true))
|
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(metaKey: true))
|
|
|
|
$('input.quick-submit-input').trigger(keydownEvent(shiftKey: true))
|
2015-10-24 18:46:33 +05:30
|
|
|
|
|
|
|
expect(@spies.submit).not.toHaveBeenTriggered()
|
|
|
|
|
|
|
|
keydownEvent = (options) ->
|
|
|
|
if navigator.userAgent.match(/Macintosh/)
|
|
|
|
defaults = { keyCode: 13, metaKey: true }
|
|
|
|
else
|
|
|
|
defaults = { keyCode: 13, ctrlKey: true }
|
|
|
|
|
|
|
|
$.Event('keydown', $.extend({}, defaults, options))
|