summaryrefslogtreecommitdiff
path: root/spec/frontend/static_site_editor/components/edit_meta_controls_spec.js
blob: 191f91be07603e0542bb8934aadfd647f852cbf6 (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
import { shallowMount } from '@vue/test-utils';

import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import { GlFormInput, GlFormTextarea } from '@gitlab/ui';

import EditMetaControls from '~/static_site_editor/components/edit_meta_controls.vue';

import { mergeRequestMeta } from '../mock_data';

describe('~/static_site_editor/components/edit_meta_controls.vue', () => {
  useLocalStorageSpy();

  let wrapper;
  let mockSelect;
  let mockGlFormInputTitleInstance;
  const { title, description } = mergeRequestMeta;
  const newTitle = 'New title';
  const newDescription = 'New description';

  const buildWrapper = (propsData = {}) => {
    wrapper = shallowMount(EditMetaControls, {
      propsData: {
        title,
        description,
        ...propsData,
      },
    });
  };

  const buildMocks = () => {
    mockSelect = jest.fn();
    mockGlFormInputTitleInstance = { $el: { select: mockSelect } };
    wrapper.vm.$refs.title = mockGlFormInputTitleInstance;
  };

  const findGlFormInputTitle = () => wrapper.find(GlFormInput);
  const findGlFormTextAreaDescription = () => wrapper.find(GlFormTextarea);

  beforeEach(() => {
    buildWrapper();
    buildMocks();

    return wrapper.vm.$nextTick();
  });

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

  it('renders the title input', () => {
    expect(findGlFormInputTitle().exists()).toBe(true);
  });

  it('renders the description input', () => {
    expect(findGlFormTextAreaDescription().exists()).toBe(true);
  });

  it('forwards the title prop to the title input', () => {
    expect(findGlFormInputTitle().attributes().value).toBe(title);
  });

  it('forwards the description prop to the description input', () => {
    expect(findGlFormTextAreaDescription().attributes().value).toBe(description);
  });

  it('calls select on the title input when mounted', () => {
    expect(mockGlFormInputTitleInstance.$el.select).toHaveBeenCalled();
  });

  describe('when inputs change', () => {
    const storageKey = 'sse-merge-request-meta-local-storage-editable';

    afterEach(() => {
      localStorage.removeItem(storageKey);
    });

    it.each`
      findFn                           | key              | value
      ${findGlFormInputTitle}          | ${'title'}       | ${newTitle}
      ${findGlFormTextAreaDescription} | ${'description'} | ${newDescription}
    `('emits updated settings when $findFn input updates', ({ key, value, findFn }) => {
      findFn().vm.$emit('input', value);

      const newSettings = { ...mergeRequestMeta, [key]: value };

      expect(wrapper.emitted('updateSettings')[0][0]).toMatchObject(newSettings);
    });

    it('should remember the input changes', () => {
      findGlFormInputTitle().vm.$emit('input', newTitle);
      findGlFormTextAreaDescription().vm.$emit('input', newDescription);

      const newSettings = { title: newTitle, description: newDescription };

      expect(localStorage.setItem).toHaveBeenCalledWith(storageKey, JSON.stringify(newSettings));
    });
  });
});