Browse Source

bookmarks: enable registration of spy on the mock connection during init

We need this because when the plugin listens to a promise, we can't trigger it
again later (after adding our spy), we instead need to add the spy beforehand.
JC Brand 8 years ago
parent
commit
07dbf14a7d
2 changed files with 45 additions and 28 deletions
  1. 19 16
      spec/bookmarks.js
  2. 26 12
      tests/mock.js

+ 19 - 16
spec/bookmarks.js

@@ -292,15 +292,8 @@
              */
         }));
 
-        it("can be retrieved from the XMPP server", mock.initConverse(function (_converse) {
-            var sent_stanza, IQ_id,
-                sendIQ = _converse.connection.sendIQ;
-            spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
-                sent_stanza = iq;
-                IQ_id = sendIQ.bind(this)(iq, callback, errback);
-            });
-            _converse.emit('chatBoxesFetched');
-
+        it("can be retrieved from the XMPP server",
+                mock.initConverseWithConnectionSpies(['send'], function (_converse) {
             /* Client requests all items
              * -------------------------
              *
@@ -310,13 +303,23 @@
              *  </pubsub>
              *  </iq>
              */
-            expect(sent_stanza.toLocaleString()).toBe(
-                "<iq from='dummy@localhost/resource' type='get' xmlns='jabber:client' id='"+IQ_id+"'>"+
-                "<pubsub xmlns='http://jabber.org/protocol/pubsub'>"+
-                    "<items node='storage:bookmarks'/>"+
-                "</pubsub>"+
-                "</iq>"
-            );
+            var IQ_id;
+            expect(_.filter(_converse.connection.send.calls.all(), function (call) {
+                var stanza = call.args[0]
+                if (!(stanza instanceof Element) || stanza.nodeName !== 'iq') {
+                    return;
+                }
+                if (stanza.outerHTML ===
+                    '<iq from="dummy@localhost/resource" type="get" '+
+                         'xmlns="jabber:client" id="'+stanza.getAttribute('id')+'">'+
+                    '<pubsub xmlns="http://jabber.org/protocol/pubsub">'+
+                        '<items node="storage:bookmarks"/>'+
+                    '</pubsub>'+
+                    '</iq>') {
+                    IQ_id = stanza.getAttribute('id');
+                    return true;
+                }
+            }).length).toBe(1);
 
             /*
              * Server returns all items

+ 26 - 12
tests/mock.js

@@ -76,24 +76,39 @@
         };
     }();
 
-    function initConverse (settings) {
+    function initConverse (settings, spies) {
         window.localStorage.clear();
         window.sessionStorage.clear();
+
+        var connection = mock.mock_connection();
+        if (!_.isUndefined(spies)) {
+            _.forEach(spies, function (method) {
+                spyOn(connection, method);
+            });
+        }
+
         var converse = converse_api.initialize(_.extend({
-            i18n: 'en',
-            auto_subscribe: false,
-            bosh_service_url: 'localhost',
-            connection: mock.mock_connection(),
-            animate: false,
-            no_trimming: true,
-            auto_login: true,
-            jid: 'dummy@localhost',
-            password: 'secret',
-            debug: false
+            'i18n': 'en',
+            'auto_subscribe': false,
+            'bosh_service_url': 'localhost',
+            'connection': connection,
+            'animate': false,
+            'no_trimming': true,
+            'auto_login': true,
+            'jid': 'dummy@localhost',
+            'password': 'secret',
+            'debug': false
         }, settings || {}));
         converse.ChatBoxViews.prototype.trimChat = function () {};
         return converse;
     }
+
+    mock.initConverseWithConnectionSpies = function (spies, func, settings) {
+        return function () {
+            return func(initConverse(settings, spies));
+        };
+    };
+
     mock.initConverseWithAsync = function (func, settings) {
         return function (done) {
             return func(done, initConverse(settings));
@@ -101,7 +116,6 @@
     };
     mock.initConverse = function (func, settings) {
         return function () {
-            initConverse(settings);
             return func(initConverse(settings));
         };
     };