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

editor_tab_spec.js « ui « components « pipeline_editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3a40ce32a24a6ab5d107d81b48840ea51b821245 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { GlAlert, GlBadge, GlTabs } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';
import EditorTab from '~/pipeline_editor/components/ui/editor_tab.vue';

const mockContent1 = 'MOCK CONTENT 1';
const mockContent2 = 'MOCK CONTENT 2';

const MockSourceEditor = {
  template: '<div>EDITOR</div>',
};

describe('~/pipeline_editor/components/ui/editor_tab.vue', () => {
  let wrapper;
  let mockChildMounted = jest.fn();

  const MockChild = {
    props: ['content'],
    template: '<div>{{content}}</div>',
    mounted() {
      mockChildMounted(this.content);
    },
  };

  const MockTabbedContent = {
    components: {
      EditorTab,
      GlTabs,
      MockChild,
    },
    template: `
        <gl-tabs>
          <editor-tab title="Tab 1" :title-link-attributes="{ 'data-testid': 'tab1-btn' }" :lazy="true">
            <mock-child content="${mockContent1}"/>
          </editor-tab>
          <editor-tab title="Tab 2" :title-link-attributes="{ 'data-testid': 'tab2-btn' }" :lazy="true" badge-title="NEW">
            <mock-child content="${mockContent2}"/>
          </editor-tab>
        </gl-tabs>
      `,
  };

  const createMockedWrapper = () => {
    wrapper = mount(MockTabbedContent);
  };

  const createWrapper = ({ props } = {}) => {
    wrapper = mount(EditorTab, {
      propsData: {
        title: 'Tab 1',
        ...props,
      },
      slots: {
        default: MockSourceEditor,
      },
    });
  };

  const findSlotComponent = () => wrapper.findComponent(MockSourceEditor);
  const findAlert = () => wrapper.findComponent(GlAlert);
  const findBadges = () => wrapper.findAll(GlBadge);

  beforeEach(() => {
    mockChildMounted = jest.fn();
  });

  it('tabs are mounted lazily', async () => {
    createMockedWrapper();

    expect(mockChildMounted).toHaveBeenCalledTimes(0);
  });

  it('first tab is only mounted after nextTick', async () => {
    createMockedWrapper();

    await nextTick();

    expect(mockChildMounted).toHaveBeenCalledTimes(1);
    expect(mockChildMounted).toHaveBeenCalledWith(mockContent1);
  });

  describe('alerts', () => {
    describe('unavailable state', () => {
      beforeEach(() => {
        createWrapper({ props: { isUnavailable: true } });
      });

      it('shows the invalid alert when the status is invalid', () => {
        const alert = findAlert();

        expect(alert.exists()).toBe(true);
        expect(alert.text()).toContain(wrapper.vm.$options.i18n.unavailable);
      });
    });

    describe('invalid state', () => {
      beforeEach(() => {
        createWrapper({ props: { isInvalid: true } });
      });

      it('shows the invalid alert when the status is invalid', () => {
        const alert = findAlert();

        expect(alert.exists()).toBe(true);
        expect(alert.text()).toBe(wrapper.vm.$options.i18n.invalid);
      });
    });

    describe('empty state', () => {
      const text = 'my custom alert message';

      beforeEach(() => {
        createWrapper({
          props: { isEmpty: true, emptyMessage: text },
        });
      });

      it('displays an empty message', () => {
        createWrapper({
          props: { isEmpty: true },
        });

        const alert = findAlert();

        expect(alert.exists()).toBe(true);
        expect(alert.text()).toBe(
          'This tab will be usable when the CI/CD configuration file is populated with valid syntax.',
        );
      });

      it('can have a custom empty message', () => {
        const alert = findAlert();

        expect(alert.exists()).toBe(true);
        expect(alert.text()).toBe(text);
      });
    });
  });

  describe('showing the tab content depending on `isEmpty`, `isUnavailable` and `isInvalid`', () => {
    it.each`
      isEmpty      | isUnavailable | isInvalid    | showSlotComponent | text
      ${undefined} | ${undefined}  | ${undefined} | ${true}           | ${'renders'}
      ${false}     | ${false}      | ${false}     | ${true}           | ${'renders'}
      ${undefined} | ${true}       | ${true}      | ${false}          | ${'hides'}
      ${true}      | ${false}      | ${false}     | ${false}          | ${'hides'}
      ${false}     | ${true}       | ${false}     | ${false}          | ${'hides'}
      ${false}     | ${false}      | ${true}      | ${false}          | ${'hides'}
    `(
      '$text the slot component when isEmpty:$isEmpty, isUnavailable:$isUnavailable and isInvalid:$isInvalid',
      ({ isEmpty, isUnavailable, isInvalid, showSlotComponent }) => {
        createWrapper({
          props: { isEmpty, isUnavailable, isInvalid },
        });
        expect(findSlotComponent().exists()).toBe(showSlotComponent);
        expect(findAlert().exists()).toBe(!showSlotComponent);
      },
    );
  });

  describe('user interaction', () => {
    const clickTab = async (testid) => {
      wrapper.find(`[data-testid="${testid}"]`).trigger('click');
      await nextTick();
    };

    beforeEach(() => {
      createMockedWrapper();
    });

    it('mounts a tab once after selecting it', async () => {
      await clickTab('tab2-btn');

      expect(mockChildMounted).toHaveBeenCalledTimes(2);
      expect(mockChildMounted).toHaveBeenNthCalledWith(1, mockContent1);
      expect(mockChildMounted).toHaveBeenNthCalledWith(2, mockContent2);
    });

    it('mounts each tab once after selecting each', async () => {
      await clickTab('tab2-btn');
      await clickTab('tab1-btn');
      await clickTab('tab2-btn');

      expect(mockChildMounted).toHaveBeenCalledTimes(2);
      expect(mockChildMounted).toHaveBeenNthCalledWith(1, mockContent1);
      expect(mockChildMounted).toHaveBeenNthCalledWith(2, mockContent2);
    });
  });

  describe('valid state', () => {
    beforeEach(() => {
      createMockedWrapper();
    });

    it('renders correct number of badges', async () => {
      expect(findBadges()).toHaveLength(1);
      expect(findBadges().at(0).text()).toBe('NEW');
    });
  });
});