blob: e2905840f8ce43964b6a657c8620b242377cdacc [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// 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
5import {assert} from 'chai';
6import sinon from 'sinon';
7
8import {prpcClient} from 'prpc-client-instance.js';
9import {MrHeader} from './mr-header.js';
10
11
12window.CS_env = {
13 token: 'foo-token',
14};
15
16let element;
17
18describe('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';
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020087 assert.equal(element.issueEntryUrl, '/p/proj/issues/wizard');
Copybara854996b2021-09-07 19:36:02 +000088 });
89
90 it('uses default issue filing URL when user is not logged in', () => {
91 element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'};
92 element.userDisplayName = '';
93 assert.equal(element.issueEntryUrl, '/p/proj/issues/entry');
94 });
95
96 it('uses default issue filing URL when user is project owner', () => {
97 element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'};
98 element.userProjects = {ownerOf: ['proj']};
99 assert.equal(element.issueEntryUrl, '/p/proj/issues/entry');
100 });
101
102 it('uses default issue filing URL when user is project member', () => {
103 element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'};
104 element.userProjects = {memberOf: ['proj']};
105 assert.equal(element.issueEntryUrl, '/p/proj/issues/entry');
106 });
107
108 it('uses default issue filing URL when user is project contributor', () => {
109 element.presentationConfig = {customIssueEntryUrl: 'https://issue.wizard'};
110 element.userProjects = {contributorTo: ['proj']};
111 assert.equal(element.issueEntryUrl, '/p/proj/issues/entry');
112 });
113 });
114
115
116 it('canAdministerProject is false when user is not logged in', () => {
117 element.userDisplayName = '';
118
119 assert.isFalse(element.canAdministerProject);
120 });
121
122 it('canAdministerProject is true when user is site admin', () => {
123 element.userDisplayName = 'test@example.com';
124 element.isSiteAdmin = true;
125
126 assert.isTrue(element.canAdministerProject);
127
128 element.isSiteAdmin = false;
129
130 assert.isFalse(element.canAdministerProject);
131 });
132
133 it('canAdministerProject is true when user is owner', () => {
134 element.userDisplayName = 'test@example.com';
135 element.isSiteAdmin = false;
136
137 element.projectName = 'chromium';
138 element.userProjects = {ownerOf: ['chromium']};
139
140 assert.isTrue(element.canAdministerProject);
141
142 element.projectName = 'v8';
143
144 assert.isFalse(element.canAdministerProject);
145
146 element.userProjects = {memberOf: ['v8']};
147
148 assert.isFalse(element.canAdministerProject);
149 });
150
151 it('_projectDropdownItems tells user to sign in if not logged in', () => {
152 element.userDisplayName = '';
153 element.loginUrl = 'http://login';
154
155 const items = element._projectDropdownItems;
156
157 // My Projects
158 assert.deepEqual(items[0], {
159 text: 'Sign in to see your projects',
160 url: 'http://login',
161 });
162 });
163
164 it('_projectDropdownItems computes projects for user', () => {
165 element.userProjects = {
166 ownerOf: ['chromium'],
167 memberOf: ['v8'],
168 contributorTo: ['skia'],
169 starredProjects: ['gerrit'],
170 };
171 element.userDisplayName = 'test@example.com';
172
173 const items = element._projectDropdownItems;
174
175 // TODO(http://crbug.com/monorail/6236): Replace these checks with
176 // deepInclude once we upgrade Chai.
177 // My Projects
178 assert.equal(items[1].text, 'chromium');
179 assert.equal(items[1].url, '/p/chromium/issues/list');
180 assert.equal(items[2].text, 'skia');
181 assert.equal(items[2].url, '/p/skia/issues/list');
182 assert.equal(items[3].text, 'v8');
183 assert.equal(items[3].url, '/p/v8/issues/list');
184
185 // Starred Projects
186 assert.equal(items[5].text, 'gerrit');
187 assert.equal(items[5].url, '/p/gerrit/issues/list');
188 });
189});