quick_submit_spec.js 3.5 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-var, no-return-assign, comma-dangle, jasmine/no-spec-dupes, new-cap, max-len */
F
Fatih Acet 已提交
2

3
import '~/behaviors/quick_submit';
F
Fatih Acet 已提交
4 5 6 7

(function() {
  describe('Quick Submit behavior', function() {
    var keydownEvent;
8
    preloadFixtures('issues/open-issue.html.raw');
F
Fatih Acet 已提交
9
    beforeEach(function() {
10
      loadFixtures('issues/open-issue.html.raw');
F
Fatih Acet 已提交
11
      $('form').submit(function(e) {
12
        // Prevent a form submit from moving us off the testing page
F
Fatih Acet 已提交
13 14
        return e.preventDefault();
      });
W
winniehell 已提交
15
      this.spies = {
F
Fatih Acet 已提交
16 17
        submit: spyOnEvent('form', 'submit')
      };
W
winniehell 已提交
18 19

      this.textarea = $('.js-quick-submit textarea').first();
F
Fatih Acet 已提交
20 21
    });
    it('does not respond to other keyCodes', function() {
W
winniehell 已提交
22
      this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
23 24 25 26 27
        keyCode: 32
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('does not respond to Enter alone', function() {
W
winniehell 已提交
28
      this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
29 30 31 32 33 34
        ctrlKey: false,
        metaKey: false
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('does not respond to repeated events', function() {
W
winniehell 已提交
35
      this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
36 37 38 39
        repeat: true
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
40 41
    it('disables input of type submit', function() {
      const submitButton = $('.js-quick-submit input[type=submit]');
W
winniehell 已提交
42
      this.textarea.trigger(keydownEvent());
43 44 45 46 47 48 49 50 51
      expect(submitButton).toBeDisabled();
    });
    it('disables button of type submit', function() {
      // button doesn't exist in fixture, add it manually
      const submitButton = $('<button type="submit">Submit it</button>');
      submitButton.insertAfter(this.textarea);

      this.textarea.trigger(keydownEvent());
      expect(submitButton).toBeDisabled();
F
Fatih Acet 已提交
52
    });
53
    // We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
54
    // only run the tests that apply to the current platform
F
Fatih Acet 已提交
55 56
    if (navigator.userAgent.match(/Macintosh/)) {
      it('responds to Meta+Enter', function() {
W
winniehell 已提交
57
        this.textarea.trigger(keydownEvent());
F
Fatih Acet 已提交
58 59 60
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
W
winniehell 已提交
61
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
62 63
          altKey: true
        }));
W
winniehell 已提交
64
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
65 66
          ctrlKey: true
        }));
W
winniehell 已提交
67
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
68 69 70 71 72 73
          shiftKey: true
        }));
        return expect(this.spies.submit).not.toHaveBeenTriggered();
      });
    } else {
      it('responds to Ctrl+Enter', function() {
W
winniehell 已提交
74
        this.textarea.trigger(keydownEvent());
F
Fatih Acet 已提交
75 76 77
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
W
winniehell 已提交
78
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
79 80
          altKey: true
        }));
W
winniehell 已提交
81
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
82 83
          metaKey: true
        }));
W
winniehell 已提交
84
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
          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));
    };
  });
106
}).call(window);