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 sinon from 'sinon'; |
| 7 | import {MrComment} from './mr-comment.js'; |
| 8 | |
| 9 | |
| 10 | let element; |
| 11 | |
| 12 | /** |
| 13 | * Testing helper to find if an Array of options has an option with some |
| 14 | * text. |
| 15 | * @param {Array<MenuItem>} options Dropdown options to look through. |
| 16 | * @param {string} needle The text to search for. |
| 17 | * @return {boolean} Whether the option exists or not. |
| 18 | */ |
| 19 | const hasOptionWithText = (options, needle) => { |
| 20 | return options.some(({text}) => text === needle); |
| 21 | }; |
| 22 | |
| 23 | describe('mr-comment', () => { |
| 24 | beforeEach(() => { |
| 25 | element = document.createElement('mr-comment'); |
| 26 | element.comment = { |
| 27 | canFlag: true, |
| 28 | localId: 898395, |
| 29 | canDelete: true, |
| 30 | projectName: 'chromium', |
| 31 | commenter: { |
| 32 | displayName: 'user@example.com', |
| 33 | userId: '12345', |
| 34 | }, |
| 35 | content: 'foo', |
| 36 | sequenceNum: 3, |
| 37 | timestamp: 1549319989, |
| 38 | }; |
| 39 | document.body.appendChild(element); |
| 40 | |
| 41 | // Stub RAF to execute immediately. |
| 42 | sinon.stub(window, 'requestAnimationFrame').callsFake((func) => func()); |
| 43 | }); |
| 44 | |
| 45 | afterEach(() => { |
| 46 | document.body.removeChild(element); |
| 47 | window.requestAnimationFrame.restore(); |
| 48 | }); |
| 49 | |
| 50 | it('initializes', () => { |
| 51 | assert.instanceOf(element, MrComment); |
| 52 | }); |
| 53 | |
| 54 | it('scrolls to comment', async () => { |
| 55 | sinon.stub(element, 'scrollIntoView'); |
| 56 | |
| 57 | element.highlighted = true; |
| 58 | await element.updateComplete; |
| 59 | |
| 60 | assert.isTrue(element.scrollIntoView.calledOnce); |
| 61 | |
| 62 | element.scrollIntoView.restore(); |
| 63 | }); |
| 64 | |
| 65 | it('comment header renders self link to comment', async () => { |
| 66 | element.comment = { |
| 67 | localId: 1, |
| 68 | projectName: 'test', |
| 69 | sequenceNum: 2, |
| 70 | commenter: { |
| 71 | displayName: 'user@example.com', |
| 72 | userId: '12345', |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | await element.updateComplete; |
| 77 | |
| 78 | const link = element.shadowRoot.querySelector('.comment-link'); |
| 79 | |
| 80 | assert.equal(link.textContent, 'Comment 2'); |
| 81 | assert.include(link.href, '?id=1#c2'); |
| 82 | }); |
| 83 | |
| 84 | it('renders issue links for Blockedon issue amendments', async () => { |
| 85 | element.comment = { |
| 86 | projectName: 'test', |
| 87 | amendments: [ |
| 88 | { |
| 89 | fieldName: 'Blockedon', |
| 90 | newOrDeltaValue: '-2 3', |
| 91 | }, |
| 92 | ], |
| 93 | commenter: { |
| 94 | displayName: 'user@example.com', |
| 95 | userId: '12345', |
| 96 | }, |
| 97 | }; |
| 98 | |
| 99 | await element.updateComplete; |
| 100 | |
| 101 | const links = element.shadowRoot.querySelectorAll('mr-issue-link'); |
| 102 | |
| 103 | assert.equal(links.length, 2); |
| 104 | |
| 105 | assert.equal(links[0].text, '-2'); |
| 106 | assert.deepEqual(links[0].href, '/p/test/issues/detail?id=2'); |
| 107 | |
| 108 | assert.equal(links[1].text, '3'); |
| 109 | assert.deepEqual(links[1].href, '/p/test/issues/detail?id=3'); |
| 110 | }); |
| 111 | |
| 112 | it('renders issue links for Blocking issue amendments', async () => { |
| 113 | element.comment = { |
| 114 | projectName: 'test', |
| 115 | amendments: [ |
| 116 | { |
| 117 | fieldName: 'Blocking', |
| 118 | newOrDeltaValue: '-2 3', |
| 119 | }, |
| 120 | ], |
| 121 | commenter: { |
| 122 | displayName: 'user@example.com', |
| 123 | userId: '12345', |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | await element.updateComplete; |
| 128 | |
| 129 | const links = element.shadowRoot.querySelectorAll('mr-issue-link'); |
| 130 | |
| 131 | assert.equal(links.length, 2); |
| 132 | |
| 133 | assert.equal(links[0].text, '-2'); |
| 134 | assert.deepEqual(links[0].href, '/p/test/issues/detail?id=2'); |
| 135 | |
| 136 | assert.equal(links[1].text, '3'); |
| 137 | assert.deepEqual(links[1].href, '/p/test/issues/detail?id=3'); |
| 138 | }); |
| 139 | |
| 140 | it('renders issue links for Mergedinto issue amendments', async () => { |
| 141 | element.comment = { |
| 142 | projectName: 'test', |
| 143 | amendments: [ |
| 144 | { |
| 145 | fieldName: 'Mergedinto', |
| 146 | newOrDeltaValue: '-2 3', |
| 147 | }, |
| 148 | ], |
| 149 | commenter: { |
| 150 | displayName: 'user@example.com', |
| 151 | userId: '12345', |
| 152 | }, |
| 153 | }; |
| 154 | |
| 155 | await element.updateComplete; |
| 156 | |
| 157 | const links = element.shadowRoot.querySelectorAll('mr-issue-link'); |
| 158 | |
| 159 | assert.equal(links.length, 2); |
| 160 | |
| 161 | assert.equal(links[0].text, '-2'); |
| 162 | assert.deepEqual(links[0].href, '/p/test/issues/detail?id=2'); |
| 163 | |
| 164 | assert.equal(links[1].text, '3'); |
| 165 | assert.deepEqual(links[1].href, '/p/test/issues/detail?id=3'); |
| 166 | }); |
| 167 | |
| 168 | describe('3-dot menu options', () => { |
| 169 | it('allows showing deleted comment content', () => { |
| 170 | element._isExpandedIfDeleted = false; |
| 171 | |
| 172 | // The comment is deleted. |
| 173 | element.comment = {content: 'test', isDeleted: true, canDelete: true}; |
| 174 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 175 | 'Show comment content')); |
| 176 | |
| 177 | // The comment is spam. |
| 178 | element.comment = {content: 'test', isSpam: true, canFlag: true}; |
| 179 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 180 | 'Show comment content')); |
| 181 | }); |
| 182 | |
| 183 | it('allows hiding deleted comment content', () => { |
| 184 | element._isExpandedIfDeleted = true; |
| 185 | |
| 186 | // The comment is deleted. |
| 187 | element.comment = {content: 'test', isDeleted: true, canDelete: true}; |
| 188 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 189 | 'Hide comment content')); |
| 190 | |
| 191 | // The comment is spam. |
| 192 | element.comment = {content: 'test', isSpam: true, canFlag: true}; |
| 193 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 194 | 'Hide comment content')); |
| 195 | }); |
| 196 | |
| 197 | it('disallows showing deleted comment content', () => { |
| 198 | // The comment is deleted. |
| 199 | element.comment = {content: 'test', isDeleted: true, canDelete: false}; |
| 200 | assert.isFalse(hasOptionWithText(element._commentOptions, |
| 201 | 'Hide comment content')); |
| 202 | |
| 203 | // The comment is spam. |
| 204 | element.comment = {content: 'test', isSpam: true, canFlag: false}; |
| 205 | assert.isFalse(hasOptionWithText(element._commentOptions, |
| 206 | 'Hide comment content')); |
| 207 | }); |
| 208 | |
| 209 | it('allows deleting comment', () => { |
| 210 | element.comment = {content: 'test', isDeleted: false, canDelete: true}; |
| 211 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 212 | 'Delete comment')); |
| 213 | }); |
| 214 | |
| 215 | it('disallows deleting comment', () => { |
| 216 | element.comment = {content: 'test', isDeleted: false, canDelete: false}; |
| 217 | assert.isFalse(hasOptionWithText(element._commentOptions, |
| 218 | 'Delete comment')); |
| 219 | }); |
| 220 | |
| 221 | it('allows undeleting comment', () => { |
| 222 | element.comment = {content: 'test', isDeleted: true, canDelete: true}; |
| 223 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 224 | 'Undelete comment')); |
| 225 | }); |
| 226 | |
| 227 | it('disallows undeleting comment', () => { |
| 228 | element.comment = {content: 'test', isDeleted: true, canDelete: false}; |
| 229 | assert.isFalse(hasOptionWithText(element._commentOptions, |
| 230 | 'Undelete comment')); |
| 231 | }); |
| 232 | |
| 233 | it('allows flagging comment as spam', () => { |
| 234 | element.comment = {content: 'test', isSpam: false, canFlag: true}; |
| 235 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 236 | 'Flag comment')); |
| 237 | }); |
| 238 | |
| 239 | it('disallows flagging comment as spam', () => { |
| 240 | element.comment = {content: 'test', isSpam: false, canFlag: false}; |
| 241 | assert.isFalse(hasOptionWithText(element._commentOptions, |
| 242 | 'Flag comment')); |
| 243 | }); |
| 244 | |
| 245 | it('allows unflagging comment as spam', () => { |
| 246 | element.comment = {content: 'test', isSpam: true, canFlag: true}; |
| 247 | assert.isTrue(hasOptionWithText(element._commentOptions, |
| 248 | 'Unflag comment')); |
| 249 | }); |
| 250 | |
| 251 | it('disallows unflagging comment as spam', () => { |
| 252 | element.comment = {content: 'test', isSpam: true, canFlag: false}; |
| 253 | assert.isFalse(hasOptionWithText(element._commentOptions, |
| 254 | 'Unflag comment')); |
| 255 | }); |
| 256 | }); |
| 257 | }); |