Adrià Vilanova MartÃnez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 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 {MrApprovalCard} from './mr-approval-card.js'; |
| 7 | |
| 8 | let element; |
| 9 | |
| 10 | describe('mr-approval-card', () => { |
| 11 | beforeEach(() => { |
| 12 | element = document.createElement('mr-approval-card'); |
| 13 | document.body.appendChild(element); |
| 14 | }); |
| 15 | |
| 16 | afterEach(() => { |
| 17 | document.body.removeChild(element); |
| 18 | }); |
| 19 | |
| 20 | it('initializes', () => { |
| 21 | assert.instanceOf(element, MrApprovalCard); |
| 22 | }); |
| 23 | |
| 24 | it('_isApprover true when user is an approver', () => { |
| 25 | // User not in approver list. |
| 26 | element.approvers = [ |
| 27 | {displayName: 'tester@user.com'}, |
| 28 | {displayName: 'test@notuser.com'}, |
| 29 | {displayName: 'hello@world.com'}, |
| 30 | ]; |
| 31 | element.user = {displayName: 'test@user.com', groups: []}; |
| 32 | assert.isFalse(element._isApprover); |
| 33 | |
| 34 | // Use is in approver list. |
| 35 | element.approvers = [ |
| 36 | {displayName: 'tester@user.com'}, |
| 37 | {displayName: 'test@notuser.com'}, |
| 38 | {displayName: 'hello@world.com'}, |
| 39 | {displayName: 'test@user.com'}, |
| 40 | ]; |
| 41 | assert.isTrue(element._isApprover); |
| 42 | |
| 43 | // User's group is not in the list. |
| 44 | element.approvers = [ |
| 45 | {displayName: 'tester@user.com'}, |
| 46 | {displayName: 'nongroup@group.com'}, |
| 47 | {displayName: 'group@nongroup.com'}, |
| 48 | {displayName: 'ignore@test.com'}, |
| 49 | ]; |
| 50 | element.user = { |
| 51 | displayName: 'test@user.com', |
| 52 | groups: [ |
| 53 | {displayName: 'group@group.com'}, |
| 54 | {displayName: 'test@group.com'}, |
| 55 | {displayName: 'group@user.com'}, |
| 56 | ], |
| 57 | }; |
| 58 | assert.isFalse(element._isApprover); |
| 59 | |
| 60 | // User's group is in the list. |
| 61 | element.approvers = [ |
| 62 | {displayName: 'tester@user.com'}, |
| 63 | {displayName: 'group@group.com'}, |
| 64 | {displayName: 'test@notuser.com'}, |
| 65 | ]; |
| 66 | element.user = { |
| 67 | displayName: 'test@user.com', |
| 68 | groups: [ |
| 69 | {displayName: 'group@group.com'}, |
| 70 | ], |
| 71 | }; |
| 72 | assert.isTrue(element._isApprover); |
| 73 | }); |
| 74 | |
| 75 | it('approvals change color based on status', async () => { |
| 76 | // Initialize dependent CSS property from a stylesheet not included in |
| 77 | // our testing environment. |
| 78 | element.style.setProperty('--chops-purple-50', '#f3e5f5'); |
| 79 | |
| 80 | element.statusEnum = 'NEEDS_REVIEW'; |
| 81 | await element.updateComplete; |
| 82 | |
| 83 | const header = element.querySelector('button.header'); |
| 84 | |
| 85 | // Purple. Note that Chrome uses RGB for computed styles regardless of |
| 86 | // underlying CSS. |
| 87 | assert.equal( |
| 88 | window.getComputedStyle(header).getPropertyValue('background-color'), |
| 89 | 'rgb(243, 229, 245)'); |
| 90 | |
| 91 | element.statusEnum = 'APPROVED'; |
| 92 | await element.updateComplete; |
| 93 | |
| 94 | // Green. |
| 95 | assert.equal( |
| 96 | window.getComputedStyle(header).getPropertyValue('background-color'), |
| 97 | 'rgb(235, 244, 215)'); |
| 98 | }); |
| 99 | |
| 100 | it('site admins have approver privileges', async () => { |
| 101 | await element.updateComplete; |
| 102 | |
| 103 | const notice = element.querySelector('.approver-notice'); |
| 104 | assert.equal(notice.textContent.trim(), ''); |
| 105 | |
| 106 | element.user = {isSiteAdmin: true}; |
| 107 | await element.updateComplete; |
| 108 | |
| 109 | assert.isTrue(element._hasApproverPrivileges); |
| 110 | |
| 111 | assert.equal(notice.textContent.trim(), |
| 112 | 'Your site admin privileges give you full access to edit this approval.', |
| 113 | ); |
| 114 | }); |
| 115 | |
| 116 | it('site admins see all approval statuses except NotSet', () => { |
| 117 | element.user = {isSiteAdmin: true}; |
| 118 | |
| 119 | assert.isFalse(element._isApprover); |
| 120 | |
| 121 | element.statusEnum = 'NEEDS_REVIEW'; |
| 122 | |
| 123 | assert.equal(element._availableStatuses.length, 7); |
| 124 | assert.equal(element._availableStatuses[0].status, 'NeedsReview'); |
| 125 | assert.equal(element._availableStatuses[1].status, 'NA'); |
| 126 | assert.equal(element._availableStatuses[2].status, 'ReviewRequested'); |
| 127 | assert.equal(element._availableStatuses[3].status, 'ReviewStarted'); |
| 128 | assert.equal(element._availableStatuses[4].status, 'NeedInfo'); |
| 129 | assert.equal(element._availableStatuses[5].status, 'Approved'); |
| 130 | assert.equal(element._availableStatuses[6].status, 'NotApproved'); |
| 131 | }); |
| 132 | |
| 133 | it('approvers see all approval statuses except NotSet', () => { |
| 134 | element.user = {isSiteAdmin: false, displayName: 'test@email.com'}; |
| 135 | element.approvers = [{displayName: 'test@email.com'}]; |
| 136 | |
| 137 | assert.isTrue(element._isApprover); |
| 138 | |
| 139 | element.statusEnum = 'NEEDS_REVIEW'; |
| 140 | |
| 141 | assert.equal(element._availableStatuses.length, 7); |
| 142 | assert.equal(element._availableStatuses[0].status, 'NeedsReview'); |
| 143 | assert.equal(element._availableStatuses[1].status, 'NA'); |
| 144 | assert.equal(element._availableStatuses[2].status, 'ReviewRequested'); |
| 145 | assert.equal(element._availableStatuses[3].status, 'ReviewStarted'); |
| 146 | assert.equal(element._availableStatuses[4].status, 'NeedInfo'); |
| 147 | assert.equal(element._availableStatuses[5].status, 'Approved'); |
| 148 | assert.equal(element._availableStatuses[6].status, 'NotApproved'); |
| 149 | }); |
| 150 | |
| 151 | it('non-approvers see non-restricted approval statuses', () => { |
| 152 | element.user = {isSiteAdmin: false, displayName: 'test@email.com'}; |
| 153 | element.approvers = [{displayName: 'test@otheremail.com'}]; |
| 154 | |
| 155 | assert.isFalse(element._isApprover); |
| 156 | |
| 157 | element.statusEnum = 'NEEDS_REVIEW'; |
| 158 | |
| 159 | assert.equal(element._availableStatuses.length, 4); |
| 160 | assert.equal(element._availableStatuses[0].status, 'NeedsReview'); |
| 161 | assert.equal(element._availableStatuses[1].status, 'ReviewRequested'); |
| 162 | assert.equal(element._availableStatuses[2].status, 'ReviewStarted'); |
| 163 | assert.equal(element._availableStatuses[3].status, 'NeedInfo'); |
| 164 | }); |
| 165 | |
| 166 | it('non-approvers see restricted approval status when set', () => { |
| 167 | element.user = {isSiteAdmin: false, displayName: 'test@email.com'}; |
| 168 | element.approvers = [{displayName: 'test@otheremail.com'}]; |
| 169 | |
| 170 | assert.isFalse(element._isApprover); |
| 171 | |
| 172 | element.statusEnum = 'APPROVED'; |
| 173 | |
| 174 | assert.equal(element._availableStatuses.length, 5); |
| 175 | assert.equal(element._availableStatuses[0].status, 'NeedsReview'); |
| 176 | assert.equal(element._availableStatuses[1].status, 'ReviewRequested'); |
| 177 | assert.equal(element._availableStatuses[2].status, 'ReviewStarted'); |
| 178 | assert.equal(element._availableStatuses[3].status, 'NeedInfo'); |
| 179 | assert.equal(element._availableStatuses[4].status, 'Approved'); |
| 180 | }); |
| 181 | |
| 182 | it('expands to show focused comment', async () => { |
| 183 | element.focusId = 'c4'; |
| 184 | element.fieldName = 'field'; |
| 185 | element.comments = [ |
| 186 | { |
| 187 | sequenceNum: 1, |
| 188 | approvalRef: {fieldName: 'other-field'}, |
| 189 | }, |
| 190 | { |
| 191 | sequenceNum: 2, |
| 192 | approvalRef: {fieldName: 'field'}, |
| 193 | }, |
| 194 | { |
| 195 | sequenceNum: 3, |
| 196 | }, |
| 197 | { |
| 198 | sequenceNum: 4, |
| 199 | approvalRef: {fieldName: 'field'}, |
| 200 | }, |
| 201 | ]; |
| 202 | |
| 203 | await element.updateComplete; |
| 204 | |
| 205 | assert.isTrue(element.opened); |
| 206 | }); |
| 207 | |
| 208 | it('does not expand to show focused comment on other elements', async () => { |
| 209 | element.focusId = 'c3'; |
| 210 | element.comments = [ |
| 211 | { |
| 212 | sequenceNum: 1, |
| 213 | approvalRef: {fieldName: 'other-field'}, |
| 214 | }, |
| 215 | { |
| 216 | sequenceNum: 2, |
| 217 | approvalRef: {fieldName: 'field'}, |
| 218 | }, |
| 219 | { |
| 220 | sequenceNum: 4, |
| 221 | approvalRef: {fieldName: 'field'}, |
| 222 | }, |
| 223 | ]; |
| 224 | |
| 225 | await element.updateComplete; |
| 226 | |
| 227 | assert.isFalse(element.opened); |
| 228 | }); |
| 229 | |
| 230 | it('mr-edit-metadata is displayed if user has addissuecomment', async () => { |
| 231 | element.issuePermissions = ['addissuecomment']; |
| 232 | |
| 233 | await element.updateComplete; |
| 234 | |
| 235 | assert.isNotNull(element.querySelector('mr-edit-metadata')); |
| 236 | }); |
| 237 | |
| 238 | it('mr-edit-metadata is hidden if user has no addissuecomment', async () => { |
| 239 | element.issuePermissions = []; |
| 240 | |
| 241 | await element.updateComplete; |
| 242 | |
| 243 | assert.isNull(element.querySelector('mr-edit-metadata')); |
| 244 | }); |
| 245 | }); |