summaryrefslogtreecommitdiff
path: root/spec/frontend/milestones/project_milestone_combobox_spec.js
blob: a7321d2155955a8339951125368279c1ba1bc941 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { milestones as projectMilestones } from './mock_data';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { shallowMount } from '@vue/test-utils';
import MilestoneCombobox from '~/milestones/project_milestone_combobox.vue';
import { GlNewDropdown, GlLoadingIcon, GlSearchBoxByType } from '@gitlab/ui';

const TEST_SEARCH_ENDPOINT = '/api/v4/projects/8/search';

const extraLinks = [
  { text: 'Create new', url: 'http://127.0.0.1:3000/h5bp/html5-boilerplate/-/milestones/new' },
  { text: 'Manage milestones', url: '/h5bp/html5-boilerplate/-/milestones' },
];

const preselectedMilestones = [];
const projectId = '8';

describe('Milestone selector', () => {
  let wrapper;
  let mock;

  const findNoResultsMessage = () => wrapper.find({ ref: 'noResults' });

  const factory = (options = {}) => {
    wrapper = shallowMount(MilestoneCombobox, {
      ...options,
    });
  };

  beforeEach(() => {
    mock = new MockAdapter(axios);
    gon.api_version = 'v4';

    mock.onGet('/api/v4/projects/8/milestones').reply(200, projectMilestones);

    factory({
      propsData: {
        projectId,
        preselectedMilestones,
        extraLinks,
      },
    });
  });

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

  it('renders the dropdown', () => {
    expect(wrapper.find(GlNewDropdown)).toExist();
  });

  it('renders additional links', () => {
    const links = wrapper.findAll('[href]');
    links.wrappers.forEach((item, idx) => {
      expect(item.text()).toBe(extraLinks[idx].text);
      expect(item.attributes('href')).toBe(extraLinks[idx].url);
    });
  });

  describe('before results', () => {
    it('should show a loading icon', () => {
      const request = mock.onGet(TEST_SEARCH_ENDPOINT, {
        params: { search: 'TEST_SEARCH', scope: 'milestones' },
      });

      expect(wrapper.find(GlLoadingIcon).exists()).toBe(true);

      return wrapper.vm.$nextTick().then(() => {
        request.reply(200, []);
      });
    });

    it('should not show any dropdown items', () => {
      expect(wrapper.findAll('[role="milestone option"]')).toHaveLength(0);
    });

    it('should have "No milestone" as the button text', () => {
      expect(wrapper.find({ ref: 'buttonText' }).text()).toBe('No milestone');
    });
  });

  describe('with empty results', () => {
    beforeEach(() => {
      mock
        .onGet(TEST_SEARCH_ENDPOINT, { params: { search: 'TEST_SEARCH', scope: 'milestones' } })
        .reply(200, []);
      wrapper.find(GlSearchBoxByType).vm.$emit('input', 'TEST_SEARCH');
      return axios.waitForAll();
    });

    it('should display that no matching items are found', () => {
      expect(findNoResultsMessage().exists()).toBe(true);
    });
  });

  describe('with results', () => {
    let items;
    beforeEach(() => {
      mock
        .onGet(TEST_SEARCH_ENDPOINT, { params: { search: 'v0.1', scope: 'milestones' } })
        .reply(200, [
          {
            id: 41,
            iid: 6,
            project_id: 8,
            title: 'v0.1',
            description: '',
            state: 'active',
            created_at: '2020-04-04T01:30:40.051Z',
            updated_at: '2020-04-04T01:30:40.051Z',
            due_date: null,
            start_date: null,
            web_url: 'http://127.0.0.1:3000/h5bp/html5-boilerplate/-/milestones/6',
          },
        ]);
      wrapper.find(GlSearchBoxByType).vm.$emit('input', 'v0.1');
      return axios.waitForAll().then(() => {
        items = wrapper.findAll('[role="milestone option"]');
      });
    });

    it('should display one item per result', () => {
      expect(items).toHaveLength(1);
    });

    it('should emit a change if an item is clicked', () => {
      items.at(0).vm.$emit('click');
      expect(wrapper.emitted().change.length).toBe(1);
      expect(wrapper.emitted().change[0]).toEqual([[{ title: 'v0.1' }]]);
    });

    it('should not have a selecton icon on any item', () => {
      items.wrappers.forEach(item => {
        expect(item.find('.selected-item').exists()).toBe(false);
      });
    });

    it('should have a selecton icon if an item is clicked', () => {
      items.at(0).vm.$emit('click');
      expect(wrapper.find('.selected-item').exists()).toBe(true);
    });

    it('should not display a message about no results', () => {
      expect(findNoResultsMessage().exists()).toBe(false);
    });
  });
});