summaryrefslogtreecommitdiff
path: root/spec/frontend/ci/ci_variable_list/components/ci_environments_dropdown_spec.js
blob: 2fd395a12304c3ad0d7e2e0c284460eb213be5d4 (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
import { GlListboxItem, GlCollapsibleListbox, GlDropdownItem, GlIcon } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import { allEnvironments } from '~/ci/ci_variable_list/constants';
import CiEnvironmentsDropdown from '~/ci/ci_variable_list/components/ci_environments_dropdown.vue';

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

  const envs = ['dev', 'prod', 'staging'];
  const defaultProps = { environments: envs, selectedEnvironmentScope: '' };

  const findAllListboxItems = () => wrapper.findAllComponents(GlListboxItem);
  const findListboxItemByIndex = (index) => wrapper.findAllComponents(GlListboxItem).at(index);
  const findActiveIconByIndex = (index) => findListboxItemByIndex(index).findComponent(GlIcon);
  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);
  const findListboxText = () => findListbox().props('toggleText');
  const findCreateWildcardButton = () => wrapper.findComponent(GlDropdownItem);

  const createComponent = ({ props = {}, searchTerm = '' } = {}) => {
    wrapper = mount(CiEnvironmentsDropdown, {
      propsData: {
        ...defaultProps,
        ...props,
      },
    });

    findListbox().vm.$emit('search', searchTerm);
  };

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

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

    it('renders create button with search term if environments do not contain search term', () => {
      const button = findCreateWildcardButton();
      expect(button.exists()).toBe(true);
      expect(button.text()).toBe('Create wildcard: stable');
    });
  });

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

    it('renders all environments when search term is empty', () => {
      expect(findListboxItemByIndex(0).text()).toBe(envs[0]);
      expect(findListboxItemByIndex(1).text()).toBe(envs[1]);
      expect(findListboxItemByIndex(2).text()).toBe(envs[2]);
    });

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

  describe('when `*` is the value of selectedEnvironmentScope props', () => {
    const wildcardScope = '*';

    beforeEach(() => {
      createComponent({ props: { selectedEnvironmentScope: wildcardScope } });
    });

    it('shows the `All environments` text and not the wildcard', () => {
      expect(findListboxText()).toContain(allEnvironments.text);
      expect(findListboxText()).not.toContain(wildcardScope);
    });
  });

  describe('Environments found', () => {
    const currentEnv = envs[2];

    beforeEach(() => {
      createComponent({ searchTerm: currentEnv });
    });

    it('renders only the environment searched for', () => {
      expect(findAllListboxItems()).toHaveLength(1);
      expect(findListboxItemByIndex(0).text()).toBe(currentEnv);
    });

    it('does not display create button', () => {
      expect(findCreateWildcardButton().exists()).toBe(false);
    });

    describe('Custom events', () => {
      describe('when selecting an environment', () => {
        const itemIndex = 0;

        beforeEach(() => {
          createComponent();
        });

        it('emits `select-environment` when an environment is clicked', () => {
          findListbox().vm.$emit('select', envs[itemIndex]);
          expect(wrapper.emitted('select-environment')).toEqual([[envs[itemIndex]]]);
        });
      });

      describe('when creating a new environment from a search term', () => {
        const search = 'new-env';
        beforeEach(() => {
          createComponent({ searchTerm: search });
        });

        it('emits create-environment-scope', () => {
          findCreateWildcardButton().vm.$emit('click');
          expect(wrapper.emitted('create-environment-scope')).toEqual([[search]]);
        });
      });
    });
  });
});