Use async/await

This commit is contained in:
JC Brand 2018-11-09 11:55:34 +01:00
parent 267d91a4f3
commit c9b540289c
2 changed files with 719 additions and 758 deletions

View File

@ -19,12 +19,11 @@
it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments", it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy') await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
.then(() => test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy')) await test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
.then(() => test_utils.openAndEnterChatRoom(_converse, 'news', 'localhost', 'dummy')) await test_utils.openAndEnterChatRoom(_converse, 'news', 'localhost', 'dummy');
.then(() => {
expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy(); expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy();
expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy(); expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
expect(u.isVisible(_converse.chatboxviews.get('news@localhost').el)).toBeTruthy(); expect(u.isVisible(_converse.chatboxviews.get('news@localhost').el)).toBeTruthy();
@ -43,42 +42,36 @@
expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined(); expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined(); expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('news@localhost')).toBeUndefined(); expect(_converse.chatboxviews.get('news@localhost')).toBeUndefined();
return test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy'); await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
}) await test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
.then(() => test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy'))
.then(() => {
expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy(); expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy();
expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy(); expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
_converse.api.roomviews.close(); _converse.api.roomviews.close();
expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined(); expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined(); expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
done(); done();
}).catch(_.partial(console.error, _));
})); }));
it("has a method 'get' which returns a wrapped groupchat (if it exists)", it("has a method 'get' which returns a wrapped groupchat (if it exists)",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
let jid, room, chatroomview;
test_utils.createContacts(_converse, 'current'); test_utils.createContacts(_converse, 'current');
test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length, 300) await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length, 300);
.then(() => test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy')) await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
.then(() => { let jid = 'lounge@localhost';
jid = 'lounge@localhost'; let room = _converse.api.rooms.get(jid);
room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid); let chatroomview = _converse.chatboxviews.get(jid);
expect(chatroomview.is_chatroom).toBeTruthy(); expect(chatroomview.is_chatroom).toBeTruthy();
expect(u.isVisible(chatroomview.el)).toBeTruthy(); expect(u.isVisible(chatroomview.el)).toBeTruthy();
chatroomview.close(); chatroomview.close();
// Test with mixed case // Test with mixed case
return test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy'); await test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy');
}).then(() => {
jid = 'Leisure@localhost'; jid = 'Leisure@localhost';
room = _converse.api.rooms.get(jid); room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
@ -103,13 +96,12 @@
room = _converse.api.rooms.get(jid); room = _converse.api.rooms.get(jid);
expect(typeof room === 'undefined').toBeTruthy(); expect(typeof room === 'undefined').toBeTruthy();
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})); }));
it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box", it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) { async function (done, _converse) {
// Mock 'getRoomFeatures', otherwise the room won't be // Mock 'getRoomFeatures', otherwise the room won't be
// displayed as it waits first for the features to be returned // displayed as it waits first for the features to be returned
@ -121,9 +113,8 @@
let chatroomview, sent_IQ, IQ_id; let chatroomview, sent_IQ, IQ_id;
test_utils.openControlBox(); test_utils.openControlBox();
test_utils.createContacts(_converse, 'current'); test_utils.createContacts(_converse, 'current');
test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length) await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length);
.then(() => _converse.api.rooms.open(jid)) let room = await _converse.api.rooms.open(jid);
.then((room) => {
// Test on groupchat that's not yet open // Test on groupchat that's not yet open
expect(room instanceof Backbone.Model).toBeTruthy(); expect(room instanceof Backbone.Model).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid); chatroomview = _converse.chatboxviews.get(jid);
@ -131,8 +122,7 @@
expect(u.isVisible(chatroomview.el)).toBeTruthy(); expect(u.isVisible(chatroomview.el)).toBeTruthy();
// Test again, now that the room exists. // Test again, now that the room exists.
return _converse.api.rooms.open(jid); room = await _converse.api.rooms.open(jid);
}).then((room) => {
expect(room instanceof Backbone.Model).toBeTruthy(); expect(room instanceof Backbone.Model).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid); chatroomview = _converse.chatboxviews.get(jid);
expect(chatroomview.is_chatroom).toBeTruthy(); expect(chatroomview.is_chatroom).toBeTruthy();
@ -141,22 +131,19 @@
// Test with mixed case in JID // Test with mixed case in JID
jid = 'Leisure@localhost'; jid = 'Leisure@localhost';
return _converse.api.rooms.open(jid); room = await _converse.api.rooms.open(jid);
}).then((room) => {
expect(room instanceof Backbone.Model).toBeTruthy(); expect(room instanceof Backbone.Model).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase()); chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(u.isVisible(chatroomview.el)).toBeTruthy(); expect(u.isVisible(chatroomview.el)).toBeTruthy();
jid = 'leisure@localhost'; jid = 'leisure@localhost';
return _converse.api.rooms.open(jid); room = await _converse.api.rooms.open(jid);
}).then((room) => {
expect(room instanceof Backbone.Model).toBeTruthy(); expect(room instanceof Backbone.Model).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase()); chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(u.isVisible(chatroomview.el)).toBeTruthy(); expect(u.isVisible(chatroomview.el)).toBeTruthy();
jid = 'leiSure@localhost'; jid = 'leiSure@localhost';
return _converse.api.rooms.open(jid); room = await _converse.api.rooms.open(jid);
}).then((room) => {
expect(room instanceof Backbone.Model).toBeTruthy(); expect(room instanceof Backbone.Model).toBeTruthy();
chatroomview = _converse.chatboxviews.get(jid.toLowerCase()); chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
expect(u.isVisible(chatroomview.el)).toBeTruthy(); expect(u.isVisible(chatroomview.el)).toBeTruthy();
@ -170,7 +157,7 @@
IQ_id = sendIQ.bind(this)(iq, callback, errback); IQ_id = sendIQ.bind(this)(iq, callback, errback);
}); });
// Test with configuration // Test with configuration
return _converse.api.rooms.open('room@conference.example.org', { room = await _converse.api.rooms.open('room@conference.example.org', {
'nick': 'some1', 'nick': 'some1',
'auto_configure': true, 'auto_configure': true,
'roomconfig': { 'roomconfig': {
@ -182,7 +169,6 @@
'whois': 'anyone' 'whois': 'anyone'
} }
}); });
}).then((room) => {
chatroomview = _converse.chatboxviews.get('room@conference.example.org'); chatroomview = _converse.chatboxviews.get('room@conference.example.org');
// We pretend this is a new room, so no disco info is returned. // We pretend this is a new room, so no disco info is returned.
@ -254,8 +240,7 @@
spyOn(chatroomview.model, 'sendConfiguration').and.callThrough(); spyOn(chatroomview.model, 'sendConfiguration').and.callThrough();
_converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild)); _converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild));
return test_utils.waitUntil(() => chatroomview.model.sendConfiguration.calls.count() === 1); await test_utils.waitUntil(() => chatroomview.model.sendConfiguration.calls.count() === 1);
}).then(() => {
var sent_stanza = sent_IQ_els.pop(); var sent_stanza = sent_IQ_els.pop();
while (sent_stanza.getAttribute('type') !== 'set') { while (sent_stanza.getAttribute('type') !== 'set') {
sent_stanza = sent_IQ_els.pop(); sent_stanza = sent_IQ_els.pop();
@ -269,7 +254,6 @@
expect(sizzle('field[var="muc#roomconfig_membersonly"] value', sent_stanza).pop().textContent).toBe('1'); expect(sizzle('field[var="muc#roomconfig_membersonly"] value', sent_stanza).pop().textContent).toBe('1');
expect(sizzle('field[var="muc#roomconfig_historylength"] value', sent_stanza).pop().textContent).toBe('20'); expect(sizzle('field[var="muc#roomconfig_historylength"] value', sent_stanza).pop().textContent).toBe('20');
done(); done();
});
})); }));
}); });
@ -278,12 +262,12 @@
it("will be created when muc_instant_rooms is set to true", it("will be created when muc_instant_rooms is set to true",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) { async function (done, _converse) {
const IQ_stanzas = _converse.connection.IQ_stanzas; const IQ_stanzas = _converse.connection.IQ_stanzas;
const sendIQ = _converse.connection.sendIQ; const sendIQ = _converse.connection.sendIQ;
const room_jid = 'lounge@localhost'; const room_jid = 'lounge@localhost';
let sent_IQ, IQ_id, view; let sent_IQ, IQ_id;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) { spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') { if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') {
sent_IQ = iq; sent_IQ = iq;
@ -292,14 +276,12 @@
sendIQ.bind(this)(iq, callback, errback); sendIQ.bind(this)(iq, callback, errback);
} }
}); });
test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy') await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
.then(() => { let stanza = await test_utils.waitUntil(() => _.get(_.filter(
return test_utils.waitUntil(() => _.get(_.filter(
IQ_stanzas, IQ_stanzas,
iq => iq.nodeTree.querySelector( iq => iq.nodeTree.querySelector(
`iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]` `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)).pop(), 'nodeTree')); )).pop(), 'nodeTree'));
}).then(stanza => {
// We pretend this is a new room, so no disco info is returned. // We pretend this is a new room, so no disco info is returned.
/* <iq from="jordie.langen@chat.example.org/converse.js-11659299" to="myroom@conference.chat.example.org" type="get"> /* <iq from="jordie.langen@chat.example.org/converse.js-11659299" to="myroom@conference.chat.example.org" type="get">
@ -311,7 +293,7 @@
* </error> * </error>
* </iq> * </iq>
*/ */
var features_stanza = $iq({ const features_stanza = $iq({
'from': 'lounge@localhost', 'from': 'lounge@localhost',
'id': stanza.getAttribute('id'), 'id': stanza.getAttribute('id'),
'to': 'dummy@localhost/desktop', 'to': 'dummy@localhost/desktop',
@ -320,7 +302,7 @@
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"}); .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
_converse.connection._dataRecv(test_utils.createRequest(features_stanza)); _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
view = _converse.chatboxviews.get('lounge@localhost'); const view = _converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'join').and.callThrough(); spyOn(view, 'join').and.callThrough();
spyOn(view, 'submitNickname').and.callThrough(); spyOn(view, 'submitNickname').and.callThrough();
@ -331,13 +313,12 @@
* node="x-roomuser-item"/> * node="x-roomuser-item"/>
* </iq> * </iq>
*/ */
return test_utils.waitUntil(() => _.filter( const node = await test_utils.waitUntil(() => _.filter(
IQ_stanzas, IQ_stanzas,
s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
).pop() ).pop()
); );
}).then(node => { stanza = node.nodeTree;
const stanza = node.nodeTree;
expect(node.toLocaleString()).toBe( expect(node.toLocaleString()).toBe(
`<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="lounge@localhost" `+ `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="lounge@localhost" `+
`type="get" xmlns="jabber:client">`+ `type="get" xmlns="jabber:client">`+
@ -357,8 +338,7 @@
}).c('error', {'type': 'cancel'}) }).c('error', {'type': 'cancel'})
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"}); .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
_converse.connection._dataRecv(test_utils.createRequest(result_stanza)); _converse.connection._dataRecv(test_utils.createRequest(result_stanza));
return test_utils.waitUntil(() => view.el.querySelector('input[name="nick"]')); const input = await test_utils.waitUntil(() => view.el.querySelector('input[name="nick"]'));
}).then(input => {
input.value = 'nicky'; input.value = 'nicky';
view.el.querySelector('input[type=submit]').click(); view.el.querySelector('input[type=submit]').click();
expect(view.submitNickname).toHaveBeenCalled(); expect(view.submitNickname).toHaveBeenCalled();
@ -405,7 +385,6 @@
`<query xmlns="http://jabber.org/protocol/muc#owner"><x type="submit" xmlns="jabber:x:data"/>`+ `<query xmlns="http://jabber.org/protocol/muc#owner"><x type="submit" xmlns="jabber:x:data"/>`+
`</query></iq>`); `</query></iq>`);
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})); }));
}); });
@ -443,10 +422,9 @@
it("shows a notification if its not anonymous", it("shows a notification if its not anonymous",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1') await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
.then(() => {
const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit'); const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
const chat_content = view.el.querySelector('.chat-content'); const chat_content = view.el.querySelector('.chat-content');
/* <presence to="dummy@localhost/_converse.js-29092160" /* <presence to="dummy@localhost/_converse.js-29092160"
@ -495,9 +473,7 @@
.toBe("This groupchat is not anonymous"); .toBe("This groupchat is not anonymous");
expect(sizzle('div.chat-info:last', chat_content).pop().textContent) expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
.toBe("some1 has entered the groupchat"); .toBe("some1 has entered the groupchat");
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
})); }));
@ -779,10 +755,9 @@
it("combines subsequent join/leave messages when users enter or exit a groupchat", it("combines subsequent join/leave messages when users enter or exit a groupchat",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy') await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy')
.then(() => {
const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit'); const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
const chat_content = view.el.querySelector('.chat-content'); const chat_content = view.el.querySelector('.chat-content');
@ -927,7 +902,6 @@
`fabio has left and re-entered the groupchat`); `fabio has left and re-entered the groupchat`);
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
})); }));
it("role-change messages that follow a MUC leave are left out", it("role-change messages that follow a MUC leave are left out",
@ -1650,9 +1624,9 @@
it("will use the user's reserved nickname, if it exists", it("will use the user's reserved nickname, if it exists",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) { async function (done, _converse) {
let sent_IQ, IQ_id, view; let sent_IQ, IQ_id;
const IQ_stanzas = _converse.connection.IQ_stanzas; const IQ_stanzas = _converse.connection.IQ_stanzas;
const sendIQ = _converse.connection.sendIQ; const sendIQ = _converse.connection.sendIQ;
const room_jid = 'lounge@localhost'; const room_jid = 'lounge@localhost';
@ -1665,16 +1639,15 @@
} }
}); });
test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy') await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
.then(() => { let stanza = await test_utils.waitUntil(() => _.get(_.filter(
return test_utils.waitUntil(() => _.get(_.filter(
IQ_stanzas, IQ_stanzas,
iq => iq.nodeTree.querySelector( iq => iq.nodeTree.querySelector(
`iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]` `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
)).pop(), 'nodeTree')); )).pop(), 'nodeTree')
}).then(stanza => { );
// We pretend this is a new room, so no disco info is returned. // We pretend this is a new room, so no disco info is returned.
var features_stanza = $iq({ const features_stanza = $iq({
from: 'lounge@localhost', from: 'lounge@localhost',
'id': stanza.getAttribute('id'), 'id': stanza.getAttribute('id'),
'to': 'dummy@localhost/desktop', 'to': 'dummy@localhost/desktop',
@ -1683,7 +1656,7 @@
.c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"}); .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
_converse.connection._dataRecv(test_utils.createRequest(features_stanza)); _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
view = _converse.chatboxviews.get('lounge@localhost'); const view = _converse.chatboxviews.get('lounge@localhost');
spyOn(view, 'join').and.callThrough(); spyOn(view, 'join').and.callThrough();
/* <iq from='hag66@shakespeare.lit/pda' /* <iq from='hag66@shakespeare.lit/pda'
@ -1694,12 +1667,11 @@
* node='x-roomuser-item'/> * node='x-roomuser-item'/>
* </iq> * </iq>
*/ */
return test_utils.waitUntil(() => _.filter( const node = await test_utils.waitUntil(() => _.filter(
IQ_stanzas, IQ_stanzas,
s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
).pop() ).pop()
); );
}).then(node => {
const iq = node.nodeTree; const iq = node.nodeTree;
expect(node.toLocaleString()).toBe( expect(node.toLocaleString()).toBe(
`<iq from="dummy@localhost/resource" id="${iq.getAttribute('id')}" to="lounge@localhost" `+ `<iq from="dummy@localhost/resource" id="${iq.getAttribute('id')}" to="lounge@localhost" `+
@ -1719,7 +1691,7 @@
* </query> * </query>
* </iq> * </iq>
*/ */
var stanza = $iq({ stanza = $iq({
'type': 'result', 'type': 'result',
'id': IQ_id, 'id': IQ_id,
'from': view.model.get('jid'), 'from': view.model.get('jid'),
@ -1734,7 +1706,7 @@
// and receives their own presence from the server. // and receives their own presence from the server.
// See example 24: // See example 24:
// http://xmpp.org/extensions/xep-0045.html#enter-pres // http://xmpp.org/extensions/xep-0045.html#enter-pres
var presence = $pres({ const presence = $pres({
to:'dummy@localhost/resource', to:'dummy@localhost/resource',
from:'lounge@localhost/thirdwitch', from:'lounge@localhost/thirdwitch',
id:'DC352437-C019-40EC-B590-AF29E879AF97' id:'DC352437-C019-40EC-B590-AF29E879AF97'
@ -1751,13 +1723,12 @@
const info_text = sizzle('.chat-content .chat-info:first', view.el).pop().textContent; const info_text = sizzle('.chat-content .chat-info:first', view.el).pop().textContent;
expect(info_text).toBe('Your nickname has been automatically set to thirdwitch'); expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
done(); done();
});
})); }));
it("allows the user to invite their roster contacts to enter the groupchat", it("allows the user to invite their roster contacts to enter the groupchat",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesFetched'], {}, null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite
// Since we don't actually fetch roster contacts, we need to // Since we don't actually fetch roster contacts, we need to
@ -1765,8 +1736,7 @@
_converse.emit('rosterContactsFetched'); _converse.emit('rosterContactsFetched');
let view; let view;
test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy') await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
.then(() => {
spyOn(_converse, 'emit'); spyOn(_converse, 'emit');
spyOn(window, 'prompt').and.callFake(function () { spyOn(window, 'prompt').and.callFake(function () {
return "Please join!"; return "Please join!";
@ -1780,8 +1750,7 @@
spyOn(view.model, 'directInvite').and.callThrough(); spyOn(view.model, 'directInvite').and.callThrough();
const chat_area = view.el.querySelector('.chat-area'); const chat_area = view.el.querySelector('.chat-area');
chat_area.parentElement.removeChild(chat_area); chat_area.parentElement.removeChild(chat_area);
return test_utils.waitUntil(() => view.el.querySelectorAll('input.invited-contact').length) await test_utils.waitUntil(() => view.el.querySelectorAll('input.invited-contact').length);
}).then(function () {
const input = view.el.querySelector('input.invited-contact'); const input = view.el.querySelector('input.invited-contact');
expect(input.getAttribute('placeholder')).toBe('Invite'); expect(input.getAttribute('placeholder')).toBe('Invite');
input.value = "Felix"; input.value = "Felix";
@ -1814,7 +1783,6 @@
); );
} }
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})); }));
it("can be joined automatically, based upon a received invite", it("can be joined automatically, based upon a received invite",
@ -1974,9 +1942,9 @@
it("shows the room topic in the header", it("shows the room topic in the header",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () { await test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
const text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org'; const text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
const stanza = Strophe.xmlHtmlNode( const stanza = Strophe.xmlHtmlNode(
'<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+ '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
@ -1991,15 +1959,14 @@
expect(sizzle('.chat-topic:last').pop().textContent).toBe(text); expect(sizzle('.chat-topic:last').pop().textContent).toBe(text);
expect(view.el.querySelector('.chatroom-description').textContent).toBe(text); expect(view.el.querySelector('.chatroom-description').textContent).toBe(text);
done(); done();
});
})); }));
it("escapes the subject before rendering it, to avoid JS-injection attacks", it("escapes the subject before rendering it, to avoid JS-injection attacks",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () { await test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
spyOn(window, 'alert'); spyOn(window, 'alert');
const subject = '<img src="x" onerror="alert(\'XSS\');"/>'; const subject = '<img src="x" onerror="alert(\'XSS\');"/>';
const view = _converse.chatboxviews.get('jdev@conference.jabber.org'); const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
@ -2011,13 +1978,12 @@
expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm'); expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
expect(sizzle('.chat-topic:last').pop().textContent).toBe(subject); expect(sizzle('.chat-topic:last').pop().textContent).toBe(subject);
done(); done();
});
})); }));
it("informs users if their nicknames has been changed.", it("informs users if their nicknames has been changed.",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
/* The service then sends two presence stanzas to the full JID /* The service then sends two presence stanzas to the full JID
* of each occupant (including the occupant who is changing his * of each occupant (including the occupant who is changing his
@ -2055,7 +2021,7 @@
* </presence> * </presence>
*/ */
const __ = _converse.__; const __ = _converse.__;
test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick').then(function () { await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick');
const view = _converse.chatboxviews.get('lounge@localhost'); const view = _converse.chatboxviews.get('lounge@localhost');
const chat_content = view.el.querySelector('.chat-content'); const chat_content = view.el.querySelector('.chat-content');
@ -2118,7 +2084,6 @@
expect(occupants.childNodes.length).toBe(1); expect(occupants.childNodes.length).toBe(1);
expect(sizzle('.occupant-nick:first', occupants).pop().textContent).toBe("newnick"); expect(sizzle('.occupant-nick:first', occupants).pop().textContent).toBe("newnick");
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})); }));
it("queries for the groupchat information before attempting to join the user", it("queries for the groupchat information before attempting to join the user",

View File

@ -15,16 +15,13 @@
} }
utils.waitUntil = waitUntilPromise.default; utils.waitUntil = waitUntilPromise.default;
utils.waitUntilDiscoConfirmed = function (_converse, entity_jid, identities, features, items, type) { utils.waitUntilDiscoConfirmed = async function (_converse, entity_jid, identities, features, items, type='info') {
if (_.isNil(type)) { const iq = await utils.waitUntil(() => {
type = 'info';
}
return utils.waitUntil(() => {
return _.filter( return _.filter(
_converse.connection.IQ_stanzas, _converse.connection.IQ_stanzas,
(iq) => sizzle(`iq[to="${entity_jid}"] query[xmlns="http://jabber.org/protocol/disco#${type}"]`, iq.nodeTree).length (iq) => sizzle(`iq[to="${entity_jid}"] query[xmlns="http://jabber.org/protocol/disco#${type}"]`, iq.nodeTree).length
).pop(); ).pop();
}, 300).then(iq => { }, 300);
const stanza = $iq({ const stanza = $iq({
'type': 'result', 'type': 'result',
'from': entity_jid, 'from': entity_jid,
@ -42,7 +39,6 @@
stanza.c('item', {'jid': item}).up(); stanza.c('item', {'jid': item}).up();
}); });
_converse.connection._dataRecv(utils.createRequest(stanza)); _converse.connection._dataRecv(utils.createRequest(stanza));
}).catch(_.partial(console.error, _));
} }
utils.createRequest = function (iq) { utils.createRequest = function (iq) {