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

environment_flux_resource_selector_spec.js « environments « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba3375c731f70b4cd9323ffe6ed69619e5cf326b (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
import { GlCollapsibleListbox, GlAlert } from '@gitlab/ui';
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import { s__ } from '~/locale';
import EnvironmentFluxResourceSelector from '~/environments/components/environment_flux_resource_selector.vue';
import createMockApollo from '../__helpers__/mock_apollo_helper';
import { mockKasTunnelUrl } from './mock_data';

const configuration = {
  basePath: mockKasTunnelUrl.replace(/\/$/, ''),
  baseOptions: {
    headers: {
      'GitLab-Agent-Id': 1,
    },
    withCredentials: true,
  },
};
const namespace = 'my-namespace';

const DEFAULT_PROPS = {
  configuration,
  namespace,
  fluxResourcePath: '',
};

describe('~/environments/components/form.vue', () => {
  let wrapper;

  const kustomizationItem = {
    apiVersion: 'kustomize.toolkit.fluxcd.io/v1beta1',
    metadata: { name: 'kustomization', namespace },
  };
  const helmReleaseItem = {
    apiVersion: 'helm.toolkit.fluxcd.io/v2beta1',
    metadata: { name: 'helm-release', namespace },
  };

  const getKustomizationsQueryResult = jest.fn().mockReturnValue([kustomizationItem]);

  const getHelmReleasesQueryResult = jest.fn().mockReturnValue([helmReleaseItem]);

  const createWrapper = ({
    propsData = {},
    kustomizationsQueryResult = null,
    helmReleasesQueryResult = null,
  } = {}) => {
    Vue.use(VueApollo);

    const mockResolvers = {
      Query: {
        fluxKustomizations: kustomizationsQueryResult || getKustomizationsQueryResult,
        fluxHelmReleases: helmReleasesQueryResult || getHelmReleasesQueryResult,
      },
    };

    return shallowMount(EnvironmentFluxResourceSelector, {
      propsData: {
        ...DEFAULT_PROPS,
        ...propsData,
      },
      apolloProvider: createMockApollo([], mockResolvers),
    });
  };

  const findFluxResourceSelector = () => wrapper.findComponent(GlCollapsibleListbox);
  const findAlert = () => wrapper.findComponent(GlAlert);

  describe('default', () => {
    const kustomizationValue = `${kustomizationItem.apiVersion}/namespaces/${kustomizationItem.metadata.namespace}/kustomizations/${kustomizationItem.metadata.name}`;
    const helmReleaseValue = `${helmReleaseItem.apiVersion}/namespaces/${helmReleaseItem.metadata.namespace}/helmreleases/${helmReleaseItem.metadata.name}`;

    beforeEach(() => {
      wrapper = createWrapper();
    });

    it('renders flux resource selector', () => {
      expect(findFluxResourceSelector().exists()).toBe(true);
    });

    it('requests the flux resources', async () => {
      await waitForPromises();

      expect(getKustomizationsQueryResult).toHaveBeenCalled();
      expect(getHelmReleasesQueryResult).toHaveBeenCalled();
    });

    it('sets the loading prop while fetching the list', async () => {
      expect(findFluxResourceSelector().props('loading')).toBe(true);

      await waitForPromises();

      expect(findFluxResourceSelector().props('loading')).toBe(false);
    });

    it('renders a list of available flux resources', async () => {
      await waitForPromises();

      expect(findFluxResourceSelector().props('items')).toEqual([
        {
          text: s__('Environments|Kustomizations'),
          options: [{ value: kustomizationValue, text: kustomizationItem.metadata.name }],
        },
        {
          text: s__('Environments|HelmReleases'),
          options: [{ value: helmReleaseValue, text: helmReleaseItem.metadata.name }],
        },
      ]);
    });

    it('filters the flux resources list on user search', async () => {
      await waitForPromises();
      findFluxResourceSelector().vm.$emit('search', 'kustomization');
      await nextTick();

      expect(findFluxResourceSelector().props('items')).toEqual([
        {
          text: s__('Environments|Kustomizations'),
          options: [{ value: kustomizationValue, text: kustomizationItem.metadata.name }],
        },
      ]);
    });

    it('emits changes to the fluxResourcePath', () => {
      findFluxResourceSelector().vm.$emit('select', kustomizationValue);

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

  describe('when environment has an associated flux resource path', () => {
    beforeEach(() => {
      wrapper = createWrapper({
        propsData: { fluxResourcePath: 'path/to/flux/resource/name/default' },
      });
    });

    it('updates flux resource selector with the name of the associated flux resource', () => {
      expect(findFluxResourceSelector().props('toggleText')).toBe('default');
    });
  });

  describe('on error', () => {
    const error = new Error('Error from the cluster_client API');

    it('renders an alert with both resource types mentioned when both queries failed', async () => {
      wrapper = createWrapper({
        kustomizationsQueryResult: jest.fn().mockRejectedValueOnce(error),
        helmReleasesQueryResult: jest.fn().mockRejectedValueOnce(error),
      });
      await waitForPromises();

      expect(findAlert().text()).toContain(
        s__(
          'Environments|Unable to access the following resources from this environment. Check your authorization on the following and try again',
        ),
      );
      expect(findAlert().text()).toContain('Kustomization');
      expect(findAlert().text()).toContain('HelmRelease');
    });

    it('renders an alert with only failed resource type mentioned when one query failed', async () => {
      wrapper = createWrapper({
        kustomizationsQueryResult: jest.fn().mockRejectedValueOnce(error),
      });
      await waitForPromises();

      expect(findAlert().text()).toContain(
        s__(
          'Environments|Unable to access the following resources from this environment. Check your authorization on the following and try again',
        ),
      );
      expect(findAlert().text()).toContain('Kustomization');
      expect(findAlert().text()).not.toContain('HelmRelease');
    });
  });
});