summaryrefslogtreecommitdiff
path: root/spec/frontend/confidential_merge_request/components/dropdown_spec.js
blob: 4d577fe11325d97d8b5fa76d1973c8360327b05e (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
import { GlCollapsibleListbox } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import Dropdown from '~/confidential_merge_request/components/dropdown.vue';

const TEST_PROJECTS = [
  {
    id: 7,
    name: 'test',
  },
  {
    id: 9,
    name: 'lorem ipsum',
  },
  {
    id: 11,
    name: 'dolar sit',
  },
];

describe('~/confidential_merge_request/components/dropdown.vue', () => {
  let wrapper;

  function factory(props = {}) {
    wrapper = shallowMount(Dropdown, {
      propsData: {
        projects: TEST_PROJECTS,
        ...props,
      },
    });
  }

  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);

  describe('default', () => {
    beforeEach(() => {
      factory();
    });

    it('renders collapsible listbox', () => {
      expect(findListbox().props()).toMatchObject({
        icon: 'lock',
        selected: [],
        toggleText: 'Select private project',
        block: true,
        items: TEST_PROJECTS.map(({ id, name }) => ({
          value: String(id),
          text: name,
        })),
      });
    });

    it('does not emit anything', () => {
      expect(wrapper.emitted()).toEqual({});
    });

    describe('when listbox emits selected', () => {
      beforeEach(() => {
        findListbox().vm.$emit('select', String(TEST_PROJECTS[1].id));
      });

      it('emits selected project', () => {
        expect(wrapper.emitted('select')).toEqual([[TEST_PROJECTS[1]]]);
      });
    });
  });

  describe('with selected', () => {
    beforeEach(() => {
      factory({ selectedProject: TEST_PROJECTS[1] });
    });

    it('shows selected project', () => {
      expect(findListbox().props()).toMatchObject({
        selected: String(TEST_PROJECTS[1].id),
        toggleText: TEST_PROJECTS[1].name,
      });
    });
  });
});