summaryrefslogtreecommitdiff
path: root/spec/frontend/ci_variable_list/components/ci_environments_dropdown_spec.js
blob: 5c5ea102f12894edb8c614da1059b69d59123a37 (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
import { GlDropdown, GlDropdownItem, GlIcon } from '@gitlab/ui';
import { mount, createLocalVue } from '@vue/test-utils';
import Vuex from 'vuex';
import CiEnvironmentsDropdown from '~/ci_variable_list/components/ci_environments_dropdown.vue';

const localVue = createLocalVue();
localVue.use(Vuex);

describe('Ci environments dropdown', () => {
  let wrapper;
  let store;

  const enterSearchTerm = (value) =>
    wrapper.find('[data-testid="ci-environment-search"]').setValue(value);

  const createComponent = (term) => {
    store = new Vuex.Store({
      getters: {
        joinedEnvironments: () => ['dev', 'prod', 'staging'],
      },
    });

    wrapper = mount(CiEnvironmentsDropdown, {
      store,
      localVue,
      propsData: {
        value: term,
      },
    });
    enterSearchTerm(term);
  };

  const findAllDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
  const findDropdownItemByIndex = (index) => wrapper.findAllComponents(GlDropdownItem).at(index);
  const findActiveIconByIndex = (index) => findDropdownItemByIndex(index).findComponent(GlIcon);

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

  describe('No environments found', () => {
    beforeEach(() => {
      createComponent('stable');
    });

    it('renders create button with search term if environments do not contain search term', () => {
      expect(findAllDropdownItems()).toHaveLength(2);
      expect(findDropdownItemByIndex(1).text()).toBe('Create wildcard: stable');
    });

    it('renders empty results message', () => {
      expect(findDropdownItemByIndex(0).text()).toBe('No matching results');
    });
  });

  describe('Search term is empty', () => {
    beforeEach(() => {
      createComponent('');
    });

    it('renders all environments when search term is empty', () => {
      expect(findAllDropdownItems()).toHaveLength(3);
      expect(findDropdownItemByIndex(0).text()).toBe('dev');
      expect(findDropdownItemByIndex(1).text()).toBe('prod');
      expect(findDropdownItemByIndex(2).text()).toBe('staging');
    });

    it('should not display active checkmark on the inactive stage', () => {
      expect(findActiveIconByIndex(0).classes('gl-visibility-hidden')).toBe(true);
    });
  });

  describe('Environments found', () => {
    beforeEach(async () => {
      createComponent('prod');
      await wrapper.vm.$nextTick();
    });

    it('renders only the environment searched for', () => {
      expect(findAllDropdownItems()).toHaveLength(1);
      expect(findDropdownItemByIndex(0).text()).toBe('prod');
    });

    it('should not display create button', () => {
      const environments = findAllDropdownItems().filter((env) => env.text().startsWith('Create'));
      expect(environments).toHaveLength(0);
      expect(findAllDropdownItems()).toHaveLength(1);
    });

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

    it('should display active checkmark if active', () => {
      expect(findActiveIconByIndex(0).classes('gl-visibility-hidden')).toBe(false);
    });

    it('should clear the search term when showing the dropdown', () => {
      wrapper.findComponent(GlDropdown).trigger('click');

      expect(wrapper.find('[data-testid="ci-environment-search"]').text()).toBe('');
    });

    describe('Custom events', () => {
      it('should emit selectEnvironment if an environment is clicked', () => {
        findDropdownItemByIndex(0).vm.$emit('click');
        expect(wrapper.emitted('selectEnvironment')).toEqual([['prod']]);
      });

      it('should emit createClicked if an environment is clicked', async () => {
        createComponent('newscope');

        await wrapper.vm.$nextTick();
        findDropdownItemByIndex(1).vm.$emit('click');
        expect(wrapper.emitted('createClicked')).toEqual([['newscope']]);
      });
    });
  });
});