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

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

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

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

  const buildWrapper = (propsData = {}) => {
    wrapper = shallowMountExtended(ToolbarTextStyleDropdown, {
      stubs: {
        GlDropdown,
        GlDropdownItem,
        EditorStateObserver,
      },
      provide: {
        tiptapEditor,
        eventHub: eventHubFactory(),
      },
      propsData: {
        ...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(async () => {
      [, activeTextStyle] = TEXT_STYLE_DROPDOWN_ITEMS;

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

      buildWrapper();
      await emitEditorEvent({ event: 'transaction', tiptapEditor });
    });

    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(async () => {
      tiptapEditor.isActive.mockReturnValue(false);
      buildWrapper();
      await emitEditorEvent({ event: 'transaction', tiptapEditor });
    });

    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();
      });
    });
  });
});