summaryrefslogtreecommitdiff
path: root/spec/javascripts/behaviors/quick_submit_spec.js
blob: d8aa5c636da0ac386a0e570ca27257bccab95c59 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import $ from 'jquery';
import '~/behaviors/quick_submit';

describe('Quick Submit behavior', function () {
  const keydownEvent = (options = { keyCode: 13, metaKey: true }) => $.Event('keydown', options);

  preloadFixtures('snippets/show.html.raw');

  beforeEach(() => {
    loadFixtures('snippets/show.html.raw');
    $('form').submit(e => {
      // Prevent a form submit from moving us off the testing page
      e.preventDefault();
    });
    this.spies = {
      submit: spyOnEvent('form', 'submit'),
    };

    this.textarea = $('.js-quick-submit textarea').first();
  });

  afterEach(() => {
    // Undo what we did to the shared <body>
    $('body').removeAttr('data-page');
  });

  it('does not respond to other keyCodes', () => {
    this.textarea.trigger(
      keydownEvent({
        keyCode: 32,
      }),
    );
    expect(this.spies.submit).not.toHaveBeenTriggered();
  });

  it('does not respond to Enter alone', () => {
    this.textarea.trigger(
      keydownEvent({
        ctrlKey: false,
        metaKey: false,
      }),
    );
    expect(this.spies.submit).not.toHaveBeenTriggered();
  });

  it('does not respond to repeated events', () => {
    this.textarea.trigger(
      keydownEvent({
        repeat: true,
      }),
    );
    expect(this.spies.submit).not.toHaveBeenTriggered();
  });

  it('disables input of type submit', () => {
    const submitButton = $('.js-quick-submit input[type=submit]');
    this.textarea.trigger(keydownEvent());

    expect(submitButton).toBeDisabled();
  });
  it('disables button of type submit', () => {
    const submitButton = $('.js-quick-submit input[type=submit]');
    this.textarea.trigger(keydownEvent());

    expect(submitButton).toBeDisabled();
  });
  it('only clicks one submit', () => {
    const existingSubmit = $('.js-quick-submit input[type=submit]');
    // 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();
  });
  // 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/)) {
    describe('In Macintosh', () => {
      it('responds to Meta+Enter', () => {
        this.textarea.trigger(keydownEvent());
        return expect(this.spies.submit).toHaveBeenTriggered();
      });

      it('excludes other modifier keys', () => {
        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', () => {
      this.textarea.trigger(keydownEvent());
      return expect(this.spies.submit).toHaveBeenTriggered();
    });

    it('excludes other modifier keys', () => {
      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();
    });
  }
});