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

custom_metrics_form_fields_spec.js « components « custom_metrics « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2001f5c144142159f1c009023cc3136b0a9af817 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { mount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { TEST_HOST } from 'helpers/test_constants';
import CustomMetricsFormFields from '~/custom_metrics/components/custom_metrics_form_fields.vue';
import axios from '~/lib/utils/axios_utils';

describe('custom metrics form fields component', () => {
  let wrapper;
  let mockAxios;

  const getNamedInput = (name) => wrapper.element.querySelector(`input[name="${name}"]`);
  const validateQueryPath = `${TEST_HOST}/mock/path`;
  const validQueryResponse = { success: true, query: { valid: true, error: '' } };
  const csrfToken = 'mockToken';
  const formOperation = 'post';
  const makeFormData = (data = {}) => ({
    formData: {
      title: '',
      yLabel: '',
      query: '',
      unit: '',
      group: '',
      legend: '',
      ...data,
    },
  });
  const mountComponent = (props) => {
    wrapper = mount(CustomMetricsFormFields, {
      propsData: {
        formOperation,
        validateQueryPath,
        ...props,
      },
      csrfToken,
    });
  };

  beforeEach(() => {
    mockAxios = new MockAdapter(axios);
  });

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

  it('checks form validity', async () => {
    mockAxios.onPost(validateQueryPath).reply(200, validQueryResponse);
    mountComponent({
      metricPersisted: true,
      ...makeFormData({
        title: 'title-old',
        yLabel: 'yLabel',
        unit: 'unit',
        group: 'group',
      }),
    });

    wrapper.find(`input[name="prometheus_metric[query]"]`).setValue('query');
    await axios.waitForAll();

    expect(wrapper.emitted('formValidation')).toStrictEqual([[true]]);
  });

  describe('hidden inputs', () => {
    beforeEach(() => {
      mountComponent();
    });

    it('specifies form operation _method', () => {
      expect(getNamedInput('_method', 'input').value).toBe('post');
    });

    it('specifies authenticity token', () => {
      expect(getNamedInput('authenticity_token', 'input').value).toBe(csrfToken);
    });
  });

  describe('name input', () => {
    const name = 'prometheus_metric[title]';

    it('is empty by default', () => {
      mountComponent();

      expect(getNamedInput(name).value).toBe('');
    });

    it('receives a persisted value', () => {
      const title = 'mockTitle';
      mountComponent(makeFormData({ title }));

      expect(getNamedInput(name).value).toBe(title);
    });
  });

  describe('group input', () => {
    it('has a default value', () => {
      mountComponent();

      expect(getNamedInput('prometheus_metric[group]', 'glformradiogroup-stub').value).toBe(
        'business',
      );
    });
  });

  describe('query input', () => {
    const queryInputName = 'prometheus_metric[query]';

    it('is empty by default', () => {
      mountComponent();

      expect(getNamedInput(queryInputName).value).toBe('');
    });

    it('receives and validates a persisted value', () => {
      const query = 'persistedQuery';
      jest.spyOn(axios, 'post');

      mountComponent({ metricPersisted: true, ...makeFormData({ query }) });

      expect(axios.post).toHaveBeenCalledWith(
        validateQueryPath,
        { query },
        expect.objectContaining({ cancelToken: expect.anything() }),
      );
      expect(getNamedInput(queryInputName).value).toBe(query);
      jest.runAllTimers();
    });

    it('checks validity on user input', async () => {
      const query = 'changedQuery';
      mountComponent();

      expect(mockAxios.history.post).toHaveLength(0);
      const queryInput = wrapper.find(`input[name="${queryInputName}"]`);
      queryInput.setValue(query);

      await axios.waitForAll();
      expect(mockAxios.history.post).toHaveLength(1);
    });

    describe('when query validation is in flight', () => {
      beforeEach(() => {
        mountComponent({ metricPersisted: true, ...makeFormData({ query: 'validQuery' }) });
        mockAxios.onPost(validateQueryPath).reply(200, validQueryResponse);
      });

      it('expect loading message to display', async () => {
        const queryInput = wrapper.find(`input[name="${queryInputName}"]`);
        queryInput.setValue('query');

        expect(wrapper.text()).toContain('Validating query');
      });

      it('expect loading message to disappear', async () => {
        const queryInput = wrapper.find(`input[name="${queryInputName}"]`);
        queryInput.setValue('query');

        await axios.waitForAll();
        expect(wrapper.text()).not.toContain('Validating query');
      });
    });

    describe('when query is invalid', () => {
      const errorMessage = 'mockErrorMessage';
      const invalidQueryResponse = { success: true, query: { valid: false, error: errorMessage } };

      beforeEach(() => {
        mockAxios.onPost(validateQueryPath).reply(200, invalidQueryResponse);
        mountComponent({ metricPersisted: true, ...makeFormData({ query: 'invalidQuery' }) });
        return axios.waitForAll();
      });

      it('shows invalid query message', async () => {
        expect(wrapper.text()).toContain(errorMessage);
      });
    });

    describe('when query is valid', () => {
      beforeEach(() => {
        mockAxios.onPost(validateQueryPath).reply(200, validQueryResponse);
        mountComponent({ metricPersisted: true, ...makeFormData({ query: 'validQuery' }) });
      });

      it('shows valid query message', async () => {
        await axios.waitForAll();

        expect(wrapper.text()).toContain('PromQL query is valid');
      });
    });
  });

  describe('yLabel input', () => {
    const name = 'prometheus_metric[y_label]';

    it('is empty by default', () => {
      mountComponent();

      expect(getNamedInput(name).value).toBe('');
    });

    it('receives a persisted value', () => {
      const yLabel = 'mockYLabel';
      mountComponent(makeFormData({ yLabel }));

      expect(getNamedInput(name).value).toBe(yLabel);
    });
  });

  describe('unit input', () => {
    const name = 'prometheus_metric[unit]';

    it('is empty by default', () => {
      mountComponent();

      expect(getNamedInput(name).value).toBe('');
    });

    it('receives a persisted value', () => {
      const unit = 'mockUnit';
      mountComponent(makeFormData({ unit }));

      expect(getNamedInput(name).value).toBe(unit);
    });
  });

  describe('legend input', () => {
    const name = 'prometheus_metric[legend]';

    it('is empty by default', () => {
      mountComponent();

      expect(getNamedInput(name).value).toBe('');
    });

    it('receives a persisted value', () => {
      const legend = 'mockLegend';
      mountComponent(makeFormData({ legend }));

      expect(getNamedInput(name).value).toBe(legend);
    });
  });
});