Test code which receives archived messages and calls callback

updates #306
This commit is contained in:
JC Brand 2015-07-12 23:58:07 +02:00
parent 27afd32aeb
commit 1a9771c717
2 changed files with 60 additions and 10 deletions

View File

@ -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) {

View File

@ -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 <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));
describe("The default preference", $.proxy(function (mock, test_utils) {