summaryrefslogtreecommitdiff
path: root/spec/frontend/feature_flags/components/new_environments_dropdown_spec.js
blob: 1c0c444c296e8e806fbf23919b21c93d4a11d342 (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
import { GlLoadingIcon, GlSearchBoxByType, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import { nextTick } from 'vue';
import NewEnvironmentsDropdown from '~/feature_flags/components/new_environments_dropdown.vue';
import axios from '~/lib/utils/axios_utils';
import httpStatusCodes from '~/lib/utils/http_status';

const TEST_HOST = '/test';
const TEST_SEARCH = 'production';

describe('New Environments Dropdown', () => {
  let wrapper;
  let axiosMock;

  beforeEach(() => {
    axiosMock = new MockAdapter(axios);
    wrapper = shallowMount(NewEnvironmentsDropdown, {
      provide: { environmentsEndpoint: TEST_HOST },
    });
  });

  afterEach(() => {
    axiosMock.restore();
    if (wrapper) {
      wrapper.destroy();
      wrapper = null;
    }
  });

  describe('before results', () => {
    it('should show a loading icon', () => {
      axiosMock.onGet(TEST_HOST).reply(() => {
        expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
      });
      wrapper.findComponent(GlSearchBoxByType).vm.$emit('focus');
      return axios.waitForAll();
    });

    it('should not show any dropdown items', () => {
      axiosMock.onGet(TEST_HOST).reply(() => {
        expect(wrapper.findAllComponents(GlDropdownItem)).toHaveLength(0);
      });
      wrapper.findComponent(GlSearchBoxByType).vm.$emit('focus');
      return axios.waitForAll();
    });
  });

  describe('with empty results', () => {
    let item;
    beforeEach(async () => {
      axiosMock.onGet(TEST_HOST).reply(200, []);
      wrapper.findComponent(GlSearchBoxByType).vm.$emit('focus');
      wrapper.findComponent(GlSearchBoxByType).vm.$emit('input', TEST_SEARCH);
      await axios.waitForAll();
      await nextTick();
      item = wrapper.findComponent(GlDropdownItem);
    });

    it('should display a Create item label', () => {
      expect(item.text()).toBe('Create production');
    });

    it('should display that no matching items are found', () => {
      expect(wrapper.findComponent({ ref: 'noResults' }).exists()).toBe(true);
    });

    it('should emit a new scope when selected', () => {
      item.vm.$emit('click');
      expect(wrapper.emitted('add')).toEqual([[TEST_SEARCH]]);
    });
  });

  describe('with results', () => {
    let items;
    beforeEach(() => {
      axiosMock.onGet(TEST_HOST).reply(httpStatusCodes.OK, ['prod', 'production']);
      wrapper.findComponent(GlSearchBoxByType).vm.$emit('focus');
      wrapper.findComponent(GlSearchBoxByType).vm.$emit('input', 'prod');
      return axios.waitForAll().then(() => {
        items = wrapper.findAllComponents(GlDropdownItem);
      });
    });

    it('should display one item per result', () => {
      expect(items).toHaveLength(2);
    });

    it('should emit an add if an item is clicked', () => {
      items.at(0).vm.$emit('click');
      expect(wrapper.emitted('add')).toEqual([['prod']]);
    });

    it('should not display a create label', () => {
      items = items.filter((i) => i.text().startsWith('Create'));
      expect(items).toHaveLength(0);
    });

    it('should not display a message about no results', () => {
      expect(wrapper.findComponent({ ref: 'noResults' }).exists()).toBe(false);
    });
  });
});