summaryrefslogtreecommitdiff
path: root/spec/javascripts/behaviors/quick_submit_spec.js
blob: f56b99f8a163860283523e215d623d7ca18f08f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
44
45
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
99
100
101
102
103
104
105
106
/* eslint-disable space-before-function-paren, no-var, no-return-assign, comma-dangle, jasmine/no-spec-dupes, new-cap, max-len */

import '~/behaviors/quick_submit';

(function() {
  describe('Quick Submit behavior', function() {
    var keydownEvent;
    preloadFixtures('issues/open-issue.html.raw');
    beforeEach(function() {
      loadFixtures('issues/open-issue.html.raw');
      $('form').submit(function(e) {
        // Prevent a form submit from moving us off the testing page
        return e.preventDefault();
      });
      this.spies = {
        submit: spyOnEvent('form', 'submit')
      };

      this.textarea = $('.js-quick-submit textarea').first();
    });
    it('does not respond to other keyCodes', function() {
      this.textarea.trigger(keydownEvent({
        keyCode: 32
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('does not respond to Enter alone', function() {
      this.textarea.trigger(keydownEvent({
        ctrlKey: false,
        metaKey: false
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('does not respond to repeated events', function() {
      this.textarea.trigger(keydownEvent({
        repeat: true
      }));
      return expect(this.spies.submit).not.toHaveBeenTriggered();
    });
    it('disables input of type submit', function() {
      const submitButton = $('.js-quick-submit input[type=submit]');
      this.textarea.trigger(keydownEvent());
      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();
    });
    // We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
    // only run the tests that apply to the current platform
    if (navigator.userAgent.match(/Macintosh/)) {
      it('responds to Meta+Enter', function() {
        this.textarea.trigger(keydownEvent());
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
        this.textarea.trigger(keydownEvent({
          altKey: true
        }));
        this.textarea.trigger(keydownEvent({
          ctrlKey: true
        }));
        this.textarea.trigger(keydownEvent({
          shiftKey: true
        }));
        return expect(this.spies.submit).not.toHaveBeenTriggered();
      });
    } else {
      it('responds to Ctrl+Enter', function() {
        this.textarea.trigger(keydownEvent());
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
        this.textarea.trigger(keydownEvent({
          altKey: true
        }));
        this.textarea.trigger(keydownEvent({
          metaKey: true
        }));
        this.textarea.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(window);