From 3a2bf766a0fbd664492066c14bf79d669e42a9df Mon Sep 17 00:00:00 2001 From: JC Brand Date: Fri, 12 Jul 2019 10:37:10 +0200 Subject: [PATCH] Add new test utility `waitUntilBookmarksReturned` --- spec/bookmarks.js | 8 ++------ src/headless/converse-bookmarks.js | 3 +-- tests/utils.js | 31 +++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/spec/bookmarks.js b/spec/bookmarks.js index 8696aab9f..32e54631f 100644 --- a/spec/bookmarks.js +++ b/spec/bookmarks.js @@ -322,11 +322,7 @@ null, ['rosterGroupsFetched'], {}, async function (done, _converse) { - await test_utils.waitUntilDiscoConfirmed( - _converse, _converse.bare_jid, - [{'category': 'pubsub', 'type': 'pep'}], - ['http://jabber.org/protocol/pubsub#publish-options'] - ); + await test_utils.waitUntilBookmarksReturned(_converse); spyOn(_converse.api.rooms, 'create').and.callThrough(); const jid = 'theplay@conference.shakespeare.lit'; const model = _converse.bookmarks.create({ @@ -450,7 +446,7 @@ ''+ ''+ ''); - + /* * Server returns all items * ------------------------ diff --git a/src/headless/converse-bookmarks.js b/src/headless/converse-bookmarks.js index 6fe04b9c9..4c430db51 100644 --- a/src/headless/converse-bookmarks.js +++ b/src/headless/converse-bookmarks.js @@ -262,8 +262,7 @@ converse.plugins.add('converse-bookmarks', { if (!_converse.allow_bookmarks) { return; } - const supported = await _converse.checkBookmarksSupport(); - if (supported) { + if (await _converse.checkBookmarksSupport()) { _converse.bookmarks = new _converse.Bookmarks(); await _converse.bookmarks.fetchBookmarks(); /** diff --git a/tests/utils.js b/tests/utils.js index c1576d55c..cd99faba8 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -78,6 +78,35 @@ return this; }; + utils.waitUntilBookmarksReturned = async function (_converse, bookmarks=[]) { + await utils.waitUntilDiscoConfirmed( + _converse, _converse.bare_jid, + [{'category': 'pubsub', 'type': 'pep'}], + ['http://jabber.org/protocol/pubsub#publish-options'] + ); + const IQ_stanzas = _converse.connection.IQ_stanzas; + const sent_stanza = await u.waitUntil( + () => IQ_stanzas.filter(s => sizzle('items[node="storage:bookmarks"]', s).length).pop() + ); + const stanza = $iq({ + 'to': _converse.connection.jid, + 'type':'result', + 'id':sent_stanza.getAttribute('id') + }).c('pubsub', {'xmlns': Strophe.NS.PUBSUB}) + .c('items', {'node': 'storage:bookmarks'}) + .c('item', {'id': 'current'}) + .c('storage', {'xmlns': 'storage:bookmarks'}); + bookmarks.forEach(bookmark => { + stanza.c('conference', { + 'name': bookmark.name, + 'autojoin': bookmark.autojoin, + 'jid': bookmark.jid + }).c('nick').t(bookmark.nick).up().up() + }); + _converse.connection._dataRecv(utils.createRequest(stanza)); + await _converse.api.waitUntil('bookmarksInitialized'); + }; + utils.openChatBoxes = function (converse, amount) { var i = 0, jid, views = []; for (i; i view.on('messageInserted', resolve)); + const promise = new Promise(resolve => view.once('messageInserted', resolve)); view.el.querySelector('.chat-textarea').value = message; view.onKeyDown({ target: view.el.querySelector('textarea.chat-textarea'),