quick_submit_spec.js 3.9 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
    it('disables input of type submit', function() {
F
Tests  
Filipa Lacerda 已提交
41
      const submitButton = $('.js-vue-quick-submit input[type=submit]');
W
winniehell 已提交
42
      this.textarea.trigger(keydownEvent());
43

44 45 46
      expect(submitButton).toBeDisabled();
    });
    it('disables button of type submit', function() {
F
Tests  
Filipa Lacerda 已提交
47
      const submitButton = $('.js-vue-quick-submit input[type=submit]');
48
      this.textarea.trigger(keydownEvent());
49

50
      expect(submitButton).toBeDisabled();
F
Fatih Acet 已提交
51
    });
52
    it('only clicks one submit', function() {
F
Tests  
Filipa Lacerda 已提交
53
      const existingSubmit = $('.js-vue-quick-submit input[type=submit]');
54 55 56 57 58 59 60 61 62 63 64 65
      // Add an extra submit button
      const newSubmit = $('<button type="submit">Submit it</button>');
      newSubmit.insertAfter(this.textarea);

      const oldClick = spyOnEvent(existingSubmit, 'click');
      const newClick = spyOnEvent(newSubmit, 'click');

      this.textarea.trigger(keydownEvent());

      expect(oldClick).not.toHaveBeenTriggered();
      expect(newClick).toHaveBeenTriggered();
    });
66
    // We cannot stub `navigator.userAgent` for CI's `rake karma` task, so we'll
67
    // only run the tests that apply to the current platform
F
Fatih Acet 已提交
68 69
    if (navigator.userAgent.match(/Macintosh/)) {
      it('responds to Meta+Enter', function() {
W
winniehell 已提交
70
        this.textarea.trigger(keydownEvent());
F
Fatih Acet 已提交
71 72 73
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
W
winniehell 已提交
74
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
75 76
          altKey: true
        }));
W
winniehell 已提交
77
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
78 79
          ctrlKey: true
        }));
W
winniehell 已提交
80
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
81 82 83 84 85 86
          shiftKey: true
        }));
        return expect(this.spies.submit).not.toHaveBeenTriggered();
      });
    } else {
      it('responds to Ctrl+Enter', function() {
W
winniehell 已提交
87
        this.textarea.trigger(keydownEvent());
F
Fatih Acet 已提交
88 89 90
        return expect(this.spies.submit).toHaveBeenTriggered();
      });
      it('excludes other modifier keys', function() {
W
winniehell 已提交
91
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
92 93
          altKey: true
        }));
W
winniehell 已提交
94
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
95 96
          metaKey: true
        }));
W
winniehell 已提交
97
        this.textarea.trigger(keydownEvent({
F
Fatih Acet 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
          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));
    };
  });
119
}).call(window);