blob: 13ddea3985410f0938ac96ec7a0e292b689a2667 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2016 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5const feedData = {
6 'open': [{name: 'New', doc: 'Newly reported'},
7 {name: 'Started', doc: 'Work has begun'}],
8 'closed': [{name: 'Fixed', doc: 'Problem was fixed'},
9 {name: 'Invalid', doc: 'Bad issue report'}],
10 'labels': [{name: 'Type-Defect', doc: 'Something is broken'},
11 {name: 'Type-Enhancement', doc: 'It could be better'},
12 {name: 'Priority-High', doc: 'Urgent'},
13 {name: 'Priority-Low', doc: 'Not so urgent'},
14 {name: 'Hot', doc: ''},
15 {name: 'Cold', doc: ''}],
16 'members': [{name: 'jrobbins', doc: ''},
17 {name: 'jrobbins@chromium.org', doc: ''}],
18 'excl_prefixes': [],
19 'strict': false,
20};
21
22function setUp() {
23 TKR_autoCompleteFeedName = 'issueOptions';
24}
25
26/**
27 * The assertEquals method cannot do element-by-element comparisons.
28 * A search of how other teams write JS unit tests turned up this
29 * way to compare arrays.
30 */
31function assertElementsEqual(arrayA, arrayB) {
32 assertEquals(arrayA.join(' ;; '), arrayB.join(' ;; '));
33}
34
35function completionsEqual(strings, completions) {
36 if (strings.length != completions.length) {
37 return false;
38 }
39 for (let i = 0; i < strings.length; i++) {
40 if (strings[i] != completions[i].value) {
41 return false;
42 }
43 }
44 return true;
45}
46
47function assertHasCompletion(s, acStore) {
48 const ch = s.charAt(0).toLowerCase();
49 const firstCharMapArray = acStore.firstCharMap_[ch];
50 assertNotNull(!firstCharMapArray);
51 for (let i = 0; i < firstCharMapArray.length; i++) {
52 if (s == firstCharMapArray[i].value) return;
53 }
54 fail('completion ' + s + ' not found in acStore[' +
55 acStoreToString(acStore) + ']');
56}
57
58function assertHasAllCompletions(stringArray, acStore) {
59 for (let i = 0; i < stringArray.length; i++) {
60 assertHasCompletion(stringArray[i], acStore);
61 }
62}
63
64function acStoreToString(acStore) {
65 const allCompletions = [];
66 for (const ch in acStore.firstCharMap_) {
67 if (acStore.firstCharMap_.hasOwnProperty(ch)) {
68 const firstCharArray = acStore.firstCharMap_[ch];
69 for (let i = 0; i < firstCharArray.length; i++) {
70 allCompletions[firstCharArray[i].value] = true;
71 }
72 }
73 }
74 const parts = [];
75 for (const comp in allCompletions) {
76 if (allCompletions.hasOwnProperty(comp)) {
77 parts.push(comp);
78 }
79 }
80 return parts.join(', ');
81}
82
83function testSetUpStatusStore() {
84 TKR_setUpStatusStore(feedData.open, feedData.closed);
85 assertElementsEqual(
86 ['New', 'Started', 'Fixed', 'Invalid'],
87 TKR_statusWords);
88 assertHasAllCompletions(
89 ['New', 'Started', 'Fixed', 'Invalid'],
90 TKR_statusStore);
91}
92
93function testSetUpSearchStore() {
94 TKR_setUpSearchStore(
95 feedData.labels, feedData.members, feedData.open, feedData.closed);
96 assertHasAllCompletions(
97 ['status:New', 'status:Started', 'status:Fixed', 'status:Invalid',
98 '-status:New', '-status:Started', '-status:Fixed', '-status:Invalid',
99 'Type=Defect', '-Type=Defect', 'Type=Enhancement', '-Type=Enhancement',
100 'label:Hot', 'label:Cold', '-label:Hot', '-label:Cold',
101 'owner:jrobbins', 'cc:jrobbins', '-owner:jrobbins', '-cc:jrobbins',
102 'summary:', 'opened-after:today-1', 'commentby:me', 'reporter:me'],
103 TKR_searchStore);
104}
105
106function testSetUpQuickEditStore() {
107 TKR_setUpQuickEditStore(
108 feedData.labels, feedData.members, feedData.open, feedData.closed);
109 assertHasAllCompletions(
110 ['status=New', 'status=Started', 'status=Fixed', 'status=Invalid',
111 'Type=Defect', 'Type=Enhancement', 'Hot', 'Cold', '-Hot', '-Cold',
112 'owner=jrobbins', 'owner=me', 'cc=jrobbins', 'cc=me', 'cc=-jrobbins',
113 'cc=-me', 'summary=""', 'owner=----'],
114 TKR_quickEditStore);
115}
116
117function testSetUpLabelStore() {
118 TKR_setUpLabelStore(feedData.labels);
119 assertHasAllCompletions(
120 ['Type-Defect', 'Type-Enhancement', 'Hot', 'Cold'],
121 TKR_labelStore);
122}
123
124function testSetUpMembersStore() {
125 TKR_setUpMemberStore(feedData.members);
126 assertHasAllCompletions(
127 ['jrobbins', 'jrobbins@chromium.org'],
128 TKR_memberListStore);
129}