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,44 +272,45 @@ ...@@ -271,44 +272,45 @@
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'], {},
test_utils.openControlBox(); async function (done, _converse) {
test_utils.createContacts(_converse, 'current', 2);
_converse.emit('rosterContactsFetched'); test_utils.openControlBox();
test_utils.createContacts(_converse, 'current', 2);
// Test on chat that doesn't exist. _converse.emit('rosterContactsFetched');
expect(_converse.api.chats.get('non-existing@jabber.org')).toBeFalsy();
const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost'; // Test on chat that doesn't exist.
const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost'; expect(_converse.api.chats.get('non-existing@jabber.org')).toBeFalsy();
const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
// Test on chat that's not open const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@localhost';
let box = _converse.api.chats.get(jid);
expect(typeof box === 'undefined').toBeTruthy(); // Test on chat that's not open
expect(_converse.chatboxes.length).toBe(1); let box = _converse.api.chats.get(jid);
expect(typeof box === 'undefined').toBeTruthy();
// Test for one JID expect(_converse.chatboxes.length).toBe(1);
test_utils.openChatBoxFor(_converse, jid);
test_utils.waitUntil(() => _converse.chatboxes.length == 1).then(() => { // Test for one JID
box = _converse.api.chats.get(jid); test_utils.openChatBoxFor(_converse, jid);
expect(box instanceof Object).toBeTruthy(); await test_utils.waitUntil(() => _converse.chatboxes.length == 1);
expect(box.get('box_id')).toBe(b64_sha1(jid)); box = _converse.api.chats.get(jid);
expect(box instanceof Object).toBeTruthy();
const chatboxview = _converse.chatboxviews.get(jid); expect(box.get('box_id')).toBe(b64_sha1(jid));
expect(u.isVisible(chatboxview.el)).toBeTruthy();
// Test for multiple JIDs const chatboxview = _converse.chatboxviews.get(jid);
test_utils.openChatBoxFor(_converse, jid2); expect(u.isVisible(chatboxview.el)).toBeTruthy();
return test_utils.waitUntil(() => _converse.chatboxes.length == 2); // Test for multiple JIDs
}).then(() => { test_utils.openChatBoxFor(_converse, jid2);
const list = _converse.api.chats.get([jid, jid2]); await test_utils.waitUntil(() => _converse.chatboxes.length == 2);
expect(_.isArray(list)).toBeTruthy(); const list = _converse.api.chats.get([jid, jid2]);
expect(list[0].get('box_id')).toBe(b64_sha1(jid)); expect(_.isArray(list)).toBeTruthy();
expect(list[1].get('box_id')).toBe(b64_sha1(jid2)); expect(list[0].get('box_id')).toBe(b64_sha1(jid));
done(); expect(list[1].get('box_id')).toBe(b64_sha1(jid2));
}).catch(_.partial(console.error, _)); done();
})); }));
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,23 +321,21 @@ ...@@ -319,23 +321,21 @@
// 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(
_.keys(box), _.keys(box),
['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set'] ['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set']
); );
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']);
......
This diff is collapsed.
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