-
-
Notifications
You must be signed in to change notification settings - Fork 771
/
autocomplete.js
407 lines (352 loc) · 18.7 KB
/
autocomplete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*global mock, converse */
const { Strophe, u, stx } = converse.env;
describe("The nickname autocomplete feature", function () {
it("shows all autocompletion options when the user presses @",
mock.initConverse(['chatBoxesFetched'], {},
async function (_converse) {
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'tom');
const view = _converse.chatboxviews.get('lounge@montague.lit');
// Nicknames from presences
['dick', 'harry'].forEach((nick) => {
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`<presence
to="tom@montague.lit/resource"
from="lounge@montague.lit/${nick}"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="${nick}@montague.lit/resource" role="participant"/>
</x>
</presence>`));
});
// Nicknames from messages
await view.model.handleMessageStanza(
stx`<message
from="lounge@montague.lit/jane"
id="${u.getUniqueId()}"
to="romeo@montague.lit"
type="groupchat"
xmlns="jabber:client">
<body>Hello world</body>
</message>`.tree());
await u.waitUntil(() => view.model.messages.last()?.get('received'));
// Test that pressing @ brings up all options
const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
const at_event = {
'target': textarea,
'preventDefault': function preventDefault () {},
'stopPropagation': function stopPropagation () {},
'keyCode': 50,
'key': '@'
};
const message_form = view.querySelector('converse-muc-message-form');
message_form.onKeyDown(at_event);
textarea.value = '@';
message_form.onKeyUp(at_event);
await u.waitUntil(() => view.querySelectorAll('.suggestion-box__results li').length === 4);
const first_child = view.querySelector('.suggestion-box__results li:first-child converse-avatar');
expect(first_child.textContent).toBe('D');
expect(first_child.nextSibling.textContent).toBe('dick');
const second_child = view.querySelector('.suggestion-box__results li:nth-child(2) converse-avatar');
expect(second_child.textContent).toBe('H');
expect(second_child.nextSibling.textContent).toBe('harry');
const third_child = view.querySelector('.suggestion-box__results li:nth-child(3) converse-avatar');
expect(third_child.textContent).toBe('J');
expect(third_child.nextSibling.textContent).toBe('jane');
const fourth_child = view.querySelector('.suggestion-box__results li:nth-child(4) converse-avatar');
expect(fourth_child.textContent).toBe('T');
expect(fourth_child.nextSibling.textContent).toBe('tom');
}));
it("shows all autocompletion options when the user presses @ right after a new line",
mock.initConverse(['chatBoxesFetched'], {},
async function (_converse) {
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'tom');
const view = _converse.chatboxviews.get('lounge@montague.lit');
// Nicknames from presences
['dick', 'harry'].forEach((nick) => {
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`<presence
to="tom@montague.lit/resource"
from="lounge@montague.lit/${nick}"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="${nick}@montague.lit/resource" role="participant"/>
</x>
</presence>`));
});
// Nicknames from messages
await view.model.handleMessageStanza(
stx`<message
from="lounge@montague.lit/jane"
id="${u.getUniqueId()}"
to="romeo@montague.lit"
type="groupchat"
xmlns="jabber:client">
<body>Hello world</body>
</message>`.tree());
await u.waitUntil(() => view.model.messages.last()?.get('received'));
// Test that pressing @ brings up all options
const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
const at_event = {
'target': textarea,
'preventDefault': function preventDefault () {},
'stopPropagation': function stopPropagation () {},
'keyCode': 50,
'key': '@'
};
const message_form = view.querySelector('converse-muc-message-form');
textarea.value = '\n'
message_form.onKeyDown(at_event);
textarea.value = '\n@';
message_form.onKeyUp(at_event);
await u.waitUntil(() => view.querySelectorAll('.suggestion-box__results li').length === 4);
const first_child = view.querySelector('.suggestion-box__results li:first-child converse-avatar');
expect(first_child.textContent).toBe('D');
expect(first_child.nextSibling.textContent).toBe('dick');
const second_child = view.querySelector('.suggestion-box__results li:nth-child(2) converse-avatar');
expect(second_child.textContent).toBe('H');
expect(second_child.nextSibling.textContent).toBe('harry');
const third_child = view.querySelector('.suggestion-box__results li:nth-child(3) converse-avatar');
expect(third_child.textContent).toBe('J');
expect(third_child.nextSibling.textContent).toBe('jane');
const fourth_child = view.querySelector('.suggestion-box__results li:nth-child(4) converse-avatar');
expect(fourth_child.textContent).toBe('T');
expect(fourth_child.nextSibling.textContent).toBe('tom');
}));
it("shows all autocompletion options when the user presses @ right after an allowed character",
mock.initConverse(
['chatBoxesFetched'], {'opening_mention_characters':['(']},
async function (_converse) {
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'tom');
const view = _converse.chatboxviews.get('lounge@montague.lit');
// Nicknames from presences
['dick', 'harry'].forEach((nick) => {
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`<presence
to="tom@montague.lit/resource"
from="lounge@montague.lit/${nick}"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="${nick}@montague.lit/resource" role="participant"/>
</x>
</presence>`))
});
// Nicknames from messages
await view.model.handleMessageStanza(
stx`<message
from="lounge@montague.lit/jane"
id="${u.getUniqueId()}"
to="romeo@montague.lit"
type="groupchat"
xmlns="jabber:client">
<body>Hello world</body>
</message>`);
await u.waitUntil(() => view.model.messages.last()?.get('received'));
// Test that pressing @ brings up all options
const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
const at_event = {
'target': textarea,
'preventDefault': function preventDefault () {},
'stopPropagation': function stopPropagation () {},
'keyCode': 50,
'key': '@'
};
textarea.value = '('
const message_form = view.querySelector('converse-muc-message-form');
message_form.onKeyDown(at_event);
textarea.value = '(@';
message_form.onKeyUp(at_event);
await u.waitUntil(() => view.querySelectorAll('.suggestion-box__results li').length === 4);
const first_child = view.querySelector('.suggestion-box__results li:first-child converse-avatar');
expect(first_child.textContent).toBe('D');
expect(first_child.nextSibling.textContent).toBe('dick');
const second_child = view.querySelector('.suggestion-box__results li:nth-child(2) converse-avatar');
expect(second_child.textContent).toBe('H');
expect(second_child.nextSibling.textContent).toBe('harry');
const third_child = view.querySelector('.suggestion-box__results li:nth-child(3) converse-avatar');
expect(third_child.textContent).toBe('J');
expect(third_child.nextSibling.textContent).toBe('jane');
const fourth_child = view.querySelector('.suggestion-box__results li:nth-child(4) converse-avatar');
expect(fourth_child.textContent).toBe('T');
expect(fourth_child.nextSibling.textContent).toBe('tom');
}));
it("should order by query index position and length", mock.initConverse(
['chatBoxesFetched'], {}, async function (_converse) {
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'tom');
const view = _converse.chatboxviews.get('lounge@montague.lit');
// Nicknames from presences
['bernard', 'naber', 'helberlo', 'john', 'jones'].forEach((nick) => {
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`<presence
to="tom@montague.lit/resource"
from="lounge@montague.lit/${nick}"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="${nick}@montague.lit/resource" role="participant"/>
</x>
</presence>`));
});
const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
const at_event = {
'target': textarea,
'preventDefault': function preventDefault() { },
'stopPropagation': function stopPropagation() { },
'keyCode': 50,
'key': '@'
};
const message_form = view.querySelector('converse-muc-message-form');
// Test that results are sorted by query index
message_form.onKeyDown(at_event);
textarea.value = '@ber';
message_form.onKeyUp(at_event);
await u.waitUntil(() => view.querySelectorAll('.suggestion-box__results li').length === 3);
const first_child = view.querySelector('.suggestion-box__results li:first-child converse-avatar');
expect(first_child.textContent).toBe('B');
expect(first_child.nextElementSibling.textContent).toBe('ber');
expect(first_child.nextElementSibling.nextSibling.textContent).toBe('nard');
const second_child = view.querySelector('.suggestion-box__results li:nth-child(2) converse-avatar');
expect(second_child.textContent).toBe('N');
expect(second_child.nextSibling.textContent).toBe('na');
expect(second_child.nextElementSibling.textContent).toBe('ber');
const third_child = view.querySelector('.suggestion-box__results li:nth-child(3) converse-avatar');
expect(third_child.textContent).toBe('H');
expect(third_child.nextSibling.textContent).toBe('hel');
expect(third_child.nextSibling.nextSibling.textContent).toBe('ber');
expect(third_child.nextSibling.nextSibling.nextSibling.textContent).toBe('lo');
// Test that when the query index is equal, results should be sorted by length
textarea.value = '@jo';
message_form.onKeyUp(at_event);
await u.waitUntil(() => view.querySelectorAll('.suggestion-box__results li').length === 2);
// First char is the avatar initial
expect(view.querySelector('.suggestion-box__results li:first-child').textContent).toBe('Jjohn');
expect(view.querySelector('.suggestion-box__results li:nth-child(2)').textContent).toBe('Jjones');
}));
it("autocompletes when the user presses tab",
mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
const view = _converse.chatboxviews.get('lounge@montague.lit');
expect(view.model.occupants.length).toBe(1);
let presence = stx`<presence
to="romeo@montague.lit/orchard"
from="lounge@montague.lit/some1"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="some1@montague.lit/resource" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
expect(view.model.occupants.length).toBe(2);
const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
textarea.value = "hello som";
// Press tab
const tab_event = {
'target': textarea,
'preventDefault': function preventDefault () {},
'stopPropagation': function stopPropagation () {},
'keyCode': 9,
'key': 'Tab'
}
const message_form = view.querySelector('converse-muc-message-form');
message_form.onKeyDown(tab_event);
message_form.onKeyUp(tab_event);
await u.waitUntil(() => view.querySelector('.suggestion-box__results').hidden === false);
expect(view.querySelectorAll('.suggestion-box__results li').length).toBe(1);
// First char is the avatar initial
expect(view.querySelector('.suggestion-box__results li').textContent).toBe('Ssome1');
const backspace_event = {
'target': textarea,
'preventDefault': function preventDefault () {},
'keyCode': 8
}
for (let i=0; i<3; i++) {
// Press backspace 3 times to remove "som"
message_form.onKeyDown(backspace_event);
textarea.value = textarea.value.slice(0, textarea.value.length-1)
message_form.onKeyUp(backspace_event);
}
await u.waitUntil(() => view.querySelector('.suggestion-box__results').hidden === true);
presence = stx`<presence
to="romeo@montague.lit/orchard"
from="lounge@montague.lit/some2"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="some2@montague.lit/resource" role="participant"/>
</x>
</presence>`;
_converse.api.connection.get()._dataRecv(mock.createRequest(presence));
textarea.value = "hello s s";
message_form.onKeyDown(tab_event);
message_form.onKeyUp(tab_event);
await u.waitUntil(() => view.querySelector('.suggestion-box__results').hidden === false);
expect(view.querySelectorAll('.suggestion-box__results li').length).toBe(2);
const up_arrow_event = {
'target': textarea,
'preventDefault': () => (up_arrow_event.defaultPrevented = true),
'stopPropagation': function stopPropagation () {},
'keyCode': 38
}
message_form.onKeyDown(up_arrow_event);
message_form.onKeyUp(up_arrow_event);
expect(view.querySelectorAll('.suggestion-box__results li').length).toBe(2);
// First char is the avatar initial
expect(view.querySelector('.suggestion-box__results li[aria-selected="false"]').textContent).toBe('Ssome1');
expect(view.querySelector('.suggestion-box__results li[aria-selected="true"]').textContent).toBe('Ssome2');
message_form.onKeyDown({
'target': textarea,
'preventDefault': function preventDefault () {},
'stopPropagation': function stopPropagation () {},
'keyCode': 13 // Enter
});
expect(textarea.value).toBe('hello s @some2 ');
// Test that pressing tab twice selects
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`<presence
to="romeo@montague.lit/orchard"
from="lounge@montague.lit/z3r0"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="z3r0@montague.lit/resource" role="participant"/>
</x>
</presence>`));
textarea.value = "hello z";
message_form.onKeyDown(tab_event);
message_form.onKeyUp(tab_event);
await u.waitUntil(() => view.querySelector('.suggestion-box__results').hidden === false);
message_form.onKeyDown(tab_event);
message_form.onKeyUp(tab_event);
await u.waitUntil(() => textarea.value === 'hello @z3r0 ');
}));
it("autocompletes when the user presses backspace",
mock.initConverse([], {}, async function (_converse) {
await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
const view = _converse.chatboxviews.get('lounge@montague.lit');
expect(view.model.occupants.length).toBe(1);
_converse.api.connection.get()._dataRecv(mock.createRequest(
stx`<presence
to="romeo@montague.lit/orchard"
from="lounge@montague.lit/some1"
xmlns="jabber:client">
<x xmlns="${Strophe.NS.MUC_USER}">
<item affiliation="none" jid="some1@montague.lit/resource" role="participant"/>
</x>
</presence>`));
expect(view.model.occupants.length).toBe(2);
const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
textarea.value = "hello @some1 ";
// Press backspace
const backspace_event = {
'target': textarea,
'preventDefault': function preventDefault () {},
'stopPropagation': function stopPropagation () {},
'keyCode': 8,
'key': 'Backspace'
}
const message_form = view.querySelector('converse-muc-message-form');
message_form.onKeyDown(backspace_event);
textarea.value = "hello @some1"; // Mimic backspace
message_form.onKeyUp(backspace_event);
await u.waitUntil(() => view.querySelector('.suggestion-box__results').hidden === false);
expect(view.querySelectorAll('.suggestion-box__results li').length).toBe(1);
// First char is the avatar initial
expect(view.querySelector('.suggestion-box__results li').textContent).toBe('Ssome1');
}));
});