summaryrefslogtreecommitdiff
path: root/spec/frontend/shortcuts_spec.js
blob: 3d16074154cbdf834d9e4a3a1e8656441a5cd997 (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
import $ from 'jquery';
import Shortcuts from '~/behaviors/shortcuts/shortcuts';

describe('Shortcuts', () => {
  const fixtureName = 'snippets/show.html';
  const createEvent = (type, target) =>
    $.Event(type, {
      target,
    });

  preloadFixtures(fixtureName);

  describe('toggleMarkdownPreview', () => {
    beforeEach(() => {
      loadFixtures(fixtureName);

      jest.spyOn(document.querySelector('.js-new-note-form .js-md-preview-button'), 'focus');
      jest.spyOn(document.querySelector('.edit-note .js-md-preview-button'), 'focus');

      new Shortcuts(); // eslint-disable-line no-new
    });

    it('focuses preview button in form', () => {
      Shortcuts.toggleMarkdownPreview(
        createEvent('KeyboardEvent', document.querySelector('.js-new-note-form .js-note-text')),
      );

      expect(
        document.querySelector('.js-new-note-form .js-md-preview-button').focus,
      ).toHaveBeenCalled();
    });

    it('focues preview button inside edit comment form', () => {
      document.querySelector('.js-note-edit').click();

      Shortcuts.toggleMarkdownPreview(
        createEvent('KeyboardEvent', document.querySelector('.edit-note .js-note-text')),
      );

      expect(
        document.querySelector('.js-new-note-form .js-md-preview-button').focus,
      ).not.toHaveBeenCalled();
      expect(document.querySelector('.edit-note .js-md-preview-button').focus).toHaveBeenCalled();
    });
  });
});