diff --git a/spec/bookmarks.js b/spec/bookmarks.js index 70fbc24c4..6b1990688 100644 --- a/spec/bookmarks.js +++ b/spec/bookmarks.js @@ -430,8 +430,13 @@ 'jid': 'another@conference.shakespeare.lit' }).c('nick').t('JC').up().up(); _converse.connection._dataRecv(test_utils.createRequest(stanza)); - expect($('#chatrooms dl.bookmarks dd').length).toBe(3); - done(); + + test_utils.waitUntil(function () { + return $('#chatrooms dl.bookmarks dd').length; + }, 300).then(function () { + expect($('#chatrooms dl.bookmarks dd').length).toBe(3); + done(); + }); })); it("remembers the toggle state of the bookmarks list", mock.initConverseWithPromises( @@ -472,15 +477,20 @@ 'nick': '' }); test_utils.openControlBox().openRoomsPanel(_converse); - expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1); - expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED); - $('#chatrooms .bookmarks-toggle').click(); - expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(0); - expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.CLOSED); - $('#chatrooms .bookmarks-toggle').click(); - expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1); - expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED); - done(); + + test_utils.waitUntil(function () { + return $('#chatrooms dl.bookmarks dd:visible').length; + }, 300).then(function () { + expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1); + expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED); + $('#chatrooms .bookmarks-toggle').click(); + expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(0); + expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.CLOSED); + $('#chatrooms .bookmarks-toggle').click(); + expect($('#chatrooms dl.bookmarks dd:visible').length).toBe(1); + expect(_converse.bookmarksview.list_model.get('toggle-state')).toBe(_converse.OPENED); + done(); + }); })); }); }); diff --git a/src/converse-bookmarks.js b/src/converse-bookmarks.js index cd2b36a91..d036c9491 100644 --- a/src/converse-bookmarks.js +++ b/src/converse-bookmarks.js @@ -229,8 +229,7 @@ }, fetchBookmarks () { - const deferred = new $.Deferred(); - const promise = deferred.promise(); + const deferred = utils.getWrappedPromise(); if (this.browserStorage.records.length > 0) { this.fetch({ 'success': _.bind(this.onCachedBookmarksFetched, this, deferred), @@ -245,7 +244,7 @@ } else { deferred.resolve(); } - return promise; + return deferred.promise; }, onCachedBookmarksFetched (deferred) { @@ -482,7 +481,7 @@ return; } _converse.bookmarks = new _converse.Bookmarks(); - _converse.bookmarks.fetchBookmarks().always(function () { + _converse.bookmarks.fetchBookmarks().then(function () { _converse.bookmarksview = new _converse.BookmarksView( {'model': _converse.bookmarks} ); diff --git a/src/utils.js b/src/utils.js index be012e3bf..98516a816 100755 --- a/src/utils.js +++ b/src/utils.js @@ -69,22 +69,22 @@ }; var isImage = function (url) { - var deferred = new $.Deferred(); - var img = new Image(); - var timer = window.setTimeout(function () { - deferred.reject(); - img = null; - }, 3000); - img.onerror = img.onabort = function () { - clearTimeout(timer); - deferred.reject(); - }; - img.onload = function () { - clearTimeout(timer); - deferred.resolve(img); - }; - img.src = url; - return deferred.promise(); + return new Promise((resolve, reject) => { + var img = new Image(); + var timer = window.setTimeout(function () { + reject(new Error("Could not determine whether it's an image")); + img = null; + }, 3000); + img.onerror = img.onabort = function () { + clearTimeout(timer); + reject(new Error("Could not determine whether it's an image")); + }; + img.onload = function () { + clearTimeout(timer); + resolve(img); + }; + img.src = url; + }); }; $.fn.hasScrollBar = function() {