Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | // 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 | |
| 5 | import {assert} from 'chai'; |
| 6 | import {MrUpdateIssueDialog} from './mr-update-issue-hotlists-dialog.js'; |
| 7 | import {prpcClient} from 'prpc-client-instance.js'; |
| 8 | |
| 9 | let element; |
| 10 | let form; |
| 11 | |
| 12 | describe('mr-update-issue-hotlists-dialog', () => { |
| 13 | beforeEach(async () => { |
| 14 | element = document.createElement('mr-update-issue-hotlists-dialog'); |
| 15 | document.body.appendChild(element); |
| 16 | |
| 17 | await element.updateComplete; |
| 18 | form = element.shadowRoot.querySelector('#issueHotlistsForm'); |
| 19 | |
| 20 | sinon.stub(prpcClient, 'call'); |
| 21 | }); |
| 22 | |
| 23 | afterEach(() => { |
| 24 | document.body.removeChild(element); |
| 25 | |
| 26 | prpcClient.call.restore(); |
| 27 | }); |
| 28 | |
| 29 | it('initializes', () => { |
| 30 | assert.instanceOf(element, MrUpdateIssueDialog); |
| 31 | }); |
| 32 | |
| 33 | it('no changes', () => { |
| 34 | assert.deepEqual(element.changes, {added: [], removed: []}); |
| 35 | }); |
| 36 | |
| 37 | it('clicking on issues produces changes', async () => { |
| 38 | element.issueHotlists = [ |
| 39 | {name: 'Hotlist-1', ownerRef: {userId: 12345}}, |
| 40 | {name: 'Hotlist-2', ownerRef: {userId: 12345}}, |
| 41 | {name: 'Hotlist-1', ownerRef: {userId: 67890}}, |
| 42 | ]; |
| 43 | element.userHotlists = [ |
| 44 | {name: 'Hotlist-1', ownerRef: {userId: 67890}}, |
| 45 | {name: 'Hotlist-2', ownerRef: {userId: 67890}}, |
| 46 | ]; |
| 47 | element.user = {userId: 67890}; |
| 48 | |
| 49 | element.open(); |
| 50 | await element.updateComplete; |
| 51 | |
| 52 | const chopsCheckboxes = form.querySelectorAll('chops-checkbox'); |
| 53 | chopsCheckboxes[0].click(); |
| 54 | chopsCheckboxes[1].click(); |
| 55 | assert.deepEqual(element.changes, { |
| 56 | added: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 57 | removed: [{name: 'Hotlist-1', owner: {userId: 67890}}], |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | it('adding new hotlist produces changes', async () => { |
| 62 | await element.updateComplete; |
| 63 | form.newHotlistName.value = 'New-Hotlist'; |
| 64 | assert.deepEqual(element.changes, { |
| 65 | added: [], |
| 66 | removed: [], |
| 67 | created: { |
| 68 | name: 'New-Hotlist', |
| 69 | summary: 'Hotlist created from issue.', |
| 70 | }, |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | it('reset changes', async () => { |
| 75 | element.issueHotlists = [ |
| 76 | {name: 'Hotlist-1', ownerRef: {userId: 12345}}, |
| 77 | {name: 'Hotlist-2', ownerRef: {userId: 12345}}, |
| 78 | {name: 'Hotlist-1', ownerRef: {userId: 67890}}, |
| 79 | ]; |
| 80 | element.userHotlists = [ |
| 81 | {name: 'Hotlist-1', ownerRef: {userId: 67890}}, |
| 82 | {name: 'Hotlist-2', ownerRef: {userId: 67890}}, |
| 83 | ]; |
| 84 | element.user = {userId: 67890}; |
| 85 | |
| 86 | element.open(); |
| 87 | await element.updateComplete; |
| 88 | |
| 89 | const chopsCheckboxes = form.querySelectorAll('chops-checkbox'); |
| 90 | const checkbox1 = chopsCheckboxes[0]; |
| 91 | const checkbox2 = chopsCheckboxes[1]; |
| 92 | checkbox1.click(); |
| 93 | checkbox2.click(); |
| 94 | form.newHotlisName = 'New-Hotlist'; |
| 95 | await element.reset(); |
| 96 | assert.isTrue(checkbox1.checked); |
| 97 | assert.isNotTrue(checkbox2.checked); // Falsey property. |
| 98 | assert.equal(form.newHotlistName.value, ''); |
| 99 | }); |
| 100 | |
| 101 | it('saving adds issues to hotlist', async () => { |
| 102 | sinon.stub(element, 'changes').get(() => ({ |
| 103 | added: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 104 | })); |
| 105 | element.issueRefs = [{localId: 22, projectName: 'test'}]; |
| 106 | |
| 107 | await element.save(); |
| 108 | |
| 109 | sinon.assert.calledWith(prpcClient.call, 'monorail.Features', |
| 110 | 'AddIssuesToHotlists', { |
| 111 | hotlistRefs: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 112 | issueRefs: [{localId: 22, projectName: 'test'}], |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | it('saving removes issues from hotlist', async () => { |
| 117 | sinon.stub(element, 'changes').get(() => ({ |
| 118 | removed: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 119 | })); |
| 120 | element.issueRefs = [{localId: 22, projectName: 'test'}]; |
| 121 | |
| 122 | await element.save(); |
| 123 | |
| 124 | sinon.assert.calledWith(prpcClient.call, 'monorail.Features', |
| 125 | 'RemoveIssuesFromHotlists', { |
| 126 | hotlistRefs: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 127 | issueRefs: [{localId: 22, projectName: 'test'}], |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | it('saving creates new hotlist with issues', async () => { |
| 132 | sinon.stub(element, 'changes').get(() => ({ |
| 133 | created: {name: 'MyHotlist', summary: 'the best hotlist'}, |
| 134 | })); |
| 135 | element.issueRefs = [{localId: 22, projectName: 'test'}]; |
| 136 | |
| 137 | await element.save(); |
| 138 | |
| 139 | sinon.assert.calledWith(prpcClient.call, 'monorail.Features', |
| 140 | 'CreateHotlist', { |
| 141 | name: 'MyHotlist', |
| 142 | summary: 'the best hotlist', |
| 143 | issueRefs: [{localId: 22, projectName: 'test'}], |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | it('saving refreshes issue hotlises if viewed issue is updated', async () => { |
| 148 | sinon.stub(element, 'changes').get(() => ({ |
| 149 | created: {name: 'MyHotlist', summary: 'the best hotlist'}, |
| 150 | })); |
| 151 | element.issueRefs = [ |
| 152 | {localId: 22, projectName: 'test'}, |
| 153 | {localId: 32, projectName: 'test'}, |
| 154 | ]; |
| 155 | element.viewedIssueRef = {localId: 32, projectName: 'test'}; |
| 156 | |
| 157 | await element.save(); |
| 158 | |
| 159 | sinon.assert.calledWith(prpcClient.call, 'monorail.Features', |
| 160 | 'ListHotlistsByIssue', {issue: {localId: 32, projectName: 'test'}}); |
| 161 | }); |
| 162 | |
| 163 | it('dispatches event upon successfully saving', async () => { |
| 164 | sinon.stub(element, 'changes').get(() => ({ |
| 165 | added: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 166 | })); |
| 167 | element.issueRefs = [{localId: 22, projectName: 'test'}]; |
| 168 | |
| 169 | const savedStub = sinon.stub(); |
| 170 | element.addEventListener('saveSuccess', savedStub); |
| 171 | |
| 172 | await element.save(); |
| 173 | |
| 174 | sinon.assert.calledOnce(savedStub); |
| 175 | }); |
| 176 | |
| 177 | it('dispatches no event upon error saving', async () => { |
| 178 | sinon.stub(element, 'changes').get(() => ({ |
| 179 | added: [{name: 'Hotlist-2', owner: {userId: 67890}}], |
| 180 | })); |
| 181 | element.issueRefs = [{localId: 22, projectName: 'test'}]; |
| 182 | |
| 183 | const error = new Error('Mistakes were made'); |
| 184 | prpcClient.call.returns(Promise.reject(error)); |
| 185 | |
| 186 | const savedStub = sinon.stub(); |
| 187 | element.addEventListener('saveSuccess', savedStub); |
| 188 | |
| 189 | await element.save(); |
| 190 | |
| 191 | sinon.assert.notCalled(savedStub); |
| 192 | }); |
| 193 | }); |