blob: 21d6c9e6d38d6a6eab12fa14df2cd089f1fbb685 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2021 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 {MrReactAutocomplete} from './mr-react-autocomplete.tsx';
9
10let element: MrReactAutocomplete;
11
12describe('mr-react-autocomplete', () => {
13 beforeEach(() => {
14 element = document.createElement('mr-react-autocomplete');
15 element.vocabularyName = 'member';
16 document.body.appendChild(element);
17
18 sinon.stub(element, 'stateChanged');
19 });
20
21 afterEach(() => {
22 document.body.removeChild(element);
23 });
24
25 it('initializes', () => {
26 assert.instanceOf(element, MrReactAutocomplete);
27 });
28
29 it('ReactDOM renders on update', async () => {
30 element.value = 'Penguin Island';
31
32 await element.updateComplete;
33
34 const input = element.querySelector('input');
35
36 assert.equal(input?.value, 'Penguin Island');
37 });
38
39 it('does not update on new copies of the same values', async () => {
40 element.fixedValues = ['test'];
41 element.value = ['hah'];
42
43 sinon.spy(element, 'updated');
44
45 await element.updateComplete;
46 sinon.assert.calledOnce(element.updated);
47
48 element.fixedValues = ['test'];
49 element.value = ['hah'];
50
51 await element.updateComplete;
52 sinon.assert.calledOnce(element.updated);
53 });
54
55 it('_getOptionDescription with component vocabulary gets docstring', () => {
56 element.vocabularyName = 'component';
57 element._components = new Map([['Infra>UI', {docstring: 'Test docs'}]]);
58 element._labels = new Map([['M-84', {docstring: 'Test label docs'}]]);
59
60 assert.equal(element._getOptionDescription('Infra>UI'), 'Test docs');
61 assert.equal(element._getOptionDescription('M-84'), '');
62 assert.equal(element._getOptionDescription('NoMatch'), '');
63 });
64
65 it('_getOptionDescription with label vocabulary gets docstring', () => {
66 element.vocabularyName = 'label';
67 element._components = new Map([['Infra>UI', {docstring: 'Test docs'}]]);
68 element._labels = new Map([['m-84', {docstring: 'Test label docs'}]]);
69
70 assert.equal(element._getOptionDescription('Infra>UI'), '');
71 assert.equal(element._getOptionDescription('M-84'), 'Test label docs');
72 assert.equal(element._getOptionDescription('NoMatch'), '');
73 });
74
75 it('_getOptionDescription with other vocabulary gets empty docstring', () => {
76 element.vocabularyName = 'owner';
77 element._components = new Map([['Infra>UI', {docstring: 'Test docs'}]]);
78 element._labels = new Map([['M-84', {docstring: 'Test label docs'}]]);
79
80 assert.equal(element._getOptionDescription('Infra>UI'), '');
81 assert.equal(element._getOptionDescription('M-84'), '');
82 assert.equal(element._getOptionDescription('NoMatch'), '');
83 });
84
85 it('_options gets component names', () => {
86 element.vocabularyName = 'component';
87 element._components = new Map([
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020088 ['Infra>UI', {path: 'Infra>UI', docstring: 'Test docs'}],
89 ['Bird>Penguin', {path: 'Bird>Penguin', docstring: 'Test docs'}],
Copybara854996b2021-09-07 19:36:02 +000090 ]);
91
92 assert.deepEqual(element._options(), ['Infra>UI', 'Bird>Penguin']);
93 });
94
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020095 it('_options does not get deprecated components', () => {
96 element.vocabularyName = 'component';
97 element._components = new Map([
98 ['Infra>UI>Deprecated', {path: 'Infra>UI>Deprecated', deprecated: true, docstring: 'Test docs'}],
99 ['Infra>UI>NotDeprecated', {path: 'Infra>UI>NotDeprecated', docstring: 'Test docs'}],
100 ]);
101
102 assert.deepEqual(element._options(), ['Infra>UI>NotDeprecated']);
103 });
104
Copybara854996b2021-09-07 19:36:02 +0000105 it('_options gets label names', () => {
106 element.vocabularyName = 'label';
107 element._labels = new Map([
108 ['M-84', {label: 'm-84', docstring: 'Test docs'}],
109 ['Restrict-View-Bagel', {label: 'restrict-VieW-bAgEl', docstring: 'T'}],
110 ]);
111
112 assert.deepEqual(element._options(), ['m-84', 'restrict-VieW-bAgEl']);
113 });
114
115 it('_options gets member names with groups', () => {
116 element.vocabularyName = 'member';
117 element._members = {
118 userRefs: [
119 {displayName: 'penguin@island.com'},
120 {displayName: 'google@monorail.com'},
121 {displayName: 'group@birds.com'},
122 ],
123 groupRefs: [{displayName: 'group@birds.com'}],
124 };
125
126 assert.deepEqual(element._options(),
127 ['penguin@island.com', 'google@monorail.com', 'group@birds.com']);
128 });
129
130 it('_options gets owner names without groups', () => {
131 element.vocabularyName = 'owner';
132 element._members = {
133 userRefs: [
134 {displayName: 'penguin@island.com'},
135 {displayName: 'google@monorail.com'},
136 {displayName: 'group@birds.com'},
137 ],
138 groupRefs: [{displayName: 'group@birds.com'}],
139 };
140
141 assert.deepEqual(element._options(),
142 ['penguin@island.com', 'google@monorail.com']);
143 });
144
145 it('_options gets owner names without groups', () => {
146 element.vocabularyName = 'project';
147 element._projects = {
148 ownerOf: ['penguins'],
149 memberOf: ['birds'],
150 contributorTo: ['canary', 'owl-island'],
151 };
152
153 assert.deepEqual(element._options(),
154 ['penguins', 'birds', 'canary', 'owl-island']);
155 });
156
157 it('_options gives empty array for empty vocabulary name', () => {
158 element.vocabularyName = '';
159 assert.deepEqual(element._options(), []);
160 });
161
162 it('_options throws error on unknown vocabulary', () => {
163 element.vocabularyName = 'whatever';
164
165 assert.throws(element._options.bind(element),
166 'Unknown vocabulary name: whatever');
167 });
168});