| <!DOCTYPE html> |
| <!-- |
| Copyright (C) 2015 The Android Open Source Project |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes"> |
| <title>gr-reviewer-list</title> |
| |
| <script src="../../../bower_components/webcomponentsjs/webcomponents.min.js"></script> |
| <script src="../../../bower_components/web-component-tester/browser.js"></script> |
| <script src="../../../scripts/util.js"></script> |
| |
| <link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html"> |
| <link rel="import" href="gr-reviewer-list.html"> |
| |
| <test-fixture id="basic"> |
| <template> |
| <gr-reviewer-list></gr-reviewer-list> |
| </template> |
| </test-fixture> |
| |
| <script> |
| suite('gr-reviewer-list tests', function() { |
| var element; |
| var autocompleteInput; |
| |
| setup(function() { |
| element = fixture('basic'); |
| autocompleteInput = element.$.input.$.input; |
| stub('gr-rest-api-interface', { |
| getChangeSuggestedReviewers: function() { |
| return Promise.resolve([ |
| { |
| account: { |
| _account_id: 1021482, |
| name: 'Andrew Bonventre', |
| email: 'andybons@chromium.org', |
| } |
| }, |
| { |
| account: { |
| _account_id: 1021863, |
| name: 'Andrew Bonventre', |
| email: 'andybons@google.com', |
| } |
| }, |
| { |
| group: { |
| id: 'c7af6dd375c092ff3b23c0937aa910693dc0c41b', |
| name: 'andy', |
| } |
| } |
| ]); |
| }, |
| addChangeReviewer: function() { |
| return Promise.resolve({ |
| ok: true, |
| text: function() { |
| return Promise.resolve( |
| ')]}\'\n' + |
| JSON.stringify({ |
| reviewers: [{ |
| _account_id: 1021482, |
| approvals: { |
| 'Code-Review': ' 0' |
| }, |
| email: 'andybons@chromium.org', |
| name: 'Andrew Bonventre', |
| }] |
| }) |
| ); |
| }, |
| }); |
| }, |
| removeChangeReviewer: function() { |
| return Promise.resolve({ok: true}); |
| }, |
| }); |
| }); |
| |
| test('controls hidden on immutable element', function() { |
| element.mutable = false; |
| assert.isTrue(element.$$('.controlsContainer').hasAttribute('hidden')); |
| element.mutable = true; |
| assert.isFalse(element.$$('.controlsContainer').hasAttribute('hidden')); |
| }); |
| |
| function getActiveElement() { |
| return document.activeElement.shadowRoot ? |
| document.activeElement.shadowRoot.activeElement : |
| document.activeElement; |
| } |
| |
| test('show/hide input', function() { |
| element.mutable = true; |
| assert.isFalse(element.$$('.addReviewer').hasAttribute('hidden')); |
| assert.isTrue( |
| element.$$('.autocompleteContainer').hasAttribute('hidden')); |
| assert.notEqual(getActiveElement().id, 'input'); |
| MockInteractions.tap(element.$$('.addReviewer')); |
| assert.isTrue(element.$$('.addReviewer').hasAttribute('hidden')); |
| assert.isFalse( |
| element.$$('.autocompleteContainer').hasAttribute('hidden')); |
| assert.equal(getActiveElement().id, 'input'); |
| |
| MockInteractions.pressAndReleaseKeyOn(autocompleteInput, 27); // 'esc' |
| |
| assert.isFalse(element.$$('.addReviewer').hasAttribute('hidden')); |
| assert.isTrue( |
| element.$$('.autocompleteContainer').hasAttribute('hidden')); |
| assert.equal(getActiveElement().id, 'addReviewer'); |
| }); |
| |
| test('only show remove for removable reviewers', function() { |
| element.mutable = true; |
| element.change = { |
| owner: { |
| _account_id: 1, |
| }, |
| reviewers: { |
| 'REVIEWER': [ |
| { |
| _account_id: 2, |
| name: 'Bojack Horseman', |
| email: 'SecretariatRulez96@hotmail.com', |
| }, |
| { |
| _account_id: 3, |
| name: 'Pinky Penguin', |
| }, |
| ], |
| 'CC': [ |
| { |
| _account_id: 4, |
| name: 'Diane Nguyen', |
| email: 'macarthurfellow2B@juno.com', |
| }, |
| ] |
| }, |
| removable_reviewers: [ |
| { |
| _account_id: 3, |
| name: 'Pinky Penguin', |
| }, |
| { |
| _account_id: 4, |
| name: 'Diane Nguyen', |
| email: 'macarthurfellow2B@juno.com', |
| }, |
| ] |
| }; |
| flushAsynchronousOperations(); |
| var chips = |
| Polymer.dom(element.root).querySelectorAll('gr-account-chip'); |
| assert.equal(chips.length, 3); |
| Array.from(chips).forEach(function(el) { |
| var accountID = parseInt(el.getAttribute('data-account-id'), 10); |
| assert.ok(accountID); |
| |
| var buttonEl = el.$$('gr-button'); |
| assert.isNotNull(buttonEl); |
| if (accountID == 2) { |
| assert.isTrue(buttonEl.hasAttribute('hidden')); |
| } else { |
| assert.isFalse(buttonEl.hasAttribute('hidden')); |
| } |
| }); |
| }); |
| |
| test('autocomplete starts at >= 3 chars', function() { |
| element._inputRequestTimeout = 0; |
| element._mutable = true; |
| element.change = {_number: 123}; |
| |
| element.$.input.text = 'fo'; |
| |
| flushAsynchronousOperations(); |
| |
| assert.isFalse(element.$.restAPI.getChangeSuggestedReviewers.called); |
| }); |
| |
| test('add/remove reviewer flow', function(done) { |
| element.change = { |
| _number: 42, |
| reviewers: {}, |
| removable_reviewers: [], |
| owner: {_account_id: 0}, |
| }; |
| element._inputRequestTimeout = 0; |
| element._mutable = true; |
| MockInteractions.tap(element.$$('.addReviewer')); |
| flushAsynchronousOperations(); |
| element.$.input.text = 'andy'; |
| |
| element._lastAutocompleteRequest.then(function() { |
| flushAsynchronousOperations(); |
| |
| MockInteractions.pressAndReleaseKeyOn(autocompleteInput, 27); // 'esc' |
| assert.isTrue(element.$$('.autocompleteContainer') |
| .hasAttribute('hidden')); |
| |
| MockInteractions.tap(element.$$('.addReviewer')); |
| |
| element.$.input.text = 'andyb'; |
| element._lastAutocompleteRequest.then(function() { |
| |
| MockInteractions.pressAndReleaseKeyOn( |
| autocompleteInput, 13); // 'enter' |
| assert.isTrue(element.disabled); |
| |
| element._xhrPromise.then(function() { |
| assert.isFalse(element.disabled); |
| flushAsynchronousOperations(); |
| var reviewerEls = |
| Polymer.dom(element.root).querySelectorAll('.reviewer'); |
| assert.equal(reviewerEls.length, 1); |
| MockInteractions.tap(element.$$('.reviewer').$$('gr-button')); |
| flushAsynchronousOperations(); |
| assert.isTrue(element.disabled); |
| |
| element._xhrPromise.then(function() { |
| flushAsynchronousOperations(); |
| assert.isFalse(element.disabled); |
| var reviewerEls = |
| Polymer.dom(element.root).querySelectorAll('.reviewer'); |
| assert.equal(reviewerEls.length, 0); |
| done(); |
| }); |
| }); |
| }); |
| }); |
| }); |
| |
| test('_makeSuggestion', function() { |
| var account = { |
| _account_id: 123456, |
| name: 'name', |
| email: 'email' |
| }; |
| var group = { |
| id: '123456', |
| name: 'name', |
| }; |
| |
| var suggestion = element._makeSuggestion({account: account}); |
| |
| assert.deepEqual(suggestion, { |
| name: 'name (email)', |
| value: {account: account}, |
| }); |
| |
| suggestion = element._makeSuggestion({group: group}); |
| |
| assert.deepEqual(suggestion, { |
| name: 'name', |
| value: {group: group}, |
| }); |
| }); |
| |
| test('_notInList', function() { |
| var group = { |
| id: '123456', |
| name: 'name', |
| }; |
| var account = { |
| _account_id: 123456, |
| name: 'name', |
| email: 'email', |
| }; |
| |
| element.change = {owner: {_account_id: 123456}}; |
| |
| // Is true when passing a group. |
| assert.isTrue(element._notInList({group: group})); |
| |
| // Is false when passing the change owner. |
| assert.isFalse(element._notInList({account: account})); |
| |
| element.change.owner._account_id = 789; |
| |
| // Is true when passing a different user than the change owner, and is not |
| // in the reviewer list. |
| assert.isTrue(element._notInList({account: account})); |
| |
| element._reviewers = [{_account_id: 123456}]; |
| |
| // Is false when passing a different user than the change owner, but *is* |
| // the reviewer list. |
| assert.isFalse(element._notInList({account: account})); |
| }); |
| }); |
| </script> |