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

content_editor_alert_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: ee9ead8f8a7e4f97792978f089c188f12fe9e224 (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 { GlAlert } from '@gitlab/ui';
import { nextTick } from 'vue';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import ContentEditorAlert from '~/content_editor/components/content_editor_alert.vue';
import EditorStateObserver 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/content_editor_alert', () => {
  let wrapper;
  let tiptapEditor;
  let eventHub;

  const findErrorAlert = () => wrapper.findComponent(GlAlert);

  const createWrapper = async () => {
    tiptapEditor = createTestEditor();
    eventHub = eventHubFactory();

    wrapper = shallowMountExtended(ContentEditorAlert, {
      provide: {
        tiptapEditor,
        eventHub,
      },
      stubs: {
        EditorStateObserver,
      },
    });
  };

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

  it.each`
    variant      | message
    ${'danger'}  | ${'An error occurred'}
    ${'warning'} | ${'A warning'}
  `(
    'renders error when content editor emits an error event for variant: $variant',
    async ({ message, variant }) => {
      createWrapper();

      eventHub.$emit(ALERT_EVENT, { message, variant });

      await nextTick();

      expect(findErrorAlert().text()).toBe(message);
      expect(findErrorAlert().attributes().variant).toBe(variant);
    },
  );

  it('does not show primary action by default', async () => {
    const message = 'error message';

    createWrapper();
    eventHub.$emit(ALERT_EVENT, { message });
    await nextTick();

    expect(findErrorAlert().attributes().primaryButtonText).toBeUndefined();
  });

  it('allows dismissing the error', async () => {
    const message = 'error message';

    createWrapper();
    eventHub.$emit(ALERT_EVENT, { message });
    await nextTick();
    findErrorAlert().vm.$emit('dismiss');
    await nextTick();

    expect(findErrorAlert().exists()).toBe(false);
  });

  it('allows dismissing the error with a primary action button', async () => {
    const message = 'error message';
    const actionLabel = 'Retry';
    const action = jest.fn();

    createWrapper();
    eventHub.$emit(ALERT_EVENT, { message, action, actionLabel });
    await nextTick();
    findErrorAlert().vm.$emit('primaryAction');
    await nextTick();

    expect(action).toHaveBeenCalled();
    expect(findErrorAlert().exists()).toBe(false);
  });
});