summaryrefslogtreecommitdiff
path: root/spec/frontend/issues/show/components/fields/description_template_spec.js
blob: 79a3bfa9840285042822bfee6260c42c3626cc0b (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
import { shallowMount } from '@vue/test-utils';
import descriptionTemplate from '~/issues/show/components/fields/description_template.vue';

describe('Issue description template component with templates as hash', () => {
  let wrapper;
  const defaultOptions = {
    propsData: {
      value: 'test',
      issuableTemplates: {
        test: [{ name: 'test', id: 'test', project_path: '/', namespace_path: '/' }],
      },
      projectId: 1,
      projectPath: '/',
      namespacePath: '/',
      projectNamespace: '/',
    },
  };

  const findIssuableSelector = () => wrapper.find('.js-issuable-selector');

  const createComponent = (options = defaultOptions) => {
    wrapper = shallowMount(descriptionTemplate, options);
  };

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

  it('renders templates as JSON hash in data attribute', () => {
    createComponent();
    expect(findIssuableSelector().attributes('data-data')).toBe(
      '{"test":[{"name":"test","id":"test","project_path":"/","namespace_path":"/"}]}',
    );
  });

  it('emits input event', () => {
    createComponent();
    wrapper.vm.issuableTemplate.editor.setValue('test new template');

    expect(wrapper.emitted('input')).toEqual([['test new template']]);
  });

  it('returns value with editor getValue', () => {
    createComponent();
    expect(wrapper.vm.issuableTemplate.editor.getValue()).toBe('test');
  });

  describe('Issue description template component with templates as array', () => {
    it('renders templates as JSON array in data attribute', () => {
      createComponent({
        propsData: {
          value: 'test',
          issuableTemplates: [{ name: 'test', id: 'test', project_path: '/', namespace_path: '/' }],
          projectId: 1,
          projectPath: '/',
          namespacePath: '/',
          projectNamespace: '/',
        },
      });
      expect(findIssuableSelector().attributes('data-data')).toBe(
        '[{"name":"test","id":"test","project_path":"/","namespace_path":"/"}]',
      );
    });
  });
});