summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/toolbar_text_style_dropdown_spec.js
blob: 9a46e27404fae57f1cea617dc6a694d2dac570b0 (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
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ToolbarTextStyleDropdown from '~/content_editor/components/toolbar_text_style_dropdown.vue';
import { TEXT_STYLE_DROPDOWN_ITEMS } from '~/content_editor/constants';
import { tiptapExtension as Heading } from '~/content_editor/extensions/heading';
import { createTestEditor, mockChainedCommands } from '../test_utils';

describe('content_editor/components/toolbar_headings_dropdown', () => {
  let wrapper;
  let tiptapEditor;

  const buildEditor = () => {
    tiptapEditor = createTestEditor({
      extensions: [Heading],
    });

    jest.spyOn(tiptapEditor, 'isActive');
  };

  const buildWrapper = (propsData = {}) => {
    wrapper = shallowMountExtended(ToolbarTextStyleDropdown, {
      stubs: {
        GlDropdown,
        GlDropdownItem,
      },
      propsData: {
        tiptapEditor,
        ...propsData,
      },
    });
  };
  const findDropdown = () => wrapper.findComponent(GlDropdown);

  beforeEach(() => {
    buildEditor();
  });

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

  it('renders all text styles as dropdown items', () => {
    buildWrapper();

    TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle) => {
      expect(wrapper.findByText(textStyle.label).exists()).toBe(true);
    });
  });

  describe('when there is an active item ', () => {
    let activeTextStyle;

    beforeEach(() => {
      [, activeTextStyle] = TEXT_STYLE_DROPDOWN_ITEMS;

      tiptapEditor.isActive.mockImplementation(
        (contentType, params) =>
          activeTextStyle.contentType === contentType && activeTextStyle.commandParams === params,
      );

      buildWrapper();
    });

    it('displays the active text style label as the dropdown toggle text ', () => {
      expect(findDropdown().props().text).toBe(activeTextStyle.label);
    });

    it('sets dropdown as enabled', () => {
      expect(findDropdown().props().disabled).toBe(false);
    });

    it('sets active item as active', () => {
      const activeItem = wrapper
        .findAllComponents(GlDropdownItem)
        .filter((item) => item.text() === activeTextStyle.label)
        .at(0);
      expect(activeItem.props().isChecked).toBe(true);
    });
  });

  describe('when there isn’t an active item', () => {
    beforeEach(() => {
      tiptapEditor.isActive.mockReturnValue(false);
      buildWrapper();
    });

    it('sets dropdown as disabled', () => {
      expect(findDropdown().props().disabled).toBe(true);
    });

    it('sets dropdown toggle text to Text style', () => {
      expect(findDropdown().props().text).toBe('Text style');
    });
  });

  describe('when a text style is selected', () => {
    it('executes the tiptap command related to that text style', () => {
      buildWrapper();

      TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle, index) => {
        const { editorCommand, commandParams } = textStyle;
        const commands = mockChainedCommands(tiptapEditor, [editorCommand, 'focus', 'run']);

        wrapper.findAllComponents(GlDropdownItem).at(index).vm.$emit('click');
        expect(commands[editorCommand]).toHaveBeenCalledWith(commandParams || {});
        expect(commands.focus).toHaveBeenCalled();
        expect(commands.run).toHaveBeenCalled();
      });
    });

    it('emits execute event with contentType and value params that indicates the heading level', () => {
      TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle, index) => {
        buildWrapper();
        const { contentType, commandParams } = textStyle;

        wrapper.findAllComponents(GlDropdownItem).at(index).vm.$emit('click');
        expect(wrapper.emitted('execute')).toEqual([
          [
            {
              contentType,
              value: commandParams?.level,
            },
          ],
        ]);
        wrapper.destroy();
      });
    });
  });
});