2018-08-14 12:50:47 +02:00
|
|
|
(function (root, factory) {
|
|
|
|
define([
|
|
|
|
"jasmine",
|
|
|
|
"mock",
|
|
|
|
"test-utils"
|
|
|
|
], factory);
|
|
|
|
} (this, function (jasmine, mock, test_utils) {
|
|
|
|
"use strict";
|
|
|
|
const $pres = converse.env.$pres;
|
2020-02-20 19:14:34 +01:00
|
|
|
const $msg = converse.env.$msg;
|
2018-08-14 12:50:47 +02:00
|
|
|
const Strophe = converse.env.Strophe;
|
2020-02-20 19:14:34 +01:00
|
|
|
const u = converse.env.utils;
|
2018-08-14 12:50:47 +02:00
|
|
|
|
2018-08-15 17:22:24 +02:00
|
|
|
describe("The nickname autocomplete feature", function () {
|
2018-08-14 12:50:47 +02:00
|
|
|
|
2018-08-14 20:15:38 +02:00
|
|
|
it("shows all autocompletion options when the user presses @",
|
2019-02-12 14:21:45 +01:00
|
|
|
mock.initConverse(
|
2019-10-11 16:38:01 +02:00
|
|
|
['rosterGroupsFetched'], {},
|
2019-03-04 09:42:16 +01:00
|
|
|
async function (done, _converse) {
|
2018-08-14 20:15:38 +02:00
|
|
|
|
2019-07-03 14:18:46 +02:00
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'tom');
|
2019-06-03 07:58:51 +02:00
|
|
|
const view = _converse.chatboxviews.get('lounge@montague.lit');
|
2018-08-14 20:15:38 +02:00
|
|
|
|
2020-02-20 19:14:34 +01:00
|
|
|
// Nicknames from presences
|
2019-03-04 09:42:16 +01:00
|
|
|
['dick', 'harry'].forEach((nick) => {
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(
|
|
|
|
$pres({
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'tom@montague.lit/resource',
|
|
|
|
'from': `lounge@montague.lit/${nick}`
|
2019-03-04 09:42:16 +01:00
|
|
|
})
|
|
|
|
.c('x', {xmlns: Strophe.NS.MUC_USER})
|
|
|
|
.c('item', {
|
|
|
|
'affiliation': 'none',
|
2019-06-03 07:58:51 +02:00
|
|
|
'jid': `${nick}@montague.lit/resource`,
|
2019-03-04 09:42:16 +01:00
|
|
|
'role': 'participant'
|
|
|
|
})));
|
|
|
|
});
|
2018-08-14 20:15:38 +02:00
|
|
|
|
2020-02-20 19:14:34 +01:00
|
|
|
// Nicknames from messages
|
|
|
|
const msg = $msg({
|
|
|
|
from: 'lounge@montague.lit/jane',
|
|
|
|
id: u.getUniqueId(),
|
|
|
|
to: 'romeo@montague.lit',
|
|
|
|
type: 'groupchat'
|
|
|
|
}).c('body').t('Hello world').tree();
|
|
|
|
await view.model.onMessage(msg);
|
|
|
|
|
2019-03-04 09:42:16 +01:00
|
|
|
// Test that pressing @ brings up all options
|
|
|
|
const textarea = view.el.querySelector('textarea.chat-textarea');
|
|
|
|
const at_event = {
|
|
|
|
'target': textarea,
|
2019-08-05 11:14:49 +02:00
|
|
|
'preventDefault': function preventDefault () {},
|
|
|
|
'stopPropagation': function stopPropagation () {},
|
2019-03-04 09:42:16 +01:00
|
|
|
'keyCode': 50,
|
|
|
|
'key': '@'
|
|
|
|
};
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(at_event);
|
2019-03-04 09:42:16 +01:00
|
|
|
textarea.value = '@';
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyUp(at_event);
|
2018-08-14 20:15:38 +02:00
|
|
|
|
2020-02-20 19:14:34 +01:00
|
|
|
expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(4);
|
2019-03-28 12:35:40 +01:00
|
|
|
expect(view.el.querySelector('.suggestion-box__results li:first-child').textContent).toBe('dick');
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results li:nth-child(2)').textContent).toBe('harry');
|
2020-02-20 19:14:34 +01:00
|
|
|
expect(view.el.querySelector('.suggestion-box__results li:nth-child(3)').textContent).toBe('jane');
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results li:nth-child(4)').textContent).toBe('tom');
|
2019-03-04 09:42:16 +01:00
|
|
|
done();
|
2018-08-14 20:15:38 +02:00
|
|
|
}));
|
|
|
|
|
2018-08-14 12:50:47 +02:00
|
|
|
it("autocompletes when the user presses tab",
|
2019-02-12 14:21:45 +01:00
|
|
|
mock.initConverse(
|
2019-10-11 16:38:01 +02:00
|
|
|
['rosterGroupsFetched'], {},
|
2019-01-25 11:53:07 +01:00
|
|
|
async function (done, _converse) {
|
|
|
|
|
2019-07-03 14:18:46 +02:00
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
|
2019-06-03 07:58:51 +02:00
|
|
|
const view = _converse.chatboxviews.get('lounge@montague.lit');
|
2019-01-25 11:53:07 +01:00
|
|
|
expect(view.model.occupants.length).toBe(1);
|
|
|
|
let presence = $pres({
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'from': 'lounge@montague.lit/some1'
|
2019-01-25 11:53:07 +01:00
|
|
|
})
|
|
|
|
.c('x', {xmlns: Strophe.NS.MUC_USER})
|
|
|
|
.c('item', {
|
|
|
|
'affiliation': 'none',
|
2019-06-03 07:58:51 +02:00
|
|
|
'jid': 'some1@montague.lit/resource',
|
2019-01-25 11:53:07 +01:00
|
|
|
'role': 'participant'
|
2018-08-14 15:15:04 +02:00
|
|
|
});
|
2019-01-25 11:53:07 +01:00
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(presence));
|
|
|
|
expect(view.model.occupants.length).toBe(2);
|
|
|
|
|
|
|
|
const textarea = view.el.querySelector('textarea.chat-textarea');
|
|
|
|
textarea.value = "hello som";
|
|
|
|
|
|
|
|
// Press tab
|
|
|
|
const tab_event = {
|
|
|
|
'target': textarea,
|
2019-08-05 11:14:49 +02:00
|
|
|
'preventDefault': function preventDefault () {},
|
|
|
|
'stopPropagation': function stopPropagation () {},
|
2019-03-04 09:42:16 +01:00
|
|
|
'keyCode': 9,
|
|
|
|
'key': 'Tab'
|
2019-01-25 11:53:07 +01:00
|
|
|
}
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(tab_event);
|
|
|
|
view.onKeyUp(tab_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
expect(view.el.querySelector('.suggestion-box__results').hidden).toBeFalsy();
|
|
|
|
expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(1);
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results li').textContent).toBe('some1');
|
|
|
|
|
|
|
|
const backspace_event = {
|
|
|
|
'target': textarea,
|
2019-08-05 11:14:49 +02:00
|
|
|
'preventDefault': function preventDefault () {},
|
2019-01-25 11:53:07 +01:00
|
|
|
'keyCode': 8
|
|
|
|
}
|
|
|
|
for (var i=0; i<3; i++) {
|
|
|
|
// Press backspace 3 times to remove "som"
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(backspace_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
textarea.value = textarea.value.slice(0, textarea.value.length-1)
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyUp(backspace_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
}
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results').hidden).toBeTruthy();
|
|
|
|
|
|
|
|
presence = $pres({
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'from': 'lounge@montague.lit/some2'
|
2019-01-25 11:53:07 +01:00
|
|
|
})
|
|
|
|
.c('x', {xmlns: Strophe.NS.MUC_USER})
|
|
|
|
.c('item', {
|
|
|
|
'affiliation': 'none',
|
2019-06-03 07:58:51 +02:00
|
|
|
'jid': 'some2@montague.lit/resource',
|
2019-01-25 11:53:07 +01:00
|
|
|
'role': 'participant'
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(presence));
|
|
|
|
|
|
|
|
textarea.value = "hello s s";
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(tab_event);
|
|
|
|
view.onKeyUp(tab_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
expect(view.el.querySelector('.suggestion-box__results').hidden).toBeFalsy();
|
|
|
|
expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(2);
|
|
|
|
|
|
|
|
const up_arrow_event = {
|
|
|
|
'target': textarea,
|
|
|
|
'preventDefault': () => (up_arrow_event.defaultPrevented = true),
|
2019-08-05 11:14:49 +02:00
|
|
|
'stopPropagation': function stopPropagation () {},
|
2019-01-25 11:53:07 +01:00
|
|
|
'keyCode': 38
|
|
|
|
}
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(up_arrow_event);
|
|
|
|
view.onKeyUp(up_arrow_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(2);
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results li[aria-selected="false"]').textContent).toBe('some1');
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results li[aria-selected="true"]').textContent).toBe('some2');
|
|
|
|
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown({
|
2019-01-25 11:53:07 +01:00
|
|
|
'target': textarea,
|
2019-08-05 11:14:49 +02:00
|
|
|
'preventDefault': function preventDefault () {},
|
|
|
|
'stopPropagation': function stopPropagation () {},
|
2019-01-25 11:53:07 +01:00
|
|
|
'keyCode': 13 // Enter
|
|
|
|
});
|
|
|
|
expect(textarea.value).toBe('hello s @some2 ');
|
|
|
|
|
|
|
|
// Test that pressing tab twice selects
|
|
|
|
presence = $pres({
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'from': 'lounge@montague.lit/z3r0'
|
2019-01-25 11:53:07 +01:00
|
|
|
})
|
|
|
|
.c('x', {xmlns: Strophe.NS.MUC_USER})
|
|
|
|
.c('item', {
|
|
|
|
'affiliation': 'none',
|
2019-06-03 07:58:51 +02:00
|
|
|
'jid': 'z3r0@montague.lit/resource',
|
2019-01-25 11:53:07 +01:00
|
|
|
'role': 'participant'
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(presence));
|
|
|
|
textarea.value = "hello z";
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(tab_event);
|
|
|
|
view.onKeyUp(tab_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(tab_event);
|
|
|
|
view.onKeyUp(tab_event);
|
2019-01-25 11:53:07 +01:00
|
|
|
expect(textarea.value).toBe('hello @z3r0 ');
|
|
|
|
done();
|
2018-08-14 12:50:47 +02:00
|
|
|
}));
|
2019-03-04 10:21:04 +01:00
|
|
|
|
|
|
|
it("autocompletes when the user presses backspace",
|
|
|
|
mock.initConverse(
|
2019-10-11 16:38:01 +02:00
|
|
|
['rosterGroupsFetched'], {},
|
2019-03-04 10:21:04 +01:00
|
|
|
async function (done, _converse) {
|
|
|
|
|
2019-07-03 14:18:46 +02:00
|
|
|
await test_utils.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
|
2019-06-03 07:58:51 +02:00
|
|
|
const view = _converse.chatboxviews.get('lounge@montague.lit');
|
2019-03-04 10:21:04 +01:00
|
|
|
expect(view.model.occupants.length).toBe(1);
|
|
|
|
const presence = $pres({
|
2019-06-03 07:58:51 +02:00
|
|
|
'to': 'romeo@montague.lit/orchard',
|
|
|
|
'from': 'lounge@montague.lit/some1'
|
2019-03-04 10:21:04 +01:00
|
|
|
})
|
|
|
|
.c('x', {xmlns: Strophe.NS.MUC_USER})
|
|
|
|
.c('item', {
|
|
|
|
'affiliation': 'none',
|
2019-06-03 07:58:51 +02:00
|
|
|
'jid': 'some1@montague.lit/resource',
|
2019-03-04 10:21:04 +01:00
|
|
|
'role': 'participant'
|
|
|
|
});
|
|
|
|
_converse.connection._dataRecv(test_utils.createRequest(presence));
|
|
|
|
expect(view.model.occupants.length).toBe(2);
|
|
|
|
|
|
|
|
const textarea = view.el.querySelector('textarea.chat-textarea');
|
|
|
|
textarea.value = "hello @some1 ";
|
|
|
|
|
|
|
|
// Press backspace
|
|
|
|
const backspace_event = {
|
|
|
|
'target': textarea,
|
2019-08-05 11:14:49 +02:00
|
|
|
'preventDefault': function preventDefault () {},
|
|
|
|
'stopPropagation': function stopPropagation () {},
|
2019-03-04 10:21:04 +01:00
|
|
|
'keyCode': 8,
|
|
|
|
'key': 'Backspace'
|
|
|
|
}
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyDown(backspace_event);
|
2019-03-04 10:21:04 +01:00
|
|
|
textarea.value = "hello @some1"; // Mimic backspace
|
2019-05-26 10:58:52 +02:00
|
|
|
view.onKeyUp(backspace_event);
|
2019-03-04 10:21:04 +01:00
|
|
|
expect(view.el.querySelector('.suggestion-box__results').hidden).toBeFalsy();
|
|
|
|
expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(1);
|
|
|
|
expect(view.el.querySelector('.suggestion-box__results li').textContent).toBe('some1');
|
|
|
|
done();
|
|
|
|
}));
|
2018-08-14 12:50:47 +02:00
|
|
|
});
|
|
|
|
}));
|