From 1a9771c7172737f86bb7d168bdbdde51213130a9 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Sun, 12 Jul 2015 23:58:07 +0200 Subject: [PATCH] Test code which receives archived messages and calls callback updates #306 --- converse.js | 14 +++++++------- spec/mam.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/converse.js b/converse.js index c71bf90d6..77d9e760d 100644 --- a/converse.js +++ b/converse.js @@ -558,23 +558,23 @@ var messages = []; converse.connection.addHandler( function (message) { - var $msg = $(message), $fin; + var $msg = $(message), $fin, rsm, i; if (typeof callback == "function") { $fin = $msg.find('fin[xmlns="'+Strophe.NS.MAM+'"]'); if ($fin.length) { - callback( - messages, - new Strophe.RSM(_.extend({xml: $fin.find('set')[0]}, _.pick(options, MAM_ATTRIBUTES))) - ); + rsm = new Strophe.RSM({xml: $fin.find('set')[0]}); + _.extend(rsm, _.pick(options, ['max', 'after', 'before'])); + _.extend(rsm, _.pick(options, MAM_ATTRIBUTES)); + callback(messages, rsm); return false; // We've received all messages, decommission this handler - } else if (queryid == $msg.find('result[xmlns="'+Strophe.NS.MAM+'"]').attr('queryid')) { + } else if (queryid == $msg.find('result').attr('queryid')) { messages.push(message); } return true; } else { return false; // There's no callback, so no use in continuing this handler. } - }, null, 'message'); + }, Strophe.NS.MAM, 'message'); }; this.getVCard = function (jid, callback, errback) { diff --git a/spec/mam.js b/spec/mam.js index 7e1041399..846a6f2ba 100644 --- a/spec/mam.js +++ b/spec/mam.js @@ -12,6 +12,7 @@ var Strophe = converse_api.env.Strophe; var $iq = converse_api.env.$iq; var $pres = converse_api.env.$pres; + var $msg = converse_api.env.$msg; // See: https://xmpp.org/rfcs/rfc3921.html describe("Message Archive Management", $.proxy(function (mock, test_utils) { @@ -261,15 +262,15 @@ // and pass it in. However, in the callback method an RSM object is // returned which can be reused for easy paging. This test is // more for that usecase. + if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { + converse.features.create({'var': Strophe.NS.MAM}); + } var sent_stanza, IQ_id; var sendIQ = converse.connection.sendIQ; spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { sent_stanza = iq; IQ_id = sendIQ.bind(this)(iq, callback, errback); }); - if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { - converse.features.create({'var': Strophe.NS.MAM}); - } // Mock the browser's method for returning the timezone var getTimezoneOffset = Date.prototype.getTimezoneOffset; Date.prototype.getTimezoneOffset = function () { @@ -305,6 +306,55 @@ Date.prototype.getTimezoneOffset = getTimezoneOffset; }); + it("accepts a callback function, which it passes the messages and a Strophe.RSM object", function () { + if (!converse.features.findWhere({'var': Strophe.NS.MAM})) { + converse.features.create({'var': Strophe.NS.MAM}); + } + var sent_stanza, IQ_id; + var sendIQ = converse.connection.sendIQ; + spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { + sent_stanza = iq; + IQ_id = sendIQ.bind(this)(iq, callback, errback); + }); + spyOn(converse, 'onMAMQueryResult').andCallThrough(); + var callback = jasmine.createSpy('callback'); + + converse_api.archive.query({'with': 'romeo@capulet.lit', 'max':'10'}, callback); + var queryid = $(sent_stanza.toString()).find('query').attr('queryid'); + + // Send the result stanza, so that the callback is called. + var stanza = $iq({'type': 'result', 'id': IQ_id}); + converse.connection._dataRecv(test_utils.createRequest(stanza)); + expect(converse.onMAMQueryResult).toHaveBeenCalled(); + + /* Send a message to indicate the end of the result set. + * + * + * + * + * 23452-4534-1 + * 390-2342-22 + * 16 + * + * + * + */ + stanza = $msg().c('fin', {'xmlns': 'urn:xmpp:mam:0', 'complete': 'true'}) + .c('set', {'xmlns': 'http://jabber.org/protocol/rsm'}) + .c('first', {'index': '0'}).t('23452-4534-1').up() + .c('last').t('390-2342-22').up() + .c('count').t('16'); + converse.connection._dataRecv(test_utils.createRequest(stanza)); + + expect(callback).toHaveBeenCalled(); + var args = callback.argsForCall[0]; + expect(args[1]['with']).toBe('romeo@capulet.lit'); + expect(args[1].max).toBe('10'); + expect(args[1].count).toBe('16'); + expect(args[1].first).toBe('23452-4534-1'); + expect(args[1].last).toBe('390-2342-22'); + }); + }, converse, mock, test_utils)); describe("The default preference", $.proxy(function (mock, test_utils) {