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

dependency_proxy_settings_spec.js « components « group « settings « packages_and_registries « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6c1d212b516989f23fbb26f0c4ae9f1e3d04cba (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { GlSprintf, GlToggle } from '@gitlab/ui';
import { createLocalVue } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';

import component from '~/packages_and_registries/settings/group/components/dependency_proxy_settings.vue';
import {
  DEPENDENCY_PROXY_HEADER,
  DEPENDENCY_PROXY_SETTINGS_DESCRIPTION,
  DEPENDENCY_PROXY_DOCS_PATH,
} from '~/packages_and_registries/settings/group/constants';

import updateDependencyProxySettings from '~/packages_and_registries/settings/group/graphql/mutations/update_dependency_proxy_settings.mutation.graphql';
import updateDependencyProxyImageTtlGroupPolicy from '~/packages_and_registries/settings/group/graphql/mutations/update_dependency_proxy_image_ttl_group_policy.mutation.graphql';
import getGroupPackagesSettingsQuery from '~/packages_and_registries/settings/group/graphql/queries/get_group_packages_settings.query.graphql';
import SettingsBlock from '~/vue_shared/components/settings/settings_block.vue';
import SettingsTitles from '~/packages_and_registries/settings/group/components/settings_titles.vue';
import {
  updateGroupDependencyProxySettingsOptimisticResponse,
  updateDependencyProxyImageTtlGroupPolicyOptimisticResponse,
} from '~/packages_and_registries/settings/group/graphql/utils/optimistic_responses';
import {
  dependencyProxySettings as dependencyProxySettingsMock,
  dependencyProxyImageTtlPolicy as dependencyProxyImageTtlPolicyMock,
  dependencyProxySettingMutationMock,
  groupPackageSettingsMock,
  mutationErrorMock,
  dependencyProxyUpdateTllPolicyMutationMock,
} from '../mock_data';

jest.mock('~/flash');
jest.mock('~/packages_and_registries/settings/group/graphql/utils/optimistic_responses');

const localVue = createLocalVue();

describe('DependencyProxySettings', () => {
  let wrapper;
  let apolloProvider;
  let updateSettingsMutationResolver;
  let updateTtlPoliciesMutationResolver;

  const defaultProvide = {
    defaultExpanded: false,
    groupPath: 'foo_group_path',
    groupDependencyProxyPath: 'group_dependency_proxy_path',
  };

  localVue.use(VueApollo);

  const mountComponent = ({
    provide = defaultProvide,
    isLoading = false,
    dependencyProxySettings = dependencyProxySettingsMock(),
    dependencyProxyImageTtlPolicy = dependencyProxyImageTtlPolicyMock(),
  } = {}) => {
    const requestHandlers = [
      [updateDependencyProxySettings, updateSettingsMutationResolver],
      [updateDependencyProxyImageTtlGroupPolicy, updateTtlPoliciesMutationResolver],
    ];

    apolloProvider = createMockApollo(requestHandlers);

    wrapper = shallowMountExtended(component, {
      localVue,
      apolloProvider,
      provide,
      propsData: {
        dependencyProxySettings,
        dependencyProxyImageTtlPolicy,
        isLoading,
      },
      stubs: {
        GlSprintf,
        GlToggle,
        SettingsBlock,
      },
    });
  };

  beforeEach(() => {
    updateSettingsMutationResolver = jest
      .fn()
      .mockResolvedValue(dependencyProxySettingMutationMock());
    updateTtlPoliciesMutationResolver = jest
      .fn()
      .mockResolvedValue(dependencyProxyUpdateTllPolicyMutationMock());
  });

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

  const findSettingsBlock = () => wrapper.findComponent(SettingsBlock);
  const findSettingsTitles = () => wrapper.findComponent(SettingsTitles);
  const findDescription = () => wrapper.findByTestId('description');
  const findDescriptionLink = () => wrapper.findByTestId('description-link');
  const findEnableProxyToggle = () => wrapper.findByTestId('dependency-proxy-setting-toggle');
  const findEnableTtlPoliciesToggle = () =>
    wrapper.findByTestId('dependency-proxy-ttl-policies-toggle');
  const findToggleHelpLink = () => wrapper.findByTestId('toggle-help-link');

  const fillApolloCache = () => {
    apolloProvider.defaultClient.cache.writeQuery({
      query: getGroupPackagesSettingsQuery,
      variables: {
        fullPath: defaultProvide.groupPath,
      },
      ...groupPackageSettingsMock,
    });
  };

  it('renders a settings block', () => {
    mountComponent();

    expect(findSettingsBlock().exists()).toBe(true);
  });

  it('passes the correct props to settings block', () => {
    mountComponent();

    expect(findSettingsBlock().props('defaultExpanded')).toBe(false);
  });

  it('has the correct header text', () => {
    mountComponent();

    expect(wrapper.text()).toContain(DEPENDENCY_PROXY_HEADER);
  });

  it('has the correct description text', () => {
    mountComponent();

    expect(findDescription().text()).toMatchInterpolatedText(DEPENDENCY_PROXY_SETTINGS_DESCRIPTION);
  });

  it('has the correct link', () => {
    mountComponent();

    expect(findDescriptionLink().attributes()).toMatchObject({
      href: DEPENDENCY_PROXY_DOCS_PATH,
    });
    expect(findDescriptionLink().text()).toBe('Learn more');
  });

  describe('enable toggle', () => {
    it('exists', () => {
      mountComponent();

      expect(findEnableProxyToggle().props()).toMatchObject({
        label: component.i18n.enabledProxyLabel,
      });
    });

    describe('when enabled', () => {
      beforeEach(() => {
        mountComponent();
      });

      it('has the help prop correctly set', () => {
        expect(findEnableProxyToggle().props()).toMatchObject({
          help: component.i18n.enabledProxyHelpText,
        });
      });

      it('has help text with a link', () => {
        expect(findEnableProxyToggle().text()).toContain(
          'To see the image prefix and what is in the cache, visit the Dependency Proxy',
        );
        expect(findToggleHelpLink().attributes()).toMatchObject({
          href: defaultProvide.groupDependencyProxyPath,
        });
      });
    });

    describe('when disabled', () => {
      beforeEach(() => {
        mountComponent({
          dependencyProxySettings: dependencyProxySettingsMock({ enabled: false }),
        });
      });

      it('has the help prop set to empty', () => {
        expect(findEnableProxyToggle().props()).toMatchObject({
          help: '',
        });
      });

      it('the help text is not visible', () => {
        expect(findToggleHelpLink().exists()).toBe(false);
      });
    });
  });

  describe('storage settings', () => {
    it('the component has the settings title', () => {
      mountComponent();

      expect(findSettingsTitles().props()).toMatchObject({
        title: component.i18n.storageSettingsTitle,
      });
    });

    describe('enable proxy ttl policies', () => {
      it('exists', () => {
        mountComponent();

        expect(findEnableTtlPoliciesToggle().props()).toMatchObject({
          label: component.i18n.ttlPolicyEnabledLabel,
          help: component.i18n.ttlPolicyEnabledHelpText,
        });
      });
    });
  });

  describe.each`
    toggleName               | toggleFinder                   | localErrorMock                                | optimisticResponse
    ${'enable proxy'}        | ${findEnableProxyToggle}       | ${dependencyProxySettingMutationMock}         | ${updateGroupDependencyProxySettingsOptimisticResponse}
    ${'enable ttl policies'} | ${findEnableTtlPoliciesToggle} | ${dependencyProxyUpdateTllPolicyMutationMock} | ${updateDependencyProxyImageTtlGroupPolicyOptimisticResponse}
  `('$toggleName settings update ', ({ optimisticResponse, toggleFinder, localErrorMock }) => {
    describe('success state', () => {
      it('emits a success event', async () => {
        mountComponent();

        fillApolloCache();
        toggleFinder().vm.$emit('change', false);

        await waitForPromises();

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

      it('has an optimistic response', () => {
        mountComponent();

        fillApolloCache();

        expect(toggleFinder().props('value')).toBe(true);

        toggleFinder().vm.$emit('change', false);

        expect(optimisticResponse).toHaveBeenCalledWith(
          expect.objectContaining({
            enabled: false,
          }),
        );
      });
    });

    describe('errors', () => {
      it('mutation payload with root level errors', async () => {
        updateSettingsMutationResolver = jest.fn().mockResolvedValue(mutationErrorMock);
        updateTtlPoliciesMutationResolver = jest.fn().mockResolvedValue(mutationErrorMock);

        mountComponent();

        fillApolloCache();

        toggleFinder().vm.$emit('change', false);

        await waitForPromises();

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

      it.each`
        type         | mutationResolverMock
        ${'local'}   | ${jest.fn().mockResolvedValue(localErrorMock({ errors: ['foo'] }))}
        ${'network'} | ${jest.fn().mockRejectedValue()}
      `('mutation payload with $type error', async ({ mutationResolverMock }) => {
        updateSettingsMutationResolver = mutationResolverMock;
        updateTtlPoliciesMutationResolver = mutationResolverMock;
        mountComponent();

        fillApolloCache();
        toggleFinder().vm.$emit('change', false);

        await waitForPromises();

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

  describe('when isLoading is true', () => {
    it('disables enable proxy toggle', () => {
      mountComponent({ isLoading: true });

      expect(findEnableProxyToggle().props('disabled')).toBe(true);
    });

    it('disables enable ttl policies toggle', () => {
      mountComponent({ isLoading: true });

      expect(findEnableTtlPoliciesToggle().props('disabled')).toBe(true);
    });
  });
});