summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/toolbar_button_spec.js
blob: d848adcbff82f110a4a867c8126d73ea527d800c (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
import { GlButton } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import ToolbarButton from '~/content_editor/components/toolbar_button.vue';
import { createTestEditor, mockChainedCommands } from '../test_utils';

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

  const buildEditor = () => {
    tiptapEditor = createTestEditor();

    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 () => {
      const editorCommand = 'toggleFoo';
      const mockCommands = mockChainedCommands(tiptapEditor, [editorCommand, 'focus', 'run']);

      buildWrapper({ editorCommand });

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

      expect(mockCommands[editorCommand]).toHaveBeenCalled();
      expect(mockCommands.focus).toHaveBeenCalled();
      expect(mockCommands.run).toHaveBeenCalled();
      expect(wrapper.emitted().execute).toHaveLength(1);
    });

    it('does not executes the content type command when executeCommand = false', async () => {
      const editorCommand = 'toggleFoo';
      const mockCommands = mockChainedCommands(tiptapEditor, [editorCommand, 'run']);

      buildWrapper();

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

      expect(mockCommands[editorCommand]).not.toHaveBeenCalled();
      expect(wrapper.emitted().execute).toHaveLength(1);
    });
  });
});