Commit 1a9771c7 authored by JC Brand's avatar JC Brand

Test code which receives archived messages and calls callback

updates #306
parent 27afd32a
...@@ -558,23 +558,23 @@ ...@@ -558,23 +558,23 @@
var messages = []; var messages = [];
converse.connection.addHandler( converse.connection.addHandler(
function (message) { function (message) {
var $msg = $(message), $fin; var $msg = $(message), $fin, rsm, i;
if (typeof callback == "function") { if (typeof callback == "function") {
$fin = $msg.find('fin[xmlns="'+Strophe.NS.MAM+'"]'); $fin = $msg.find('fin[xmlns="'+Strophe.NS.MAM+'"]');
if ($fin.length) { if ($fin.length) {
callback( rsm = new Strophe.RSM({xml: $fin.find('set')[0]});
messages, _.extend(rsm, _.pick(options, ['max', 'after', 'before']));
new Strophe.RSM(_.extend({xml: $fin.find('set')[0]}, _.pick(options, MAM_ATTRIBUTES))) _.extend(rsm, _.pick(options, MAM_ATTRIBUTES));
); callback(messages, rsm);
return false; // We've received all messages, decommission this handler 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); messages.push(message);
} }
return true; return true;
} else { } else {
return false; // There's no callback, so no use in continuing this handler. 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) { this.getVCard = function (jid, callback, errback) {
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
var Strophe = converse_api.env.Strophe; var Strophe = converse_api.env.Strophe;
var $iq = converse_api.env.$iq; var $iq = converse_api.env.$iq;
var $pres = converse_api.env.$pres; var $pres = converse_api.env.$pres;
var $msg = converse_api.env.$msg;
// See: https://xmpp.org/rfcs/rfc3921.html // See: https://xmpp.org/rfcs/rfc3921.html
describe("Message Archive Management", $.proxy(function (mock, test_utils) { describe("Message Archive Management", $.proxy(function (mock, test_utils) {
...@@ -261,15 +262,15 @@ ...@@ -261,15 +262,15 @@
// and pass it in. However, in the callback method an RSM object is // and pass it in. However, in the callback method an RSM object is
// returned which can be reused for easy paging. This test is // returned which can be reused for easy paging. This test is
// more for that usecase. // 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 sent_stanza, IQ_id;
var sendIQ = converse.connection.sendIQ; var sendIQ = converse.connection.sendIQ;
spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) { spyOn(converse.connection, 'sendIQ').andCallFake(function (iq, callback, errback) {
sent_stanza = iq; sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback); 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 // Mock the browser's method for returning the timezone
var getTimezoneOffset = Date.prototype.getTimezoneOffset; var getTimezoneOffset = Date.prototype.getTimezoneOffset;
Date.prototype.getTimezoneOffset = function () { Date.prototype.getTimezoneOffset = function () {
...@@ -305,6 +306,55 @@ ...@@ -305,6 +306,55 @@
Date.prototype.getTimezoneOffset = getTimezoneOffset; 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 <fin> message to indicate the end of the result set.
*
* <message>
* <fin xmlns='urn:xmpp:mam:0' complete='true'>
* <set xmlns='http://jabber.org/protocol/rsm'>
* <first index='0'>23452-4534-1</first>
* <last>390-2342-22</last>
* <count>16</count>
* </set>
* </fin>
* </message>
*/
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)); }, converse, mock, test_utils));
describe("The default preference", $.proxy(function (mock, test_utils) { describe("The default preference", $.proxy(function (mock, test_utils) {
......
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