summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/toolbar_button_spec.js
blob: a49efa34017a574d7e3346e9447ad313552b2388 (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
import { GlButton } from '@gitlab/ui';
import { Extension } from '@tiptap/core';
import { shallowMount } from '@vue/test-utils';
import ToolbarButton from '~/content_editor/components/toolbar_button.vue';
import { createContentEditor } from '~/content_editor/services/create_content_editor';

describe('content_editor/components/toolbar_button', () => {
  let wrapper;
  let tiptapEditor;
  let toggleFooSpy;
  const CONTENT_TYPE = 'bold';
  const ICON_NAME = 'bold';
  const LABEL = 'Bold';

  const buildEditor = () => {
    toggleFooSpy = jest.fn();
    tiptapEditor = createContentEditor({
      extensions: [
        {
          tiptapExtension: Extension.create({
            addCommands() {
              return {
                toggleFoo: () => toggleFooSpy,
              };
            },
          }),
        },
      ],
      renderMarkdown: () => true,
    }).tiptapEditor;

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

  const buildWrapper = (propsData = {}) => {
    wrapper = shallowMount(ToolbarButton, {
      stubs: {
        GlButton,
      },
      propsData: {
        tiptapEditor,
        contentType: CONTENT_TYPE,
        iconName: ICON_NAME,
        label: LABEL,
        ...propsData,
      },
    });
  };
  const findButton = () => wrapper.findComponent(GlButton);

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

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

  it('displays tertiary, small button with a provided label and icon', () => {
    buildWrapper();

    expect(findButton().html()).toMatchSnapshot();
  });

  it.each`
    editorState                             | outcomeDescription         | outcome
    ${{ isActive: true, isFocused: true }}  | ${'button is active'}      | ${true}
    ${{ isActive: false, isFocused: true }} | ${'button is not active'}  | ${false}
    ${{ isActive: true, isFocused: false }} | ${'button is not active '} | ${false}
  `('$outcomeDescription when when editor state is $editorState', ({ editorState, outcome }) => {
    tiptapEditor.isActive.mockReturnValueOnce(editorState.isActive);
    tiptapEditor.isFocused = editorState.isFocused;
    buildWrapper();

    expect(findButton().classes().includes('active')).toBe(outcome);
    expect(tiptapEditor.isActive).toHaveBeenCalledWith(CONTENT_TYPE);
  });

  describe('when button is clicked', () => {
    it('executes the content type command when executeCommand = true', async () => {
      buildWrapper({ editorCommand: 'toggleFoo' });

      await findButton().trigger('click');

      expect(toggleFooSpy).toHaveBeenCalled();
      expect(wrapper.emitted().execute).toHaveLength(1);
    });

    it('does not executes the content type command when executeCommand = false', async () => {
      buildWrapper();

      await findButton().trigger('click');

      expect(toggleFooSpy).not.toHaveBeenCalled();
      expect(wrapper.emitted().execute).toHaveLength(1);
    });
  });
});