Welcome to mirror list, hosted at ThFree Co, Russian Federation.

header_spec.js « markdown « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 077c21745714a60aed38c5cc9441d63037feae40 (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
135
136
137
138
139
140
141
142
143
import { shallowMount } from '@vue/test-utils';
import $ from 'jquery';
import HeaderComponent from '~/vue_shared/components/markdown/header.vue';
import ToolbarButton from '~/vue_shared/components/markdown/toolbar_button.vue';

describe('Markdown field header component', () => {
  let wrapper;

  const createWrapper = (props) => {
    wrapper = shallowMount(HeaderComponent, {
      propsData: {
        previewMarkdown: false,
        ...props,
      },
    });
  };

  const findToolbarButtons = () => wrapper.findAll(ToolbarButton);
  const findToolbarButtonByProp = (prop, value) =>
    findToolbarButtons()
      .filter((button) => button.props(prop) === value)
      .at(0);

  beforeEach(() => {
    window.gl = {
      client: {
        isMac: true,
      },
    };

    createWrapper();
  });

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('markdown header buttons', () => {
    it('renders the buttons with the correct title', () => {
      const buttons = [
        'Add bold text (⌘B)',
        'Add italic text (⌘I)',
        'Insert a quote',
        'Insert suggestion',
        'Insert code',
        'Add a link (⌘K)',
        'Add a bullet list',
        'Add a numbered list',
        'Add a task list',
        'Add a table',
        'Go full screen',
      ];
      const elements = findToolbarButtons();

      elements.wrappers.forEach((buttonEl, index) => {
        expect(buttonEl.props('buttonTitle')).toBe(buttons[index]);
      });
    });

    describe('when the user is on a non-Mac', () => {
      beforeEach(() => {
        delete window.gl.client.isMac;

        createWrapper();
      });

      it('renders keyboard shortcuts with Ctrl+ instead of ⌘', () => {
        const boldButton = findToolbarButtonByProp('icon', 'bold');

        expect(boldButton.props('buttonTitle')).toBe('Add bold text (Ctrl+B)');
      });
    });
  });

  it('renders `write` link as active when previewMarkdown is false', () => {
    expect(wrapper.find('li:nth-child(1)').classes()).toContain('active');
  });

  it('renders `preview` link as active when previewMarkdown is true', () => {
    createWrapper({ previewMarkdown: true });

    expect(wrapper.find('li:nth-child(2)').classes()).toContain('active');
  });

  it('emits toggle markdown event when clicking preview', () => {
    wrapper.find('.js-preview-link').trigger('click');

    return wrapper.vm
      .$nextTick()
      .then(() => {
        expect(wrapper.emitted('preview-markdown').length).toEqual(1);

        wrapper.find('.js-write-link').trigger('click');
        return wrapper.vm.$nextTick();
      })
      .then(() => {
        expect(wrapper.emitted('write-markdown').length).toEqual(1);
      });
  });

  it('does not emit toggle markdown event when triggered from another form', () => {
    $(document).triggerHandler('markdown-preview:show', [
      $(
        '<form><div class="js-vue-markdown-field"><textarea class="markdown-area"></textarea></div></form>',
      ),
    ]);

    expect(wrapper.emitted('preview-markdown')).toBeFalsy();
    expect(wrapper.emitted('write-markdown')).toBeFalsy();
  });

  it('blurs preview link after click', () => {
    const link = wrapper.find('li:nth-child(2) button');
    jest.spyOn(HTMLElement.prototype, 'blur').mockImplementation();

    link.trigger('click');

    expect(link.element.blur).toHaveBeenCalled();
  });

  it('renders markdown table template', () => {
    const tableButton = findToolbarButtonByProp('icon', 'table');

    expect(tableButton.props('tag')).toEqual(
      '| header | header |\n| ------ | ------ |\n| cell | cell |\n| cell | cell |',
    );
  });

  it('renders suggestion template', () => {
    expect(findToolbarButtonByProp('buttonTitle', 'Insert suggestion').props('tag')).toEqual(
      '```suggestion:-0+0\n{text}\n```',
    );
  });

  it('does not render suggestion button if `canSuggest` is set to false', () => {
    createWrapper({
      canSuggest: false,
    });

    expect(wrapper.find('.js-suggestion-btn').exists()).toBe(false);
  });
});