quick_submit_spec.js 3.34 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-var, no-return-assign, comma-dangle, jasmine/no-spec-dupes, new-cap, padded-blocks, max-len */
Fatih Acet's avatar
Fatih Acet committed
2

3 4
require('./spec_helper');
require('behaviors/quick_submit');
Fatih Acet's avatar
Fatih Acet committed
5 6 7 8

(function() {
  describe('Quick Submit behavior', function() {
    var keydownEvent;
9
    preloadFixtures('static/behaviors/quick_submit.html.raw');
Fatih Acet's avatar
Fatih Acet committed
10
    beforeEach(function() {
11
      loadFixtures('static/behaviors/quick_submit.html.raw');
Fatih Acet's avatar
Fatih Acet committed
12
      $('form').submit(function(e) {
13
        // Prevent a form submit from moving us off the testing page
Fatih Acet's avatar
Fatih Acet committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        return e.preventDefault();
      });
      return this.spies = {
        submit: spyOnEvent('form', 'submit')
      };
    });
    it('does not respond to other keyCodes', function() {
      $('input.quick-submit-input').trigger(keydownEvent({
        keyCode: 32
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('does not respond to Enter alone', function() {
      $('input.quick-submit-input').trigger(keydownEvent({
        ctrlKey: false,
        metaKey: false
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('does not respond to repeated events', function() {
      $('input.quick-submit-input').trigger(keydownEvent({
        repeat: true
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('disables submit buttons', function() {
      $('textarea').trigger(keydownEvent());
      expect($('input[type=submit]')).toBeDisabled();
      return expect($('button[type=submit]')).toBeDisabled();
    });
44 45
    // We cannot stub `navigator.userAgent` for CI's `rake teaspoon` task, so we'll
    // only run the tests that apply to the current platform
Fatih Acet's avatar
Fatih Acet committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    if (navigator.userAgent.match(/Macintosh/)) {
      it('responds to Meta+Enter', function() {
        $('input.quick-submit-input').trigger(keydownEvent());
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
        $('input.quick-submit-input').trigger(keydownEvent({
          altKey: true
        }));
        $('input.quick-submit-input').trigger(keydownEvent({
          ctrlKey: true
        }));
        $('input.quick-submit-input').trigger(keydownEvent({
          shiftKey: true
        }));
        return expect(this.spies.submit).not.toHaveBeenTriggered();
      });
    } else {
      it('responds to Ctrl+Enter', function() {
        $('input.quick-submit-input').trigger(keydownEvent());
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
        $('input.quick-submit-input').trigger(keydownEvent({
          altKey: true
        }));
        $('input.quick-submit-input').trigger(keydownEvent({
          metaKey: true
        }));
        $('input.quick-submit-input').trigger(keydownEvent({
          shiftKey: true
        }));
        return expect(this.spies.submit).not.toHaveBeenTriggered();
      });
    }
    return keydownEvent = function(options) {
      var defaults;
      if (navigator.userAgent.match(/Macintosh/)) {
        defaults = {
          keyCode: 13,
          metaKey: true
        };
      } else {
        defaults = {
          keyCode: 13,
          ctrlKey: true
        };
      }
      return $.Event('keydown', $.extend({}, defaults, options));
    };
  });

}).call(this);