Commit 4e6ac905 authored by JC Brand's avatar JC Brand

Async/await in controlbox tests

parent c22fe41a
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
it("can be used to add contact and it checks for case-sensivity", it("can be used to add contact and it checks for case-sensivity",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
spyOn(_converse, 'emit'); spyOn(_converse, 'emit');
spyOn(_converse.rosterview, 'update').and.callThrough(); spyOn(_converse.rosterview, 'update').and.callThrough();
...@@ -55,14 +55,11 @@ ...@@ -55,14 +55,11 @@
ask: 'subscribe', ask: 'subscribe',
fullname: mock.pend_names[0] fullname: mock.pend_names[0]
}); });
test_utils.waitUntil(function () { await test_utils.waitUntil(() => $(_converse.rosterview.el).find('.roster-group li:visible').length, 700);
return $(_converse.rosterview.el).find('.roster-group li:visible').length; // Checking that only one entry is created because both JID is same (Case sensitive check)
}, 700).then(function () { expect($(_converse.rosterview.el).find('li:visible').length).toBe(1);
// Checking that only one entry is created because both JID is same (Case sensitive check) expect(_converse.rosterview.update).toHaveBeenCalled();
expect($(_converse.rosterview.el).find('li:visible').length).toBe(1); done();
expect(_converse.rosterview.update).toHaveBeenCalled();
done();
});
})); }));
it("shows the number of unread mentions received", it("shows the number of unread mentions received",
...@@ -130,7 +127,7 @@ ...@@ -130,7 +127,7 @@
it("can be used to set the current user's chat status", it("can be used to set the current user's chat status",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openControlBox(); test_utils.openControlBox();
...@@ -138,48 +135,42 @@ ...@@ -138,48 +135,42 @@
cbview.el.querySelector('.change-status').click() cbview.el.querySelector('.change-status').click()
var modal = _converse.xmppstatusview.status_modal; var modal = _converse.xmppstatusview.status_modal;
test_utils.waitUntil(function () { await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
return u.isVisible(modal.el); const view = _converse.xmppstatusview;
}, 1000).then(function () { spyOn(_converse, 'emit');
var view = _converse.xmppstatusview; modal.el.querySelector('label[for="radio-busy"]').click(); // Change status to "dnd"
spyOn(_converse, 'emit'); modal.el.querySelector('[type="submit"]').click();
modal.el.querySelector('label[for="radio-busy"]').click(); // Change status to "dnd"
modal.el.querySelector('[type="submit"]').click(); expect(_converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
expect($(view.el).find('.xmpp-status span:first-child').hasClass('online')).toBe(false);
expect(_converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd'); expect($(view.el).find('.xmpp-status span:first-child').hasClass('dnd')).toBe(true);
expect($(view.el).find('.xmpp-status span:first-child').hasClass('online')).toBe(false); expect(view.el.querySelector('.xmpp-status span:first-child').textContent.trim()).toBe('I am busy');
expect($(view.el).find('.xmpp-status span:first-child').hasClass('dnd')).toBe(true); done();
expect(view.el.querySelector('.xmpp-status span:first-child').textContent.trim()).toBe('I am busy');
done();
});
})); }));
it("can be used to set a custom status message", it("can be used to set a custom status message",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.openControlBox(); test_utils.openControlBox();
var cbview = _converse.chatboxviews.get('controlbox'); const cbview = _converse.chatboxviews.get('controlbox');
cbview.el.querySelector('.change-status').click() cbview.el.querySelector('.change-status').click()
var modal = _converse.xmppstatusview.status_modal; const modal = _converse.xmppstatusview.status_modal;
test_utils.waitUntil(function () { await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
return u.isVisible(modal.el); const view = _converse.xmppstatusview;
}, 1000).then(function () { spyOn(_converse, 'emit');
var view = _converse.xmppstatusview;
spyOn(_converse, 'emit');
var msg = 'I am happy'; const msg = 'I am happy';
modal.el.querySelector('input[name="status_message"]').value = msg; modal.el.querySelector('input[name="status_message"]').value = msg;
modal.el.querySelector('[type="submit"]').click(); modal.el.querySelector('[type="submit"]').click();
expect(_converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg); expect(_converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg);
expect($(view.el).find('.xmpp-status span:first-child').hasClass('online')).toBe(true); expect($(view.el).find('.xmpp-status span:first-child').hasClass('online')).toBe(true);
expect(view.el.querySelector('.xmpp-status span:first-child').textContent.trim()).toBe(msg); expect(view.el.querySelector('.xmpp-status span:first-child').textContent.trim()).toBe(msg);
done(); done();
});
})); }));
}); });
}); });
...@@ -189,40 +180,37 @@ ...@@ -189,40 +180,37 @@
it("opens up an add modal when you click on it", it("opens up an add modal when you click on it",
mock.initConverseWithPromises( mock.initConverseWithPromises(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},
function (done, _converse) { async function (done, _converse) {
test_utils.createContacts(_converse, 'all').openControlBox(); test_utils.createContacts(_converse, 'all').openControlBox();
var panel = _converse.chatboxviews.get('controlbox').contactspanel; const panel = _converse.chatboxviews.get('controlbox').contactspanel;
var cbview = _converse.chatboxviews.get('controlbox'); const cbview = _converse.chatboxviews.get('controlbox');
cbview.el.querySelector('.add-contact').click() cbview.el.querySelector('.add-contact').click()
var modal = _converse.rosterview.add_contact_modal; const modal = _converse.rosterview.add_contact_modal;
return test_utils.waitUntil(function () { await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
return u.isVisible(modal.el); const sendIQ = _converse.connection.sendIQ;
}, 1000).then(function () { let sent_stanza, IQ_id;
var sendIQ = _converse.connection.sendIQ; spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
var sent_stanza, IQ_id; sent_stanza = iq;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) { IQ_id = sendIQ.bind(this)(iq, callback, errback);
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
expect(!_.isNull(modal.el.querySelector('form.add-xmpp-contact'))).toBeTruthy();
var input_jid = modal.el.querySelector('input[name="jid"]');
var input_name = modal.el.querySelector('input[name="name"]');
input_jid.value = 'someone@';
var evt = new Event('input');
input_jid.dispatchEvent(evt);
expect(modal.el.querySelector('.awesomplete li').textContent).toBe('someone@localhost');
input_jid.value = 'someone@localhost';
input_name.value = 'Someone';
modal.el.querySelector('button[type="submit"]').click();
expect(sent_stanza.toLocaleString()).toEqual(
`<iq id="${IQ_id}" type="set" xmlns="jabber:client">`+
`<query xmlns="jabber:iq:roster"><item jid="someone@localhost" name="Someone"/></query>`+
`</iq>`);
done();
}); });
expect(!_.isNull(modal.el.querySelector('form.add-xmpp-contact'))).toBeTruthy();
const input_jid = modal.el.querySelector('input[name="jid"]');
const input_name = modal.el.querySelector('input[name="name"]');
input_jid.value = 'someone@';
const evt = new Event('input');
input_jid.dispatchEvent(evt);
expect(modal.el.querySelector('.awesomplete li').textContent).toBe('someone@localhost');
input_jid.value = 'someone@localhost';
input_name.value = 'Someone';
modal.el.querySelector('button[type="submit"]').click();
expect(sent_stanza.toLocaleString()).toEqual(
`<iq id="${IQ_id}" type="set" xmlns="jabber:client">`+
`<query xmlns="jabber:iq:roster"><item jid="someone@localhost" name="Someone"/></query>`+
`</iq>`);
done();
})); }));
...@@ -232,7 +220,7 @@ ...@@ -232,7 +220,7 @@
{ 'xhr_user_search': true, { 'xhr_user_search': true,
'xhr_user_search_url': 'http://example.org/' 'xhr_user_search_url': 'http://example.org/'
}, },
function (done, _converse) { async function (done, _converse) {
var xhr = { var xhr = {
'open': _.noop, 'open': _.noop,
...@@ -255,46 +243,40 @@ ...@@ -255,46 +243,40 @@
var cbview = _converse.chatboxviews.get('controlbox'); var cbview = _converse.chatboxviews.get('controlbox');
cbview.el.querySelector('.add-contact').click() cbview.el.querySelector('.add-contact').click()
var modal = _converse.rosterview.add_contact_modal; var modal = _converse.rosterview.add_contact_modal;
return test_utils.waitUntil(function () { await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
return u.isVisible(modal.el); input_el = modal.el.querySelector('input[name="name"]');
}, 1000).then(function () { input_el.value = 'marty';
input_el = modal.el.querySelector('input[name="name"]'); var evt = new Event('input');
input_el.value = 'marty'; input_el.dispatchEvent(evt);
var evt = new Event('input'); await test_utils.waitUntil(() => modal.el.querySelector('.awesomplete li'), 1000);
input_el.dispatchEvent(evt); var sendIQ = _converse.connection.sendIQ;
return test_utils.waitUntil(function () { var sent_stanza, IQ_id;
return modal.el.querySelector('.awesomplete li'); spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
}, 1000); sent_stanza = iq;
}).then(function () { IQ_id = sendIQ.bind(this)(iq, callback, errback);
var sendIQ = _converse.connection.sendIQ;
var sent_stanza, IQ_id;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
expect(modal.el.querySelectorAll('.awesomplete li').length).toBe(1);
const suggestion = modal.el.querySelector('.awesomplete li');
expect(suggestion.textContent).toBe('Marty McFly');
// Can't trigger "mousedown" event so trigger the Awesomplete
// custom event which would have been triggered upon mousedown.
var evt = document.createEvent("HTMLEvents");
evt.initEvent('awesomplete-selectcomplete', true, true );
evt.text = {
'label': 'Marty McFly',
'value': 'marty@mcfly.net'
}
modal.el.dispatchEvent(evt);
expect(input_el.value).toBe('Marty McFly');
expect(modal.el.querySelector('input[name="jid"]').value).toBe('marty@mcfly.net');
modal.el.querySelector('button[type="submit"]').click();
expect(sent_stanza.toLocaleString()).toEqual(
`<iq id="${IQ_id}" type="set" xmlns="jabber:client">`+
`<query xmlns="jabber:iq:roster"><item jid="marty@mcfly.net" name="Marty McFly"/></query>`+
`</iq>`);
window.XMLHttpRequest = XMLHttpRequestBackup;
done();
}); });
expect(modal.el.querySelectorAll('.awesomplete li').length).toBe(1);
const suggestion = modal.el.querySelector('.awesomplete li');
expect(suggestion.textContent).toBe('Marty McFly');
// Can't trigger "mousedown" event so trigger the Awesomplete
// custom event which would have been triggered upon mousedown.
evt = document.createEvent("HTMLEvents");
evt.initEvent('awesomplete-selectcomplete', true, true );
evt.text = {
'label': 'Marty McFly',
'value': 'marty@mcfly.net'
}
modal.el.dispatchEvent(evt);
expect(input_el.value).toBe('Marty McFly');
expect(modal.el.querySelector('input[name="jid"]').value).toBe('marty@mcfly.net');
modal.el.querySelector('button[type="submit"]').click();
expect(sent_stanza.toLocaleString()).toEqual(
`<iq id="${IQ_id}" type="set" xmlns="jabber:client">`+
`<query xmlns="jabber:iq:roster"><item jid="marty@mcfly.net" name="Marty McFly"/></query>`+
`</iq>`);
window.XMLHttpRequest = XMLHttpRequestBackup;
done();
})); }));
}); });
})); }));
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