summaryrefslogtreecommitdiff
path: root/spec/javascripts/boards/issue_card_labels_spec.js
blob: 50b1ee18423ca91af1b1c058020521fb5d10eb28 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import Vue from 'vue';
import labelsComponent from '~/boards/components/issue_card_labels';
import eventHub from '~/boards/eventhub';
import '~/boards/stores/boards_store';

const propData = {
  labels: [{
    color: 'rgb(0, 0, 0)',
    textColor: 'rgb(255, 255, 255)',
    description: '',
    id: 1,
    title: 'Frontend',
  }, {
    color: 'rgb(255, 255, 255)',
    textColor: 'rgb(0, 0, 0)',
    description: '',
    id: 2,
    title: 'Community Contribution',
  }],
  list: {
    color: 'rgb(0, 0, 0)',
    id: 3,
    title: 'bug',
  },
  updateFilters: true,
};

const createComponent = (propsData) => {
  const Component = Vue.extend(labelsComponent);

  return new Component({
    el: document.createElement('div'),
    propsData,
  });
};

describe('IssueCardLabels', () => {
  describe('props', () => {
    const props = labelsComponent.props;

    it('should have labels prop', () => {
      const { labels } = props;
      const LabelsClass = labels.type;

      expect(labels).toBeDefined();
      expect(new LabelsClass() instanceof Array).toBeTruthy();
      expect(labels.required).toBeTruthy();
    });

    it('should have list prop', () => {
      const { list } = props;

      expect(list).toBeDefined();
      expect(list instanceof Object).toBeTruthy();
      expect(list.required).toBeFalsy();
    });

    it('should have updateFilters prop', () => {
      const { updateFilters } = props;
      const UpdateFiltersClass = updateFilters.type;

      expect(updateFilters).toBeDefined();
      expect(new UpdateFiltersClass() instanceof Boolean).toBeTruthy();
      expect(updateFilters.required).toBeFalsy();
      expect(updateFilters.default).toBeFalsy();
    });
  });

  describe('methods', () => {
    describe('showLabel', () => {
      it('should return true if there is no list', () => {
        const data = Object.assign({}, propData);
        data.list = null;
        const vm = createComponent(data);

        expect(vm.showLabel()).toEqual(true);
      });

      it('should return true if there is a list and no list.label', () => {
        const vm = createComponent(propData);

        expect(vm.showLabel()).toEqual(true);
      });

      it('should return true if list.label.id does not match label.id', () => {
        const data = Object.assign({}, propData);
        data.list.label = {
          id: 100,
        };
        const vm = createComponent(data);

        const showLabel = vm.showLabel({
          id: 1,
        });

        expect(showLabel).toEqual(true);
      });
    });

    describe('filterByLabel', () => {
      it('should not continue if there is no updateFilters set', () => {
        const data = Object.assign({}, propData);
        data.updateFilters = null;

        const spy = spyOn(gl.issueBoards.BoardsStore.filter.path, 'split').and.callThrough();
        const vm = createComponent(data);
        vm.filterByLabel({ title: 'title' }, { currentTarget: '' });

        expect(spy).not.toHaveBeenCalled();
      });

      it('should hide tooltip', () => {
        const spy = spyOn($.fn, 'tooltip').and.callFake(() => {});
        const vm = createComponent(propData);

        vm.filterByLabel({ title: 'title' }, { currentTarget: '' });
        expect(spy).toHaveBeenCalledWith('hide');
      });

      it('should add/remove label to BoardsStore filter path', () => {
        const originalPath = gl.issueBoards.BoardsStore.filter.path;
        const vm = createComponent(propData);

        const label = { title: 'special' };
        vm.filterByLabel(label, { currentTarget: '' });

        expect(gl.issueBoards.BoardsStore.filter.path).toEqual(`${originalPath}&label_name[]=${label.title}`);

        vm.filterByLabel(label, { currentTarget: '' });
        expect(gl.issueBoards.BoardsStore.filter.path).toEqual(originalPath);
      });

      it('should encode label title', () => {
        const originalPath = gl.issueBoards.BoardsStore.filter.path;
        const vm = createComponent(propData);

        const label = { title: '!@#$%^ &*()' };
        vm.filterByLabel(label, { currentTarget: '' });

        expect(gl.issueBoards.BoardsStore.filter.path).toEqual(`${originalPath}&label_name[]=${encodeURIComponent(label.title)}`);
      });

      it('should updateFiltersUrl', () => {
        spyOn(gl.issueBoards.BoardsStore, 'updateFiltersUrl').and.callFake(() => {});

        const vm = createComponent(propData);
        vm.filterByLabel({ title: 'title' }, { currentTarget: '' });

        expect(gl.issueBoards.BoardsStore.updateFiltersUrl).toHaveBeenCalled();
        expect(gl.issueBoards.BoardsStore.updateFiltersUrl.calls.count()).toEqual(1);
      });

      it('should emit updateTokens to eventHub', (done) => {
        const vm = createComponent(propData);
        spyOn(eventHub, '$emit').and.callFake((message) => {
          expect(message).toEqual('updateTokens');
          done();
        });

        vm.filterByLabel({ title: 'title' }, { currentTarget: '' });
      });
    });

    describe('labelStyle', () => {
      it('should return style object with backgroundColor and color', () => {
        const data = {
          color: '#000000',
          textColor: '#FFFFFF',
        };

        const vm = createComponent(propData);
        const style = vm.labelStyle(data);
        expect(style.backgroundColor).toEqual(data.color);
        expect(style.color).toEqual(data.textColor);
      });
    });
  });

  describe('template', () => {
    it('should have correct elements', () => {
      const vm = createComponent(propData);
      const el = vm.$el;
      spyOn(vm, 'filterByLabel').and.callThrough();

      expect(el.tagName).toEqual('DIV');
      expect(el.classList.contains('card-footer')).toEqual(true);

      const labels = el.querySelectorAll('button');
      expect(labels.length).toEqual(2);

      const firstLabel = labels[0];
      expect(firstLabel.getAttribute('type')).toEqual('button');
      expect(firstLabel.textContent.trim()).toEqual(propData.labels[0].title);
      expect(firstLabel.getAttribute('title')).toEqual(propData.labels[0].description);
      expect(firstLabel.style.backgroundColor).toEqual(propData.labels[0].color);
      expect(firstLabel.style.color).toEqual(propData.labels[0].textColor);

      firstLabel.click();
      expect(vm.filterByLabel).toHaveBeenCalled();
      expect(vm.filterByLabel.calls.count()).toEqual(1);
      vm.filterByLabel.calls.reset();

      const secondLabel = labels[1];
      expect(secondLabel.getAttribute('type')).toEqual('button');
      expect(secondLabel.textContent.trim()).toEqual(propData.labels[1].title);
      expect(secondLabel.getAttribute('title')).toEqual(propData.labels[1].description);
      expect(secondLabel.style.backgroundColor).toEqual(propData.labels[1].color);
      expect(secondLabel.style.color).toEqual(propData.labels[1].textColor);

      secondLabel.click();
      expect(vm.filterByLabel).toHaveBeenCalled();
      expect(vm.filterByLabel.calls.count()).toEqual(1);
    });

    it('should not display label if showLabel is false', () => {
      const data = Object.assign({}, propData);
      data.list.label = {
        id: 1,
      };
      const vm = createComponent(data);
      const el = vm.$el;

      const labels = el.querySelectorAll('button');
      expect(labels.length).toEqual(1);
    });
  });
});