blob: 25ab0e7a47a070485b83fa843fc1669bb1bdc95c [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 {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', () => {
102 element.issuePermissions = [ISSUE_DELETE_PERMISSION];
103 assert.isDefined(findOptionWithText(element._issueOptions,
104 'Move issue'));
105 assert.isDefined(findOptionWithText(element._issueOptions,
106 'Copy issue'));
107
108 element.isRestricted = true;
109 assert.isUndefined(findOptionWithText(element._issueOptions,
110 'Move issue'));
111 assert.isUndefined(findOptionWithText(element._issueOptions,
112 'Copy issue'));
113
114 element.issuePermissions = [];
115
116 assert.isUndefined(findOptionWithText(element._issueOptions,
117 'Move issue'));
118 assert.isUndefined(findOptionWithText(element._issueOptions,
119 'Copy issue'));
120 });
121
122 it('_issueOptions toggles edit description', () => {
123 element.issuePermissions = [ISSUE_EDIT_PERMISSION];
124 assert.isDefined(findOptionWithText(element._issueOptions,
125 'Edit issue description'));
126
127 element.issuePermissions = [];
128
129 assert.isUndefined(findOptionWithText(element._issueOptions,
130 'Edit issue description'));
131 });
132
133 it('markdown toggle renders on enabled projects', async () => {
134 element.projectName = 'monkeyrail';
135
136 await element.updateComplete;
137
138 // This looks for how many mr-pref-toggle buttons there are,
139 // if there are two then this project also renders on markdown.
140 const chopsToggles = element.shadowRoot.querySelectorAll('mr-pref-toggle');
141 assert.equal(chopsToggles.length, 2);
142
143 });
144
145 it('markdown toggle does not render on disabled projects', async () => {
146 element.projectName = 'moneyrail';
147
148 await element.updateComplete;
149
150 const chopsToggles = element.shadowRoot.querySelectorAll('mr-pref-toggle');
151 assert.equal(chopsToggles.length, 1);
152 });
153
154 it('markdown toggle is on by default on enabled projects', async () => {
155 element.projectName = 'monkeyrail';
156
157 await element.updateComplete;
158
159 const chopsToggles = element.shadowRoot.querySelectorAll('mr-pref-toggle');
160 const markdownButton = chopsToggles[1];
161 assert.equal("true", markdownButton.getAttribute('initialvalue'));
162 });
163});
164
165function findOptionWithText(issueOptions, text) {
166 return issueOptions.find((option) => option.text === text);
167}