summaryrefslogtreecommitdiff
path: root/spec/frontend/issues/show/components/fields/description_spec.js
blob: cd4d422583b5deae61f813fb08a4594e36d3077b (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
import { shallowMount } from '@vue/test-utils';
import DescriptionField from '~/issues/show/components/fields/description.vue';
import eventHub from '~/issues/show/event_hub';
import MarkdownField from '~/vue_shared/components/markdown/field.vue';
import MarkdownEditor from '~/vue_shared/components/markdown/markdown_editor.vue';

describe('Description field component', () => {
  let wrapper;

  const findTextarea = () => wrapper.findComponent({ ref: 'textarea' });
  const findMarkdownEditor = () => wrapper.findComponent(MarkdownEditor);

  const mountComponent = ({ description = 'test', contentEditorOnIssues = false } = {}) =>
    shallowMount(DescriptionField, {
      attachTo: document.body,
      propsData: {
        markdownPreviewPath: '/',
        markdownDocsPath: '/',
        quickActionsDocsPath: '/',
        value: description,
      },
      provide: {
        glFeatures: {
          contentEditorOnIssues,
        },
      },
      stubs: {
        MarkdownField,
      },
    });

  beforeEach(() => {
    jest.spyOn(eventHub, '$emit');
  });

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

  it('renders markdown field with description', () => {
    wrapper = mountComponent();

    expect(findTextarea().element.value).toBe('test');
  });

  it('renders markdown field with a markdown description', () => {
    const markdown = '**test**';

    wrapper = mountComponent({ description: markdown });

    expect(findTextarea().element.value).toBe(markdown);
  });

  it('focuses field when mounted', () => {
    wrapper = mountComponent();

    expect(document.activeElement).toBe(findTextarea().element);
  });

  it('triggers update with meta+enter', () => {
    wrapper = mountComponent();

    findTextarea().trigger('keydown.enter', { metaKey: true });

    expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
  });

  it('triggers update with ctrl+enter', () => {
    wrapper = mountComponent();

    findTextarea().trigger('keydown.enter', { ctrlKey: true });

    expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
  });

  describe('when contentEditorOnIssues feature flag is on', () => {
    beforeEach(() => {
      wrapper = mountComponent({ contentEditorOnIssues: true });
    });

    it('uses the MarkdownEditor component to edit markdown', () => {
      expect(findMarkdownEditor().props()).toEqual(
        expect.objectContaining({
          value: 'test',
          renderMarkdownPath: '/',
          markdownDocsPath: '/',
          quickActionsDocsPath: expect.any(String),
          initOnAutofocus: true,
          supportsQuickActions: true,
          enableAutocomplete: true,
        }),
      );
    });

    it('triggers update with meta+enter', () => {
      findMarkdownEditor().vm.$emit('keydown', {
        type: 'keydown',
        keyCode: 13,
        metaKey: true,
      });

      expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
    });

    it('triggers update with ctrl+enter', () => {
      findMarkdownEditor().vm.$emit('keydown', {
        type: 'keydown',
        keyCode: 13,
        ctrlKey: true,
      });

      expect(eventHub.$emit).toHaveBeenCalledWith('update.issuable');
    });

    it('emits input event when MarkdownEditor emits input event', () => {
      const markdown = 'markdown';

      findMarkdownEditor().vm.$emit('input', markdown);

      expect(wrapper.emitted('input')).toEqual([[markdown]]);
    });
  });
});