blob: 495ffe2e034c652e32be61615f3623c584fbccc7 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6import sinon from 'sinon';
7import {MrShowColumnsDropdown} from './mr-show-columns-dropdown.js';
8
9/** @type {MrShowColumnsDropdown} */
10let element;
11
12describe('mr-show-columns-dropdown', () => {
13 beforeEach(() => {
14 element = document.createElement('mr-show-columns-dropdown');
15 document.body.appendChild(element);
16
17 sinon.stub(element, '_baseUrl').returns('/p/chromium/issues/list');
18 sinon.stub(element, '_page');
19 });
20
21 afterEach(() => {
22 document.body.removeChild(element);
23 });
24
25 it('initializes', () => {
26 assert.instanceOf(element, MrShowColumnsDropdown);
27 });
28
29 it('displaying columns (spa)', async () => {
30 element.defaultFields = ['ID', 'Summary', 'AllLabels'];
31 element.columns = ['ID'];
32 element.issues = [
33 {approvalValues: [{fieldRef: {fieldName: 'Approval-Name'}}]},
34 {fieldValues: [
35 {phaseRef: {phaseName: 'Phase'}, fieldRef: {fieldName: 'Field-Name'}},
36 {fieldRef: {fieldName: 'Field-Name'}},
37 ]},
38 {labelRefs: [{label: 'Label-Name'}]},
39 ];
40
41 await element.updateComplete;
42
43 const actual =
44 element.items.map((item) => ({icon: item.icon, text: item.text}));
45 const expected = [
46 {icon: 'check', text: 'ID'},
47 {icon: '', text: 'AllLabels'},
48 {icon: '', text: 'Approval-Name'},
49 {icon: '', text: 'Approval-Name-Approver'},
50 {icon: '', text: 'Field-Name'},
51 {icon: '', text: 'Label'},
52 {icon: '', text: 'Phase.Field-Name'},
53 {icon: '', text: 'Summary'},
54 ];
55 assert.deepEqual(actual, expected);
56 });
57
58 describe('displaying columns (ezt)', () => {
59 it('sorts default column options', async () => {
60 element.defaultFields = ['ID', 'Summary', 'AllLabels'];
61 element.columns = [];
62 element._labelPrefixFields = [];
63
64 // Re-compute menu items on update.
65 await element.updateComplete;
66 const options = element.items;
67
68 assert.equal(options.length, 3);
69
70 assert.equal(options[0].text.trim(), 'AllLabels');
71 assert.equal(options[0].icon, '');
72
73 assert.equal(options[1].text.trim(), 'ID');
74 assert.equal(options[1].icon, '');
75
76 assert.equal(options[2].text.trim(), 'Summary');
77 assert.equal(options[2].icon, '');
78 });
79
80 it('sorts selected columns above unselected columns', async () => {
81 element.defaultFields = ['ID', 'Summary', 'AllLabels'];
82 element.columns = ['ID'];
83 element._labelPrefixFields = [];
84
85 // Re-compute menu items on update.
86 await element.updateComplete;
87 const options = element.items;
88
89 assert.equal(options.length, 3);
90
91 assert.equal(options[0].text.trim(), 'ID');
92 assert.equal(options[0].icon, 'check');
93
94 assert.equal(options[1].text.trim(), 'AllLabels');
95 assert.equal(options[1].icon, '');
96
97 assert.equal(options[2].text.trim(), 'Summary');
98 assert.equal(options[2].icon, '');
99 });
100
101 it('sorts field defs and label prefix column options', async () => {
102 element.defaultFields = ['ID', 'Summary'];
103 element.columns = [];
104 element._fieldDefs = [
105 {fieldRef: {fieldName: 'HelloWorld'}},
106 {fieldRef: {fieldName: 'TestField'}},
107 ];
108
109 element._labelPrefixFields = ['Milestone', 'Priority'];
110
111 // Re-compute menu items on update.
112 await element.updateComplete;
113 const options = element.items;
114
115 assert.equal(options.length, 6);
116 assert.equal(options[0].text.trim(), 'HelloWorld');
117 assert.equal(options[0].icon, '');
118
119 assert.equal(options[1].text.trim(), 'ID');
120 assert.equal(options[1].icon, '');
121
122 assert.equal(options[2].text.trim(), 'Milestone');
123 assert.equal(options[2].icon, '');
124
125 assert.equal(options[3].text.trim(), 'Priority');
126 assert.equal(options[3].icon, '');
127
128 assert.equal(options[4].text.trim(), 'Summary');
129 assert.equal(options[4].icon, '');
130
131 assert.equal(options[5].text.trim(), 'TestField');
132 assert.equal(options[5].icon, '');
133 });
134
135 it('add approver fields for approval type fields', async () => {
136 element.defaultFields = [];
137 element.columns = [];
138 element._fieldDefs = [
139 {fieldRef: {fieldName: 'HelloWorld', type: 'APPROVAL_TYPE'}},
140 ];
141 element._labelPrefixFields = [];
142
143 // Re-compute menu items on update.
144 await element.updateComplete;
145 const options = element.items;
146
147 assert.equal(options.length, 2);
148 assert.equal(options[0].text.trim(), 'HelloWorld');
149 assert.equal(options[0].icon, '');
150
151 assert.equal(options[1].text.trim(), 'HelloWorld-Approver');
152 assert.equal(options[1].icon, '');
153 });
154
155 it('phase field columns are correctly named', async () => {
156 element.defaultFields = [];
157 element.columns = [];
158 element._fieldDefs = [
159 {fieldRef: {fieldName: 'Number', type: 'INT_TYPE'}, isPhaseField: true},
160 {fieldRef: {fieldName: 'Speak', type: 'STR_TYPE'}, isPhaseField: true},
161 ];
162 element._labelPrefixFields = [];
163 element.phaseNames = ['cow', 'chicken'];
164
165 // Re-compute menu items on update.
166 await element.updateComplete;
167 const options = element.items;
168
169 assert.equal(options.length, 4);
170 assert.equal(options[0].text.trim(), 'chicken.Number');
171 assert.equal(options[0].icon, '');
172
173 assert.equal(options[1].text.trim(), 'chicken.Speak');
174 assert.equal(options[1].icon, '');
175
176 assert.equal(options[2].text.trim(), 'cow.Number');
177 assert.equal(options[2].icon, '');
178
179 assert.equal(options[3].text.trim(), 'cow.Speak');
180 assert.equal(options[3].icon, '');
181 });
182 });
183
184 describe('modifying columns', () => {
185 it('clicking unset column adds a column', async () => {
186 element.columns = ['ID', 'Summary'];
187 element.defaultFields = ['ID', 'Summary', 'AllLabels'];
188 element.queryParams = {};
189
190 await element.updateComplete;
191 element.clickItem(2);
192
193 sinon.assert.calledWith(element._page,
194 '/p/chromium/issues/list?colspec=ID%20Summary%20AllLabels');
195 });
196
197 it('clicking set column removes a column', async () => {
198 element.columns = ['ID', 'Summary'];
199 element.defaultFields = ['ID', 'Summary', 'AllLabels'];
200 element.queryParams = {};
201
202 await element.updateComplete;
203 element.clickItem(0);
204
205 sinon.assert.calledWith(element._page,
206 '/p/chromium/issues/list?colspec=Summary');
207 });
208 });
209});