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

toolbar_text_style_dropdown_spec.js « components « content_editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a725ac1ca494391e0dbeda307e5583a9e26be9d (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
import { GlCollapsibleListbox } 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: {
        EditorStateObserver,
      },
      provide: {
        tiptapEditor,
        eventHub: eventHubFactory(),
      },
      propsData: {
        ...propsData,
      },
    });
  };
  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);

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

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

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

    TEXT_STYLE_DROPDOWN_ITEMS.forEach((textStyle, index) => {
      expect(findListbox().props('items').at(index).text).toContain(textStyle.label);
    });
    expect(findListbox().props('items').length).toBe(TEXT_STYLE_DROPDOWN_ITEMS.length);
  });

  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(findListbox().props('toggleText')).toBe(activeTextStyle.label);
    });

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

  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(findListbox().props('disabled')).toBe(true);
    });

    it('sets dropdown toggle text to Text style', () => {
      expect(findListbox().props('toggleText')).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']);

        findListbox().vm.$emit('select', TEXT_STYLE_DROPDOWN_ITEMS[index].label);
        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;

        findListbox().vm.$emit('select', TEXT_STYLE_DROPDOWN_ITEMS[index].label);
        expect(wrapper.emitted('execute')).toEqual([
          [
            {
              contentType,
              value: commandParams?.level,
            },
          ],
        ]);
        wrapper.destroy();
      });
    });
  });
});