Welcome to mirror list, hosted at ThFree Co, Russian Federation.

listbox_input_spec.js « listbox_input « components « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb7262b15e300dea4bf048b05a3be01cda4be178 (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
121
122
123
124
125
126
127
128
129
130
131
132
import { shallowMount } from '@vue/test-utils';
import { GlListbox } from '@gitlab/ui';
import ListboxInput from '~/vue_shared/components/listbox_input/listbox_input.vue';

describe('ListboxInput', () => {
  let wrapper;

  // Props
  const name = 'name';
  const defaultToggleText = 'defaultToggleText';
  const items = [
    {
      text: 'Group 1',
      options: [
        { text: 'Item 1', value: '1' },
        { text: 'Item 2', value: '2' },
      ],
    },
    {
      text: 'Group 2',
      options: [{ text: 'Item 3', value: '3' }],
    },
  ];

  // Finders
  const findGlListbox = () => wrapper.findComponent(GlListbox);
  const findInput = () => wrapper.find('input');

  const createComponent = (propsData) => {
    wrapper = shallowMount(ListboxInput, {
      propsData: {
        name,
        defaultToggleText,
        items,
        ...propsData,
      },
    });
  };

  describe('input attributes', () => {
    beforeEach(() => {
      createComponent();
    });

    it('sets the input name', () => {
      expect(findInput().attributes('name')).toBe(name);
    });
  });

  describe('toggle text', () => {
    it('uses the default toggle text while no value is selected', () => {
      createComponent();

      expect(findGlListbox().props('toggleText')).toBe(defaultToggleText);
    });

    it("uses the selected option's text as the toggle text", () => {
      const selectedOption = items[0].options[0];
      createComponent({ selected: selectedOption.value });

      expect(findGlListbox().props('toggleText')).toBe(selectedOption.text);
    });
  });

  describe('input value', () => {
    const selectedOption = items[0].options[0];

    beforeEach(() => {
      createComponent({ selected: selectedOption.value });
      jest.spyOn(findInput().element, 'dispatchEvent');
    });

    it("sets the listbox's and input's values", () => {
      const { value } = selectedOption;

      expect(findGlListbox().props('selected')).toBe(value);
      expect(findInput().attributes('value')).toBe(value);
    });

    describe("when the listbox's value changes", () => {
      const newSelectedOption = items[1].options[0];

      beforeEach(() => {
        findGlListbox().vm.$emit('select', newSelectedOption.value);
      });

      it('emits the `select` event', () => {
        expect(wrapper.emitted('select')).toEqual([[newSelectedOption.value]]);
      });
    });
  });

  describe('search', () => {
    beforeEach(() => {
      createComponent();
    });

    it('passes all items to GlListbox by default', () => {
      createComponent();
      expect(findGlListbox().props('items')).toStrictEqual(items);
    });

    describe('with groups', () => {
      beforeEach(() => {
        createComponent();
        findGlListbox().vm.$emit('search', '1');
      });

      it('passes only the items that match the search string', async () => {
        expect(findGlListbox().props('items')).toStrictEqual([
          {
            text: 'Group 1',
            options: [{ text: 'Item 1', value: '1' }],
          },
        ]);
      });
    });

    describe('with flat items', () => {
      beforeEach(() => {
        createComponent({
          items: items[0].options,
        });
        findGlListbox().vm.$emit('search', '1');
      });

      it('passes only the items that match the search string', async () => {
        expect(findGlListbox().props('items')).toStrictEqual([{ text: 'Item 1', value: '1' }]);
      });
    });
  });
});