Commit c22fe41a authored by JC Brand's avatar JC Brand

Use async/await in tests

parent cb3ba7bd
This diff is collapsed.
...@@ -11,9 +11,10 @@ ...@@ -11,9 +11,10 @@
describe("Converse", function() { describe("Converse", function() {
describe("Authentication", function () { describe("Authentication", function () {
it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(function (_converse) { it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(function (_converse) {
var url = _converse.bosh_service_url; const url = _converse.bosh_service_url;
var connection = _converse.connection; const connection = _converse.connection;
delete _converse.bosh_service_url; delete _converse.bosh_service_url;
delete _converse.connection; delete _converse.connection;
expect(_converse.initConnection).toThrow( expect(_converse.initConnection).toThrow(
...@@ -271,7 +272,9 @@ ...@@ -271,7 +272,9 @@
describe("The \"chats\" API", function() { describe("The \"chats\" API", function() {
it("has a method 'get' which returns the promise that resolves to a chat model", mock.initConverseWithPromises( it("has a method 'get' which returns the promise that resolves to a chat model", mock.initConverseWithPromises(
null, ['rosterInitialized', 'chatBoxesInitialized'], {}, function (done, _converse) { null, ['rosterInitialized', 'chatBoxesInitialized'], {},
async function (done, _converse) {
test_utils.openControlBox(); test_utils.openControlBox();
test_utils.createContacts(_converse, 'current', 2); test_utils.createContacts(_converse, 'current', 2);
_converse.emit('rosterContactsFetched'); _converse.emit('rosterContactsFetched');
...@@ -288,7 +291,7 @@ ...@@ -288,7 +291,7 @@
// Test for one JID // Test for one JID
test_utils.openChatBoxFor(_converse, jid); test_utils.openChatBoxFor(_converse, jid);
test_utils.waitUntil(() => _converse.chatboxes.length == 1).then(() => { await test_utils.waitUntil(() => _converse.chatboxes.length == 1);
box = _converse.api.chats.get(jid); box = _converse.api.chats.get(jid);
expect(box instanceof Object).toBeTruthy(); expect(box instanceof Object).toBeTruthy();
expect(box.get('box_id')).toBe(b64_sha1(jid)); expect(box.get('box_id')).toBe(b64_sha1(jid));
...@@ -297,18 +300,17 @@ ...@@ -297,18 +300,17 @@
expect(u.isVisible(chatboxview.el)).toBeTruthy(); expect(u.isVisible(chatboxview.el)).toBeTruthy();
// Test for multiple JIDs // Test for multiple JIDs
test_utils.openChatBoxFor(_converse, jid2); test_utils.openChatBoxFor(_converse, jid2);
return test_utils.waitUntil(() => _converse.chatboxes.length == 2); await test_utils.waitUntil(() => _converse.chatboxes.length == 2);
}).then(() => {
const list = _converse.api.chats.get([jid, jid2]); const list = _converse.api.chats.get([jid, jid2]);
expect(_.isArray(list)).toBeTruthy(); expect(_.isArray(list)).toBeTruthy();
expect(list[0].get('box_id')).toBe(b64_sha1(jid)); expect(list[0].get('box_id')).toBe(b64_sha1(jid));
expect(list[1].get('box_id')).toBe(b64_sha1(jid2)); expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
done(); done();
}).catch(_.partial(console.error, _));
})); }));
it("has a method 'open' which opens and returns a promise that resolves to a chat model", mock.initConverseWithPromises( it("has a method 'open' which opens and returns a promise that resolves to a chat model", mock.initConverseWithPromises(
null, ['rosterGroupsFetched', 'chatBoxesInitialized'], {}, function (done, _converse) { null, ['rosterGroupsFetched', 'chatBoxesInitialized'], {},
async function (done, _converse) {
test_utils.openControlBox(); test_utils.openControlBox();
test_utils.createContacts(_converse, 'current', 2); test_utils.createContacts(_converse, 'current', 2);
...@@ -319,7 +321,7 @@ ...@@ -319,7 +321,7 @@
// Test on chat that doesn't exist. // Test on chat that doesn't exist.
expect(_converse.api.chats.get('non-existing@jabber.org')).toBeFalsy(); expect(_converse.api.chats.get('non-existing@jabber.org')).toBeFalsy();
return _converse.api.chats.open(jid).then((box) => { const box = await _converse.api.chats.open(jid);
expect(box instanceof Object).toBeTruthy(); expect(box instanceof Object).toBeTruthy();
expect(box.get('box_id')).toBe(b64_sha1(jid)); expect(box.get('box_id')).toBe(b64_sha1(jid));
expect( expect(
...@@ -329,13 +331,11 @@ ...@@ -329,13 +331,11 @@
const chatboxview = _converse.chatboxviews.get(jid); const chatboxview = _converse.chatboxviews.get(jid);
expect(u.isVisible(chatboxview.el)).toBeTruthy(); expect(u.isVisible(chatboxview.el)).toBeTruthy();
// Test for multiple JIDs // Test for multiple JIDs
return _converse.api.chats.open([jid, jid2]); const list = await _converse.api.chats.open([jid, jid2]);
}).then((list) => {
expect(_.isArray(list)).toBeTruthy(); expect(_.isArray(list)).toBeTruthy();
expect(list[0].get('box_id')).toBe(b64_sha1(jid)); expect(list[0].get('box_id')).toBe(b64_sha1(jid));
expect(list[1].get('box_id')).toBe(b64_sha1(jid2)); expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
done(); done();
});
})); }));
}); });
...@@ -361,7 +361,7 @@ ...@@ -361,7 +361,7 @@
it("only has a method 'add' for registering plugins", mock.initConverse(function (_converse) { it("only has a method 'add' for registering plugins", mock.initConverse(function (_converse) {
expect(_.keys(converse.plugins)).toEqual(["add"]); expect(_.keys(converse.plugins)).toEqual(["add"]);
// Cheating a little bit. We clear the plugins to test more easily. // Cheating a little bit. We clear the plugins to test more easily.
var _old_plugins = _converse.pluggable.plugins; const _old_plugins = _converse.pluggable.plugins;
_converse.pluggable.plugins = []; _converse.pluggable.plugins = [];
converse.plugins.add('plugin1', {}); converse.plugins.add('plugin1', {});
expect(_.keys(_converse.pluggable.plugins)).toEqual(['plugin1']); expect(_.keys(_converse.pluggable.plugins)).toEqual(['plugin1']);
......
...@@ -58,22 +58,20 @@ ...@@ -58,22 +58,20 @@
{ whitelisted_plugins: ['converse-roomslist'], { whitelisted_plugins: ['converse-roomslist'],
allow_bookmarks: false // Makes testing easier, otherwise we allow_bookmarks: false // Makes testing easier, otherwise we
// have to mock stanza traffic. // have to mock stanza traffic.
}, function (done, _converse) { }, async function (done, _converse) {
spyOn(_converse, 'isSingleton').and.callFake(() => true); spyOn(_converse, 'isSingleton').and.callFake(() => true);
let room_els, item; let room_els, item;
test_utils.openControlBox(); test_utils.openControlBox();
_converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'}) await _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
.then(() => {
room_els = _converse.rooms_list_view.el.querySelectorAll(".available-chatroom"); room_els = _converse.rooms_list_view.el.querySelectorAll(".available-chatroom");
expect(room_els.length).toBe(1); expect(room_els.length).toBe(1);
item = room_els[0]; item = room_els[0];
expect(u.hasClass('open', item)).toBe(true); expect(u.hasClass('open', item)).toBe(true);
expect(item.textContent.trim()).toBe('coven@chat.shakespeare.lit'); expect(item.textContent.trim()).toBe('coven@chat.shakespeare.lit');
return _converse.api.rooms.open('balcony@chat.shakespeare.lit', {'nick': 'some1'}); await _converse.api.rooms.open('balcony@chat.shakespeare.lit', {'nick': 'some1'});
}).then(() => {
room_els = _converse.rooms_list_view.el.querySelectorAll(".open-room"); room_els = _converse.rooms_list_view.el.querySelectorAll(".open-room");
expect(room_els.length).toBe(2); expect(room_els.length).toBe(2);
...@@ -82,7 +80,6 @@ ...@@ -82,7 +80,6 @@
item = room_els[0]; item = room_els[0];
expect(item.textContent.trim()).toBe('balcony@chat.shakespeare.lit'); expect(item.textContent.trim()).toBe('balcony@chat.shakespeare.lit');
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})); }));
it("has an info icon which opens a details modal when clicked", mock.initConverseWithPromises( it("has an info icon which opens a details modal when clicked", mock.initConverseWithPromises(
...@@ -195,15 +192,13 @@ ...@@ -195,15 +192,13 @@
it("can be closed", mock.initConverseWithPromises( it("can be closed", mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], null, ['rosterGroupsFetched'],
{ whitelisted_plugins: ['converse-roomslist'], { whitelisted_plugins: ['converse-roomslist'],
allow_bookmarks: false // Makes testing easier, otherwise we allow_bookmarks: false // Makes testing easier, otherwise we have to mock stanza traffic.
// have to mock stanza traffic.
}, },
function (done, _converse) { async function (done, _converse) {
spyOn(window, 'confirm').and.callFake(() => true); spyOn(window, 'confirm').and.callFake(() => true);
expect(_converse.chatboxes.length).toBe(1); expect(_converse.chatboxes.length).toBe(1);
test_utils.openChatRoom(_converse, 'lounge', 'conference.shakespeare.lit', 'JC') await test_utils.openChatRoom(_converse, 'lounge', 'conference.shakespeare.lit', 'JC');
.then(() => {
expect(_converse.chatboxes.length).toBe(2); expect(_converse.chatboxes.length).toBe(2);
var room_els = _converse.rooms_list_view.el.querySelectorAll(".open-room"); var room_els = _converse.rooms_list_view.el.querySelectorAll(".open-room");
expect(room_els.length).toBe(1); expect(room_els.length).toBe(1);
...@@ -215,25 +210,20 @@ ...@@ -215,25 +210,20 @@
expect(room_els.length).toBe(0); expect(room_els.length).toBe(0);
expect(_converse.chatboxes.length).toBe(1); expect(_converse.chatboxes.length).toBe(1);
done(); done();
});
})); }));
it("shows unread messages directed at the user", mock.initConverseWithAsync( it("shows unread messages directed at the user", mock.initConverseWithAsync(
{ whitelisted_plugins: ['converse-roomslist'], { whitelisted_plugins: ['converse-roomslist'],
allow_bookmarks: false // Makes testing easier, otherwise we allow_bookmarks: false // Makes testing easier, otherwise we have to mock stanza traffic.
// have to mock stanza traffic. }, async function (done, _converse) {
}, function (done, _converse) {
let view, nick;
const room_jid = 'kitchen@conference.shakespeare.lit'; const room_jid = 'kitchen@conference.shakespeare.lit';
await test_utils.waitUntil(() => !_.isUndefined(_converse.rooms_list_view), 500);
test_utils.waitUntil(() => !_.isUndefined(_converse.rooms_list_view), 500) await test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'romeo');
.then(() => test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'romeo')) const view = _converse.chatboxviews.get(room_jid);
.then(() => {
view = _converse.chatboxviews.get(room_jid);
view.model.set({'minimized': true}); view.model.set({'minimized': true});
const contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost'; const contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
nick = mock.chatroom_names[0]; const nick = mock.chatroom_names[0];
view.model.onMessage( view.model.onMessage(
$msg({ $msg({
from: room_jid+'/'+nick, from: room_jid+'/'+nick,
...@@ -243,9 +233,7 @@ ...@@ -243,9 +233,7 @@
}).c('body').t('foo').tree()); }).c('body').t('foo').tree());
// If the user isn't mentioned, the counter doesn't get incremented, but the text of the groupchat is bold // If the user isn't mentioned, the counter doesn't get incremented, but the text of the groupchat is bold
var room_el = _converse.rooms_list_view.el.querySelector( let room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom");
".available-chatroom"
);
expect(_.includes(room_el.classList, 'unread-msgs')); expect(_.includes(room_el.classList, 'unread-msgs'));
// If the user is mentioned, the counter also gets updated // If the user is mentioned, the counter also gets updated
...@@ -257,10 +245,9 @@ ...@@ -257,10 +245,9 @@
type: 'groupchat' type: 'groupchat'
}).c('body').t('romeo: Your attention is required').tree() }).c('body').t('romeo: Your attention is required').tree()
); );
return test_utils.waitUntil(() => _converse.rooms_list_view.el.querySelectorAll(".msgs-indicator").length); await test_utils.waitUntil(() => _converse.rooms_list_view.el.querySelectorAll(".msgs-indicator").length);
}).then(() => {
spyOn(view.model, 'incrementUnreadMsgCounter').and.callThrough(); spyOn(view.model, 'incrementUnreadMsgCounter').and.callThrough();
const indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator"); let indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
expect(indicator_el.textContent).toBe('1'); expect(indicator_el.textContent).toBe('1');
view.model.onMessage( view.model.onMessage(
$msg({ $msg({
...@@ -270,19 +257,17 @@ ...@@ -270,19 +257,17 @@
type: 'groupchat' type: 'groupchat'
}).c('body').t('romeo: and another thing...').tree() }).c('body').t('romeo: and another thing...').tree()
); );
return test_utils.waitUntil(() => view.model.incrementUnreadMsgCounter.calls.count()); await test_utils.waitUntil(() => view.model.incrementUnreadMsgCounter.calls.count());
}).then(() => { indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
let indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
expect(indicator_el.textContent).toBe('2'); expect(indicator_el.textContent).toBe('2');
// When the chat gets maximized again, the unread indicators are removed // When the chat gets maximized again, the unread indicators are removed
view.model.set({'minimized': false}); view.model.set({'minimized': false});
indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator"); indicator_el = _converse.rooms_list_view.el.querySelector(".msgs-indicator");
expect(_.isNull(indicator_el)); expect(_.isNull(indicator_el));
const room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom"); room_el = _converse.rooms_list_view.el.querySelector(".available-chatroom");
expect(_.includes(room_el.classList, 'unread-msgs')).toBeFalsy(); expect(_.includes(room_el.classList, 'unread-msgs')).toBeFalsy();
done(); done();
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})); }));
}); });
})); }));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment