blob: b82bac469268fdd4b8ae739fe26b2990b027387a [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 {MrRestrictionIndicator} from './mr-restriction-indicator.js';
7
8let element;
9
10describe('mr-restriction-indicator', () => {
11 beforeEach(() => {
12 element = document.createElement('mr-restriction-indicator');
13 document.body.appendChild(element);
14 });
15
16 afterEach(() => {
17 document.body.removeChild(element);
18 });
19
20 it('initializes', () => {
21 assert.instanceOf(element, MrRestrictionIndicator);
22 });
23
24 it('shows element only when restricted or showWarning', async () => {
25 await element.updateComplete;
26
27 assert.isTrue(element.hasAttribute('hidden'));
28
29 element.restrictions = {view: ['Google']};
30 await element.updateComplete;
31
32 assert.isFalse(element.hasAttribute('hidden'));
33
34 element.restrictions = {};
35 await element.updateComplete;
36
37 assert.isTrue(element.hasAttribute('hidden'));
38
39 element.prefs = new Map([['public_issue_notice', true]]);
40 await element.updateComplete;
41
42 assert.isFalse(element.hasAttribute('hidden'));
43
44 element.prefs = new Map([['public_issue_notice', false]]);
45 await element.updateComplete;
46
47 assert.isTrue(element.hasAttribute('hidden'));
48
49 element.prefs = new Map([]);
50 await element.updateComplete;
51
52 assert.isTrue(element.hasAttribute('hidden'));
53
54 // It is possible to have an edit or comment restriction on
55 // a public issue when the user is opted in to public issue notices.
56 // In that case, the lock icon is shown, plus a warning icon and the
57 // public issue notice.
58 element.restrictions = new Map([['edit', ['Google']]]);
59 element.prefs = new Map([['public_issue_notice', true]]);
60 await element.updateComplete;
61
62 assert.isFalse(element.hasAttribute('hidden'));
63 });
64
65 it('displays view restrictions', async () => {
66 element.restrictions = {
67 view: ['Google', 'hello'],
68 edit: ['Editor', 'world'],
69 comment: ['commentor'],
70 };
71
72 await element.updateComplete;
73
74 const restrictString =
75 'Only users with Google and hello permission or issue reporter may view.';
76 assert.equal(element._restrictionText, restrictString);
77
78 assert.include(element.shadowRoot.textContent, restrictString);
79 });
80
81 it('displays edit restrictions', async () => {
82 element.restrictions = {
83 view: [],
84 edit: ['Editor', 'world'],
85 comment: ['commentor'],
86 };
87
88 await element.updateComplete;
89
90 const restrictString =
91 'Only users with Editor and world permission may edit.';
92 assert.equal(element._restrictionText, restrictString);
93
94 assert.include(element.shadowRoot.textContent, restrictString);
95 });
96
97 it('displays comment restrictions', async () => {
98 element.restrictions = {
99 view: [],
100 edit: [],
101 comment: ['commentor'],
102 };
103
104 await element.updateComplete;
105
106 const restrictString =
107 'Only users with commentor permission or issue reporter may comment.';
108 assert.equal(element._restrictionText, restrictString);
109
110 assert.include(element.shadowRoot.textContent, restrictString);
111 });
112
113 it('displays public issue notice, if the user has that pref', async () => {
114 element.restrictions = {};
115
116 element.prefs = new Map();
117 assert.equal(element._restrictionText, '');
118 assert.include(element.shadowRoot.textContent, '');
119
120 element.prefs = new Map([['public_issue_notice', true]]);
121
122 await element.updateComplete;
123
124 const noticeString =
125 'Public issue: Please do not post confidential information.';
126 assert.equal(element._warningText, noticeString);
127
128 assert.include(element.shadowRoot.textContent, noticeString);
129 });
130});