summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/namespace_select/namespace_select_spec.js
blob: 8f07f63993dd7002d3e5043fc86b7b7f96353b99 (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
import { GlDropdown, GlDropdownItem, GlDropdownSectionHeader } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import NamespaceSelect, {
  i18n,
} from '~/vue_shared/components/namespace_select/namespace_select.vue';
import { user, group, namespaces } from './mock_data';

describe('Namespace Select', () => {
  let wrapper;

  const createComponent = (props = {}) =>
    shallowMountExtended(NamespaceSelect, {
      propsData: {
        data: namespaces,
        ...props,
      },
    });

  const wrappersText = (arr) => arr.wrappers.map((w) => w.text());
  const flatNamespaces = () => [...group, ...user];
  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findDropdownAttributes = (attr) => findDropdown().attributes(attr);
  const selectedDropdownItemText = () => findDropdownAttributes('text');
  const findDropdownItems = () => wrapper.findAllComponents(GlDropdownItem);
  const findSectionHeaders = () => wrapper.findAllComponents(GlDropdownSectionHeader);

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

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

  it('renders the dropdown', () => {
    expect(findDropdown().exists()).toBe(true);
  });

  it('renders each dropdown item', () => {
    const items = findDropdownItems().wrappers;
    expect(items).toHaveLength(flatNamespaces().length);
  });

  it('renders the human name for each item', () => {
    const dropdownItems = wrappersText(findDropdownItems());
    const flatNames = flatNamespaces().map(({ humanName }) => humanName);
    expect(dropdownItems).toEqual(flatNames);
  });

  it('sets the initial dropdown text', () => {
    expect(selectedDropdownItemText()).toBe(i18n.DEFAULT_TEXT);
  });

  it('splits group and user namespaces', () => {
    const headers = findSectionHeaders();
    expect(headers).toHaveLength(2);
    expect(wrappersText(headers)).toEqual([i18n.GROUPS, i18n.USERS]);
  });

  it('sets the dropdown to full width', () => {
    expect(findDropdownAttributes('block')).toBeUndefined();

    wrapper = createComponent({ fullWidth: true });

    expect(findDropdownAttributes('block')).not.toBeUndefined();
    expect(findDropdownAttributes('block')).toBe('true');
  });

  describe('with a selected namespace', () => {
    const selectedGroupIndex = 1;
    const selectedItem = group[selectedGroupIndex];

    beforeEach(() => {
      findDropdownItems().at(selectedGroupIndex).vm.$emit('click');
    });

    it('sets the dropdown text', () => {
      expect(selectedDropdownItemText()).toBe(selectedItem.humanName);
    });

    it('emits the `select` event when a namespace is selected', () => {
      const args = [selectedItem];
      expect(wrapper.emitted('select')).toEqual([args]);
    });
  });
});