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

editor_state_observer_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: e8c2d8c8793f116414eea554993f561cf7b81b40 (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
import { shallowMount } from '@vue/test-utils';
import { each } from 'lodash';
import EditorStateObserver, {
  tiptapToComponentMap,
} from '~/content_editor/components/editor_state_observer.vue';
import eventHubFactory from '~/helpers/event_hub_factory';
import { ALERT_EVENT } from '~/content_editor/constants';
import { createTestEditor } from '../test_utils';

describe('content_editor/components/editor_state_observer', () => {
  let tiptapEditor;
  let wrapper;
  let onDocUpdateListener;
  let onSelectionUpdateListener;
  let onTransactionListener;
  let onAlertListener;
  let eventHub;

  const buildEditor = () => {
    tiptapEditor = createTestEditor();
    eventHub = eventHubFactory();
    jest.spyOn(tiptapEditor, 'on');
  };

  const buildWrapper = () => {
    wrapper = shallowMount(EditorStateObserver, {
      provide: { tiptapEditor, eventHub },
      listeners: {
        docUpdate: onDocUpdateListener,
        selectionUpdate: onSelectionUpdateListener,
        transaction: onTransactionListener,
        [ALERT_EVENT]: onAlertListener,
      },
    });
  };

  beforeEach(() => {
    onDocUpdateListener = jest.fn();
    onSelectionUpdateListener = jest.fn();
    onTransactionListener = jest.fn();
    onAlertListener = jest.fn();
    buildEditor();
  });

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

  describe('when editor content changes', () => {
    it('emits update, selectionUpdate, and transaction events', () => {
      const content = '<p>My paragraph</p>';

      buildWrapper();

      tiptapEditor.commands.insertContent(content);

      expect(onDocUpdateListener).toHaveBeenCalledWith(
        expect.objectContaining({ editor: tiptapEditor }),
      );
      expect(onSelectionUpdateListener).toHaveBeenCalledWith(
        expect.objectContaining({ editor: tiptapEditor }),
      );
      expect(onSelectionUpdateListener).toHaveBeenCalledWith(
        expect.objectContaining({ editor: tiptapEditor }),
      );
    });
  });

  it.each`
    event          | listener
    ${ALERT_EVENT} | ${() => onAlertListener}
  `('listens to $event event in the eventBus object', ({ event, listener }) => {
    const args = {};

    buildWrapper();

    eventHub.$emit(event, args);
    expect(listener()).toHaveBeenCalledWith(args);
  });

  describe('when component is destroyed', () => {
    it('removes onTiptapDocUpdate and onTiptapSelectionUpdate hooks', () => {
      jest.spyOn(tiptapEditor, 'off');

      buildWrapper();

      wrapper.destroy();

      each(tiptapToComponentMap, (_, tiptapEvent) => {
        expect(tiptapEditor.off).toHaveBeenCalledWith(
          tiptapEvent,
          tiptapEditor.on.mock.calls.find(([eventName]) => eventName === tiptapEvent)[1],
        );
      });
    });

    it.each`
      event
      ${ALERT_EVENT}
    `('removes $event event hook from eventHub', ({ event }) => {
      jest.spyOn(eventHub, '$off');
      jest.spyOn(eventHub, '$on');

      buildWrapper();

      wrapper.destroy();

      expect(eventHub.$off).toHaveBeenCalledWith(
        event,
        eventHub.$on.mock.calls.find(([eventName]) => eventName === event)[1],
      );
    });
  });
});