summaryrefslogtreecommitdiff
path: root/spec/frontend/groups/components/visibility_level_dropdown_spec.js
blob: bf9508dc768d08bfb1995b348b0b4f9bf94b58f8 (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
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Component from '~/groups/components/visibility_level_dropdown.vue';

describe('Visibility Level Dropdown', () => {
  let wrapper;

  const options = [
    { level: 0, label: 'Private', description: 'Private description' },
    { level: 20, label: 'Public', description: 'Public description' },
  ];
  const defaultLevel = 0;

  const createComponent = propsData => {
    wrapper = shallowMount(Component, {
      propsData,
    });
  };

  beforeEach(() => {
    createComponent({
      visibilityLevelOptions: options,
      defaultLevel,
    });
  });

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

  const hiddenInputValue = () =>
    wrapper.find("input[name='group[visibility_level]']").attributes('value');
  const dropdownText = () => wrapper.find(GlDropdown).props('text');
  const findDropdownItems = () =>
    wrapper.findAll(GlDropdownItem).wrappers.map(option => ({
      text: option.text(),
      secondaryText: option.props('secondaryText'),
    }));

  describe('Default values', () => {
    it('sets the value of the hidden input to the default value', () => {
      expect(hiddenInputValue()).toBe(options[0].level.toString());
    });

    it('sets the text of the dropdown to the default value', () => {
      expect(dropdownText()).toBe(options[0].label);
    });

    it('shows all dropdown options', () => {
      expect(findDropdownItems()).toEqual(
        options.map(({ label, description }) => ({ text: label, secondaryText: description })),
      );
    });
  });

  describe('Selecting an option', () => {
    beforeEach(() => {
      wrapper
        .findAll(GlDropdownItem)
        .at(1)
        .vm.$emit('click');
    });

    it('sets the value of the hidden input to the selected value', () => {
      expect(hiddenInputValue()).toBe(options[1].level.toString());
    });

    it('sets the text of the dropdown to the selected value', () => {
      expect(dropdownText()).toBe(options[1].label);
    });
  });
});