quick_submit_spec.js.coffee 2.4 KB
Newer Older
R
Robert Speicher 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#= 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', ->
17
    $('input.quick-submit-input').trigger(keydownEvent(keyCode: 32))
R
Robert Speicher 已提交
18 19 20 21

    expect(@spies.submit).not.toHaveBeenTriggered()

  it 'does not respond to Enter alone', ->
22
    $('input.quick-submit-input').trigger(keydownEvent(ctrlKey: false, metaKey: false))
R
Robert Speicher 已提交
23 24 25

    expect(@spies.submit).not.toHaveBeenTriggered()

R
Robert Speicher 已提交
26
  it 'does not respond to repeated events', ->
27
    $('input.quick-submit-input').trigger(keydownEvent(repeat: true))
R
Robert Speicher 已提交
28 29 30

    expect(@spies.submit).not.toHaveBeenTriggered()

R
Robert Speicher 已提交
31 32 33 34 35 36 37 38 39 40
  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', ->
41
      $('input.quick-submit-input').trigger(keydownEvent())
R
Robert Speicher 已提交
42 43 44 45

      expect(@spies.submit).toHaveBeenTriggered()

    it 'excludes other modifier keys', ->
46 47 48
      $('input.quick-submit-input').trigger(keydownEvent(altKey: true))
      $('input.quick-submit-input').trigger(keydownEvent(ctrlKey: true))
      $('input.quick-submit-input').trigger(keydownEvent(shiftKey: true))
R
Robert Speicher 已提交
49 50 51 52

      expect(@spies.submit).not.toHaveBeenTriggered()
  else
    it 'responds to Ctrl+Enter', ->
53
      $('input.quick-submit-input').trigger(keydownEvent())
R
Robert Speicher 已提交
54 55 56 57

      expect(@spies.submit).toHaveBeenTriggered()

    it 'excludes other modifier keys', ->
58 59 60
      $('input.quick-submit-input').trigger(keydownEvent(altKey: true))
      $('input.quick-submit-input').trigger(keydownEvent(metaKey: true))
      $('input.quick-submit-input').trigger(keydownEvent(shiftKey: true))
R
Robert Speicher 已提交
61 62 63 64 65 66 67 68 69

      expect(@spies.submit).not.toHaveBeenTriggered()

  keydownEvent = (options) ->
    if navigator.userAgent.match(/Macintosh/)
      defaults = { keyCode: 13, metaKey: true }
    else
      defaults = { keyCode: 13, ctrlKey: true }

70
    $.Event('keydown', $.extend({}, defaults, options))