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

environment_namespace_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: 53e4f807751361297dc67b276a137ba99adedf88 (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
import { GlAlert, GlCollapsibleListbox, GlButton } 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 EnvironmentNamespaceSelector from '~/environments/components/environment_namespace_selector.vue';
import { stubComponent } from 'helpers/stub_component';
import createMockApollo from '../__helpers__/mock_apollo_helper';
import { mockKasTunnelUrl } from './mock_data';

const configuration = {
  basePath: mockKasTunnelUrl.replace(/\/$/, ''),
  headers: {
    'GitLab-Agent-Id': 2,
    'Content-Type': 'application/json',
    Accept: 'application/json',
  },
  credentials: 'include',
};

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

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

  const getNamespacesQueryResult = jest
    .fn()
    .mockReturnValue([
      { metadata: { name: 'default' } },
      { metadata: { name: 'agent' } },
      { metadata: { name: 'test-agent' } },
    ]);

  const closeMock = jest.fn();

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

    const mockResolvers = {
      Query: {
        k8sNamespaces: queryResult || getNamespacesQueryResult,
      },
    };

    return shallowMount(EnvironmentNamespaceSelector, {
      propsData: {
        ...DEFAULT_PROPS,
        ...propsData,
      },
      stubs: {
        GlCollapsibleListbox: stubComponent(GlCollapsibleListbox, {
          template: `<div><slot name="footer"></slot></div>`,
          methods: {
            close: closeMock,
          },
        }),
      },
      apolloProvider: createMockApollo([], mockResolvers),
    });
  };

  const findNamespaceSelector = () => wrapper.findComponent(GlCollapsibleListbox);
  const findAlert = () => wrapper.findComponent(GlAlert);
  const findSelectButton = () => wrapper.findComponent(GlButton);

  const searchNamespace = async (searchTerm = 'test') => {
    findNamespaceSelector().vm.$emit('search', searchTerm);
    await nextTick();
  };

  describe('default', () => {
    beforeEach(() => {
      wrapper = createWrapper();
    });

    it('renders namespace selector', () => {
      expect(findNamespaceSelector().exists()).toBe(true);
    });

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

      expect(getNamespacesQueryResult).toHaveBeenCalled();
    });

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

      await waitForPromises();

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

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

      expect(findNamespaceSelector().props('items')).toMatchObject([
        {
          text: 'default',
          value: 'default',
        },
        {
          text: 'agent',
          value: 'agent',
        },
        {
          text: 'test-agent',
          value: 'test-agent',
        },
      ]);
    });

    it('filters the namespaces list on user search', async () => {
      await waitForPromises();
      await searchNamespace('agent');

      expect(findNamespaceSelector().props('items')).toMatchObject([
        {
          text: 'agent',
          value: 'agent',
        },
        {
          text: 'test-agent',
          value: 'test-agent',
        },
      ]);
    });

    it('emits changes to the namespace', () => {
      findNamespaceSelector().vm.$emit('select', 'agent');

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

  describe('custom select button', () => {
    beforeEach(async () => {
      wrapper = createWrapper();
      await waitForPromises();
    });

    it("doesn't render custom select button before searching", () => {
      expect(findSelectButton().exists()).toBe(false);
    });

    it("doesn't render custom select button when the search is found in the namespaces list", async () => {
      await searchNamespace('test-agent');
      expect(findSelectButton().exists()).toBe(false);
    });

    it('renders custom select button when the namespace searched for is not found in the namespaces list', async () => {
      await searchNamespace();
      expect(findSelectButton().exists()).toBe(true);
    });

    it('emits custom filled namespace name to the `change` event', async () => {
      await searchNamespace();
      findSelectButton().vm.$emit('click');

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

    it('closes the listbox after the custom value for the namespace was selected', async () => {
      await searchNamespace();
      findSelectButton().vm.$emit('click');

      expect(closeMock).toHaveBeenCalled();
    });
  });

  describe('when environment has an associated namespace', () => {
    beforeEach(() => {
      wrapper = createWrapper({
        propsData: { namespace: 'existing-namespace' },
      });
    });

    it('updates namespace selector with the name of the associated namespace', () => {
      expect(findNamespaceSelector().props('toggleText')).toBe('existing-namespace');
    });
  });

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

    beforeEach(async () => {
      wrapper = createWrapper({
        queryResult: jest.fn().mockRejectedValueOnce(error),
      });
      await waitForPromises();
    });

    it('renders an alert with the error text', () => {
      expect(findAlert().text()).toContain(error.message);
    });

    it('renders an empty namespace selector', () => {
      expect(findNamespaceSelector().props('items')).toMatchObject([]);
    });

    it('renders custom select button when the user performs search', async () => {
      await searchNamespace();

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

    it('emits custom filled namespace name to the `change` event', async () => {
      await searchNamespace();
      findSelectButton().vm.$emit('click');

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