blob: d6b50966b33e98df37a93269734ed896892bc942 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2019 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// 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 {MrIssueHeader} from './mr-issue-header.js';
7import {store, resetState} from 'reducers/base.js';
8import * as issueV0 from 'reducers/issueV0.js';
9import {ISSUE_EDIT_PERMISSION, ISSUE_DELETE_PERMISSION,
10 ISSUE_FLAGSPAM_PERMISSION} from 'shared/consts/permissions.js';
11
12let element;
13
14describe('mr-issue-header', () => {
15 beforeEach(() => {
16 store.dispatch(resetState());
17 element = document.createElement('mr-issue-header');
18 document.body.appendChild(element);
19 });
20
21 afterEach(() => {
22 document.body.removeChild(element);
23 });
24
25 it('initializes', () => {
26 assert.instanceOf(element, MrIssueHeader);
27 });
28
29 it('updating issue id changes header', () => {
30 store.dispatch({type: issueV0.VIEW_ISSUE,
31 issueRef: {localId: 1, projectName: 'test'}});
32 store.dispatch({type: issueV0.FETCH_SUCCESS,
33 issue: {localId: 1, projectName: 'test', summary: 'test'}});
34
35 assert.deepEqual(element.issue, {localId: 1, projectName: 'test',
36 summary: 'test'});
37 });
38
39 it('_issueOptions toggles spam', () => {
40 element.issuePermissions = [ISSUE_FLAGSPAM_PERMISSION];
41 element.issue = {isSpam: false};
42 assert.isDefined(findOptionWithText(element._issueOptions,
43 'Flag issue as spam'));
44 assert.isUndefined(findOptionWithText(element._issueOptions,
45 'Un-flag issue as spam'));
46
47 element.issue = {isSpam: true};
48
49 assert.isUndefined(findOptionWithText(element._issueOptions,
50 'Flag issue as spam'));
51 assert.isDefined(findOptionWithText(element._issueOptions,
52 'Un-flag issue as spam'));
53
54 element.issuePermissions = [];
55
56 assert.isUndefined(findOptionWithText(element._issueOptions,
57 'Flag issue as spam'));
58 assert.isUndefined(findOptionWithText(element._issueOptions,
59 'Un-flag issue as spam'));
60
61 element.issue = {isSpam: false};
62 assert.isUndefined(findOptionWithText(element._issueOptions,
63 'Flag issue as spam'));
64 assert.isUndefined(findOptionWithText(element._issueOptions,
65 'Un-flag issue as spam'));
66 });
67
68 it('_issueOptions toggles convert issue', () => {
69 element.issuePermissions = [];
70 element.projectTemplates = [];
71
72 assert.isUndefined(findOptionWithText(element._issueOptions,
73 'Convert issue template'));
74
75 element.projectTemplates = [{templateName: 'test'}];
76
77 assert.isUndefined(findOptionWithText(element._issueOptions,
78 'Convert issue template'));
79
80 element.issuePermissions = [ISSUE_EDIT_PERMISSION];
81 element.projectTemplates = [];
82 assert.isUndefined(findOptionWithText(element._issueOptions,
83 'Convert issue template'));
84
85 element.projectTemplates = [{templateName: 'test'}];
86 assert.isDefined(findOptionWithText(element._issueOptions,
87 'Convert issue template'));
88 });
89
90 it('_issueOptions toggles delete', () => {
91 element.issuePermissions = [ISSUE_DELETE_PERMISSION];
92 assert.isDefined(findOptionWithText(element._issueOptions,
93 'Delete issue'));
94
95 element.issuePermissions = [];
96
97 assert.isUndefined(findOptionWithText(element._issueOptions,
98 'Delete issue'));
99 });
100
101 it('_issueOptions toggles move and copy', () => {
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100102 element.projectName = '';
Copybara854996b2021-09-07 19:36:02 +0000103 element.issuePermissions = [ISSUE_DELETE_PERMISSION];
104 assert.isDefined(findOptionWithText(element._issueOptions,
105 'Move issue'));
106 assert.isDefined(findOptionWithText(element._issueOptions,
107 'Copy issue'));
108
109 element.isRestricted = true;
110 assert.isUndefined(findOptionWithText(element._issueOptions,
111 'Move issue'));
112 assert.isUndefined(findOptionWithText(element._issueOptions,
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100113 'Copy issue'));
114
115 element.projectName = 'Chromium';
116 assert.isDefined(findOptionWithText(element._issueOptions,
117 'Move issue'));
118 assert.isUndefined(findOptionWithText(element._issueOptions,
119 'Copy issue'));
120
121 element.projectName = 'Monkeyrail';
122 assert.isUndefined(findOptionWithText(element._issueOptions,
123 'Move issue'));
124 assert.isUndefined(findOptionWithText(element._issueOptions,
125 'Copy issue'));
126
127 element.projectName = 'webrtc';
128 assert.isDefined(findOptionWithText(element._issueOptions,
129 'Move issue'));
130 assert.isUndefined(findOptionWithText(element._issueOptions,
131 'Copy issue'));
Copybara854996b2021-09-07 19:36:02 +0000132
133 element.issuePermissions = [];
134
135 assert.isUndefined(findOptionWithText(element._issueOptions,
136 'Move issue'));
137 assert.isUndefined(findOptionWithText(element._issueOptions,
138 'Copy issue'));
139 });
140
141 it('_issueOptions toggles edit description', () => {
142 element.issuePermissions = [ISSUE_EDIT_PERMISSION];
143 assert.isDefined(findOptionWithText(element._issueOptions,
144 'Edit issue description'));
145
146 element.issuePermissions = [];
147
148 assert.isUndefined(findOptionWithText(element._issueOptions,
149 'Edit issue description'));
150 });
151
152 it('markdown toggle renders on enabled projects', async () => {
153 element.projectName = 'monkeyrail';
154
155 await element.updateComplete;
156
157 // This looks for how many mr-pref-toggle buttons there are,
158 // if there are two then this project also renders on markdown.
159 const chopsToggles = element.shadowRoot.querySelectorAll('mr-pref-toggle');
160 assert.equal(chopsToggles.length, 2);
161
162 });
163
164 it('markdown toggle does not render on disabled projects', async () => {
165 element.projectName = 'moneyrail';
166
167 await element.updateComplete;
168
169 const chopsToggles = element.shadowRoot.querySelectorAll('mr-pref-toggle');
170 assert.equal(chopsToggles.length, 1);
171 });
172
173 it('markdown toggle is on by default on enabled projects', async () => {
174 element.projectName = 'monkeyrail';
175
176 await element.updateComplete;
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100177
Copybara854996b2021-09-07 19:36:02 +0000178 const chopsToggles = element.shadowRoot.querySelectorAll('mr-pref-toggle');
179 const markdownButton = chopsToggles[1];
180 assert.equal("true", markdownButton.getAttribute('initialvalue'));
181 });
182});
183
184function findOptionWithText(issueOptions, text) {
185 return issueOptions.find((option) => option.text === text);
186}