blob: 8553c36beb9e8874a2b46e91f86897d5afe9ecff [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([
88 ['Infra>UI', {docstring: 'Test docs'}],
89 ['Bird>Penguin', {docstring: 'Test docs'}],
90 ]);
91
92 assert.deepEqual(element._options(), ['Infra>UI', 'Bird>Penguin']);
93 });
94
95 it('_options gets label names', () => {
96 element.vocabularyName = 'label';
97 element._labels = new Map([
98 ['M-84', {label: 'm-84', docstring: 'Test docs'}],
99 ['Restrict-View-Bagel', {label: 'restrict-VieW-bAgEl', docstring: 'T'}],
100 ]);
101
102 assert.deepEqual(element._options(), ['m-84', 'restrict-VieW-bAgEl']);
103 });
104
105 it('_options gets member names with groups', () => {
106 element.vocabularyName = 'member';
107 element._members = {
108 userRefs: [
109 {displayName: 'penguin@island.com'},
110 {displayName: 'google@monorail.com'},
111 {displayName: 'group@birds.com'},
112 ],
113 groupRefs: [{displayName: 'group@birds.com'}],
114 };
115
116 assert.deepEqual(element._options(),
117 ['penguin@island.com', 'google@monorail.com', 'group@birds.com']);
118 });
119
120 it('_options gets owner names without groups', () => {
121 element.vocabularyName = 'owner';
122 element._members = {
123 userRefs: [
124 {displayName: 'penguin@island.com'},
125 {displayName: 'google@monorail.com'},
126 {displayName: 'group@birds.com'},
127 ],
128 groupRefs: [{displayName: 'group@birds.com'}],
129 };
130
131 assert.deepEqual(element._options(),
132 ['penguin@island.com', 'google@monorail.com']);
133 });
134
135 it('_options gets owner names without groups', () => {
136 element.vocabularyName = 'project';
137 element._projects = {
138 ownerOf: ['penguins'],
139 memberOf: ['birds'],
140 contributorTo: ['canary', 'owl-island'],
141 };
142
143 assert.deepEqual(element._options(),
144 ['penguins', 'birds', 'canary', 'owl-island']);
145 });
146
147 it('_options gives empty array for empty vocabulary name', () => {
148 element.vocabularyName = '';
149 assert.deepEqual(element._options(), []);
150 });
151
152 it('_options throws error on unknown vocabulary', () => {
153 element.vocabularyName = 'whatever';
154
155 assert.throws(element._options.bind(element),
156 'Unknown vocabulary name: whatever');
157 });
158});