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 | |
| 8 | import {prpcClient} from 'prpc-client-instance.js'; |
| 9 | import {MrHeader} from './mr-header.js'; |
| 10 | |
| 11 | |
| 12 | window.CS_env = { |
| 13 | token: 'foo-token', |
| 14 | }; |
| 15 | |
| 16 | let element; |
| 17 | |
| 18 | describe('mr-header', () => { |
| 19 | beforeEach(() => { |
| 20 | element = document.createElement('mr-header'); |
| 21 | document.body.appendChild(element); |
| 22 | |
| 23 | window.ga = sinon.stub(); |
| 24 | }); |
| 25 | |
| 26 | afterEach(() => { |
| 27 | document.body.removeChild(element); |
| 28 | }); |
| 29 | |
| 30 | it('initializes', () => { |
| 31 | assert.instanceOf(element, MrHeader); |
| 32 | }); |
| 33 | |
| 34 | it('presentationConfig renders', async () => { |
| 35 | element.projectName = 'best-project'; |
| 36 | element.projectThumbnailUrl = 'http://images.google.com/'; |
| 37 | element.presentationConfig = { |
| 38 | projectSummary: 'The best project', |
| 39 | }; |
| 40 | |
| 41 | await element.updateComplete; |
| 42 | |
| 43 | assert.equal(element.shadowRoot.querySelector('.project-logo').src, |
| 44 | 'http://images.google.com/'); |
| 45 | |
| 46 | assert.endsWith(element.shadowRoot.querySelector('.new-issue-link').href, |
| 47 | '/p/best-project/issues/entry'); |
| 48 | |
| 49 | assert.equal(element.shadowRoot.querySelector('.project-selector').title, |
| 50 | 'The best project'); |
| 51 | }); |
| 52 | |
| 53 | describe('issueEntryUrl', () => { |
| 54 | let oldToken; |
| 55 | |
| 56 | beforeEach(() => { |
| 57 | oldToken = prpcClient.token; |
| 58 | prpcClient.token = 'token1'; |
| 59 | |
| 60 | element.projectName = 'proj'; |
| 61 | |
| 62 | sinon.stub(element, '_origin').get(() => 'http://localhost'); |
| 63 | }); |
| 64 | |
| 65 | afterEach(() => { |
| 66 | prpcClient.token = oldToken; |
| 67 | }); |
| 68 | |
| 69 | it('updates on project change', async () => { |
| 70 | await element.updateComplete; |
| 71 | |
| 72 | assert.endsWith(element.shadowRoot.querySelector('.new-issue-link').href, |
| 73 | '/p/proj/issues/entry'); |
| 74 | |
| 75 | element.projectName = 'the-best-project'; |
| 76 | |
| 77 | await element.updateComplete; |
| 78 | |
| 79 | assert.endsWith(element.shadowRoot.querySelector('.new-issue-link').href, |
| 80 | '/p/the-best-project/issues/entry'); |
| 81 | }); |
| 82 | |
| 83 | it('generates wizard URL when customIssueEntryUrl defined', () => { |
| 84 | element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'}; |
| 85 | element.userProjects = {ownerOf: ['not-proj']}; |
| 86 | element.userDisplayName = 'test@example.com'; |
| 87 | assert.equal(element.issueEntryUrl, |
| 88 | 'https://issue.wizard?token=token1&role=&' + |
| 89 | 'continue=http://localhost/p/proj/issues/entry.do'); |
| 90 | }); |
| 91 | |
| 92 | it('uses default issue filing URL when user is not logged in', () => { |
| 93 | element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'}; |
| 94 | element.userDisplayName = ''; |
| 95 | assert.equal(element.issueEntryUrl, '/p/proj/issues/entry'); |
| 96 | }); |
| 97 | |
| 98 | it('uses default issue filing URL when user is project owner', () => { |
| 99 | element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'}; |
| 100 | element.userProjects = {ownerOf: ['proj']}; |
| 101 | assert.equal(element.issueEntryUrl, '/p/proj/issues/entry'); |
| 102 | }); |
| 103 | |
| 104 | it('uses default issue filing URL when user is project member', () => { |
| 105 | element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'}; |
| 106 | element.userProjects = {memberOf: ['proj']}; |
| 107 | assert.equal(element.issueEntryUrl, '/p/proj/issues/entry'); |
| 108 | }); |
| 109 | |
| 110 | it('uses default issue filing URL when user is project contributor', () => { |
| 111 | element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'}; |
| 112 | element.userProjects = {contributorTo: ['proj']}; |
| 113 | assert.equal(element.issueEntryUrl, '/p/proj/issues/entry'); |
| 114 | }); |
| 115 | }); |
| 116 | |
| 117 | |
| 118 | it('canAdministerProject is false when user is not logged in', () => { |
| 119 | element.userDisplayName = ''; |
| 120 | |
| 121 | assert.isFalse(element.canAdministerProject); |
| 122 | }); |
| 123 | |
| 124 | it('canAdministerProject is true when user is site admin', () => { |
| 125 | element.userDisplayName = 'test@example.com'; |
| 126 | element.isSiteAdmin = true; |
| 127 | |
| 128 | assert.isTrue(element.canAdministerProject); |
| 129 | |
| 130 | element.isSiteAdmin = false; |
| 131 | |
| 132 | assert.isFalse(element.canAdministerProject); |
| 133 | }); |
| 134 | |
| 135 | it('canAdministerProject is true when user is owner', () => { |
| 136 | element.userDisplayName = 'test@example.com'; |
| 137 | element.isSiteAdmin = false; |
| 138 | |
| 139 | element.projectName = 'chromium'; |
| 140 | element.userProjects = {ownerOf: ['chromium']}; |
| 141 | |
| 142 | assert.isTrue(element.canAdministerProject); |
| 143 | |
| 144 | element.projectName = 'v8'; |
| 145 | |
| 146 | assert.isFalse(element.canAdministerProject); |
| 147 | |
| 148 | element.userProjects = {memberOf: ['v8']}; |
| 149 | |
| 150 | assert.isFalse(element.canAdministerProject); |
| 151 | }); |
| 152 | |
| 153 | it('_projectDropdownItems tells user to sign in if not logged in', () => { |
| 154 | element.userDisplayName = ''; |
| 155 | element.loginUrl = 'http://login'; |
| 156 | |
| 157 | const items = element._projectDropdownItems; |
| 158 | |
| 159 | // My Projects |
| 160 | assert.deepEqual(items[0], { |
| 161 | text: 'Sign in to see your projects', |
| 162 | url: 'http://login', |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | it('_projectDropdownItems computes projects for user', () => { |
| 167 | element.userProjects = { |
| 168 | ownerOf: ['chromium'], |
| 169 | memberOf: ['v8'], |
| 170 | contributorTo: ['skia'], |
| 171 | starredProjects: ['gerrit'], |
| 172 | }; |
| 173 | element.userDisplayName = 'test@example.com'; |
| 174 | |
| 175 | const items = element._projectDropdownItems; |
| 176 | |
| 177 | // TODO(http://crbug.com/monorail/6236): Replace these checks with |
| 178 | // deepInclude once we upgrade Chai. |
| 179 | // My Projects |
| 180 | assert.equal(items[1].text, 'chromium'); |
| 181 | assert.equal(items[1].url, '/p/chromium/issues/list'); |
| 182 | assert.equal(items[2].text, 'skia'); |
| 183 | assert.equal(items[2].url, '/p/skia/issues/list'); |
| 184 | assert.equal(items[3].text, 'v8'); |
| 185 | assert.equal(items[3].url, '/p/v8/issues/list'); |
| 186 | |
| 187 | // Starred Projects |
| 188 | assert.equal(items[5].text, 'gerrit'); |
| 189 | assert.equal(items[5].url, '/p/gerrit/issues/list'); |
| 190 | }); |
| 191 | }); |