blob: 6949afc4f50327e978565e7a658e8251759f7285 [file] [log] [blame]
Andrew Bonventre82361ef2015-12-21 18:22:49 -05001<!DOCTYPE html>
2<!--
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04003@license
Andrew Bonventre82361ef2015-12-21 18:22:49 -05004Copyright (C) 2015 The Android Open Source Project
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17-->
18
19<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
Tao Zhoud90e7f42020-04-29 17:07:14 +020020<meta charset="utf-8">
Andrew Bonventre82361ef2015-12-21 18:22:49 -050021<title>gr-reviewer-list</title>
Tao Zhou8ef16f72019-11-18 14:14:36 -080022
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010023<script src="/node_modules/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
Andrew Bonventre82361ef2015-12-21 18:22:49 -050024
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010025<script src="/node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
26<script src="/components/wct-browser-legacy/browser.js"></script>
Viktar Donich29e1ce52017-03-28 17:02:44 -070027
Andrew Bonventre82361ef2015-12-21 18:22:49 -050028<test-fixture id="basic">
29 <template>
30 <gr-reviewer-list></gr-reviewer-list>
31 </template>
32</test-fixture>
33
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010034<script type="module">
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010035import '../../../test/common-test-setup.js';
36import './gr-reviewer-list.js';
37import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
38suite('gr-reviewer-list tests', () => {
39 let element;
40 let sandbox;
Andrew Bonventre82361ef2015-12-21 18:22:49 -050041
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010042 setup(() => {
43 element = fixture('basic');
Dmitrii Filippovbcf84d22020-03-18 15:31:59 +010044 element.serverConfig = {};
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010045 sandbox = sinon.sandbox.create();
46 stub('gr-rest-api-interface', {
47 getConfig() { return Promise.resolve({}); },
48 removeChangeReviewer() {
49 return Promise.resolve({ok: true});
50 },
Andrew Bonventre82361ef2015-12-21 18:22:49 -050051 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010052 });
Andrew Bonventre82361ef2015-12-21 18:22:49 -050053
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010054 teardown(() => {
55 sandbox.restore();
56 });
57
58 test('controls hidden on immutable element', () => {
59 element.mutable = false;
60 assert.isTrue(element.shadowRoot
61 .querySelector('.controlsContainer').hasAttribute('hidden'));
62 element.mutable = true;
63 assert.isFalse(element.shadowRoot
64 .querySelector('.controlsContainer').hasAttribute('hidden'));
65 });
66
67 test('add reviewer button opens reply dialog', done => {
68 element.addEventListener('show-reply-dialog', () => {
69 done();
Logan Hanksa75fb052016-08-01 13:23:38 -070070 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010071 MockInteractions.tap(element.shadowRoot
72 .querySelector('.addReviewer'));
73 });
Logan Hanksa75fb052016-08-01 13:23:38 -070074
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010075 test('only show remove for removable reviewers', () => {
76 element.mutable = true;
77 element.change = {
78 owner: {
79 _account_id: 1,
80 },
81 reviewers: {
82 REVIEWER: [
83 {
84 _account_id: 2,
85 name: 'Bojack Horseman',
86 email: 'SecretariatRulez96@hotmail.com',
87 },
Andrew Bonventre82361ef2015-12-21 18:22:49 -050088 {
89 _account_id: 3,
90 name: 'Pinky Penguin',
91 },
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010092 ],
93 CC: [
Andrew Bonventre82361ef2015-12-21 18:22:49 -050094 {
95 _account_id: 4,
96 name: 'Diane Nguyen',
97 email: 'macarthurfellow2B@juno.com',
98 },
Kasper Nilsson8cbccea2017-03-23 10:53:21 -070099 {
100 email: 'test@e.mail',
101 },
102 ],
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100103 },
104 removable_reviewers: [
105 {
106 _account_id: 3,
107 name: 'Pinky Penguin',
108 },
109 {
110 _account_id: 4,
111 name: 'Diane Nguyen',
112 email: 'macarthurfellow2B@juno.com',
113 },
114 {
115 email: 'test@e.mail',
116 },
117 ],
118 };
119 flushAsynchronousOperations();
120 const chips =
121 dom(element.root).querySelectorAll('gr-account-chip');
122 assert.equal(chips.length, 4);
Becky Siegel7442a112017-05-15 09:35:25 -0700123
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100124 for (const el of Array.from(chips)) {
125 const accountID = el.account._account_id || el.account.email;
126 assert.ok(accountID);
Han-Wen Nienhuysb72d5442016-03-16 16:34:44 +0100127
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100128 const buttonEl = el.shadowRoot
129 .querySelector('gr-button');
130 assert.isNotNull(buttonEl);
131 if (accountID == 2) {
132 assert.isTrue(buttonEl.hasAttribute('hidden'));
133 } else {
134 assert.isFalse(buttonEl.hasAttribute('hidden'));
Becky Siegel7442a112017-05-15 09:35:25 -0700135 }
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100136 }
Andrew Bonventre82361ef2015-12-21 18:22:49 -0500137 });
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100138
139 test('tracking reviewers and ccs', () => {
140 let counter = 0;
141 function makeAccount() {
142 return {_account_id: counter++};
143 }
144
145 const owner = makeAccount();
146 const reviewer = makeAccount();
147 const cc = makeAccount();
148 const reviewers = {
149 REMOVED: [makeAccount()],
150 REVIEWER: [owner, reviewer],
151 CC: [owner, cc],
152 };
153
154 element.ccsOnly = false;
155 element.reviewersOnly = false;
156 element.change = {
157 owner,
158 reviewers,
159 };
160 assert.deepEqual(element._reviewers, [reviewer, cc]);
161
162 element.reviewersOnly = true;
163 element.change = {
164 owner,
165 reviewers,
166 };
167 assert.deepEqual(element._reviewers, [reviewer]);
168
169 element.ccsOnly = true;
170 element.reviewersOnly = false;
171 element.change = {
172 owner,
173 reviewers,
174 };
175 assert.deepEqual(element._reviewers, [cc]);
176 });
177
178 test('_handleAddTap passes mode with event', () => {
Tao Zhou0a3c9862020-04-08 14:45:37 +0200179 const fireStub = sandbox.stub(element, 'dispatchEvent');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100180 const e = {preventDefault() {}};
181
182 element.ccsOnly = false;
183 element.reviewersOnly = false;
184 element._handleAddTap(e);
Tao Zhou0a3c9862020-04-08 14:45:37 +0200185 assert.equal(fireStub.lastCall.args[0].type, 'show-reply-dialog');
186 assert.deepEqual(fireStub.lastCall.args[0].detail, {value: {}});
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100187
188 element.reviewersOnly = true;
189 element._handleAddTap(e);
Tao Zhou0a3c9862020-04-08 14:45:37 +0200190 assert.equal(fireStub.lastCall.args[0].type, 'show-reply-dialog');
191 assert.deepEqual(
192 fireStub.lastCall.args[0].detail,
193 {value: {reviewersOnly: true}});
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100194
195 element.ccsOnly = true;
196 element.reviewersOnly = false;
197 element._handleAddTap(e);
Tao Zhou0a3c9862020-04-08 14:45:37 +0200198 assert.equal(fireStub.lastCall.args[0].type, 'show-reply-dialog');
199 assert.deepEqual(fireStub.lastCall.args[0].detail,
200 {value: {ccsOnly: true}});
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100201 });
202
Dmitrii Filippovbcf84d22020-03-18 15:31:59 +0100203 test('dont show all reviewers button with 4 reviewers', () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100204 const reviewers = [];
Dmitrii Filippovbcf84d22020-03-18 15:31:59 +0100205 element.maxReviewersDisplayed = 3;
206 for (let i = 0; i < 4; i++) {
207 reviewers.push(
208 {email: i+'reviewer@google.com', name: 'reviewer-' + i});
209 }
210 element.ccsOnly = true;
211
212 element.change = {
213 owner: {
214 _account_id: 1,
215 },
216 reviewers: {
217 CC: reviewers,
218 },
219 };
220 assert.equal(element._hiddenReviewerCount, 0);
221 assert.equal(element._displayedReviewers.length, 4);
222 assert.equal(element._reviewers.length, 4);
223 assert.isTrue(element.shadowRoot
224 .querySelector('.hiddenReviewers').hidden);
225 });
226
227 test('show all reviewers button with 6 reviewers', () => {
228 const reviewers = [];
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100229 for (let i = 0; i < 6; i++) {
230 reviewers.push(
231 {email: i+'reviewer@google.com', name: 'reviewer-' + i});
232 }
233 element.ccsOnly = true;
234
235 element.change = {
236 owner: {
237 _account_id: 1,
238 },
239 reviewers: {
240 CC: reviewers,
241 },
242 };
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100243 assert.equal(element._hiddenReviewerCount, 3);
Dmitrii Filippovbcf84d22020-03-18 15:31:59 +0100244 assert.equal(element._displayedReviewers.length, 3);
245 assert.equal(element._reviewers.length, 6);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100246 assert.isFalse(element.shadowRoot
247 .querySelector('.hiddenReviewers').hidden);
248 });
249
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100250 test('show all reviewers button', () => {
251 const reviewers = [];
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100252 for (let i = 0; i < 100; i++) {
253 reviewers.push(
254 {email: i+'reviewer@google.com', name: 'reviewer-' + i});
255 }
256 element.ccsOnly = true;
257
258 element.change = {
259 owner: {
260 _account_id: 1,
261 },
262 reviewers: {
263 CC: reviewers,
264 },
265 };
Dmitrii Filippovbcf84d22020-03-18 15:31:59 +0100266 assert.equal(element._hiddenReviewerCount, 97);
267 assert.equal(element._displayedReviewers.length, 3);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100268 assert.equal(element._reviewers.length, 100);
269 assert.isFalse(element.shadowRoot
270 .querySelector('.hiddenReviewers').hidden);
271
272 MockInteractions.tap(element.shadowRoot
273 .querySelector('.hiddenReviewers'));
274
275 assert.equal(element._hiddenReviewerCount, 0);
276 assert.equal(element._displayedReviewers.length, 100);
277 assert.equal(element._reviewers.length, 100);
278 assert.isTrue(element.shadowRoot
279 .querySelector('.hiddenReviewers').hidden);
280 });
281
282 test('votable labels', () => {
283 const change = {
284 labels: {
285 Foo: {
286 all: [{_account_id: 7, permitted_voting_range: {max: 2}}],
287 },
288 Bar: {
289 all: [{_account_id: 1, permitted_voting_range: {max: 1}},
290 {_account_id: 7, permitted_voting_range: {max: 1}}],
291 },
292 FooBar: {
293 all: [{_account_id: 7, value: 0}],
294 },
295 },
296 permitted_labels: {
297 Foo: ['-1', ' 0', '+1', '+2'],
298 FooBar: ['-1', ' 0'],
299 },
300 };
301 assert.strictEqual(
Dmitrii Filippov6c0b1b12020-03-18 14:17:56 +0100302 element._computeVoteableText({_account_id: 1}, change),
303 'Bar');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100304 assert.strictEqual(
Dmitrii Filippov6c0b1b12020-03-18 14:17:56 +0100305 element._computeVoteableText({_account_id: 7}, change),
306 'Foo: +2, Bar, FooBar');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100307 assert.strictEqual(
Dmitrii Filippov6c0b1b12020-03-18 14:17:56 +0100308 element._computeVoteableText({_account_id: 2}, change),
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100309 '');
310 });
311
312 test('fails gracefully when all is not included', () => {
313 const change = {
314 labels: {Foo: {}},
315 permitted_labels: {
316 Foo: ['-1', ' 0', '+1', '+2'],
317 },
318 };
319 assert.strictEqual(
Dmitrii Filippov6c0b1b12020-03-18 14:17:56 +0100320 element._computeVoteableText({_account_id: 1}, change), '');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +0100321 });
322});
Andrew Bonventre82361ef2015-12-21 18:22:49 -0500323</script>