From f49234a77279229173873c73c8927950f1a2bc37 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 25 Jun 2015 17:29:25 +0100 Subject: [PATCH] Add pagination tests --- lib/client.js | 1 - .../integ/matrix-client-room-timeline.spec.js | 83 +++++++++++++++++-- spec/mock-request.js | 10 ++- 3 files changed, 81 insertions(+), 13 deletions(-) diff --git a/lib/client.js b/lib/client.js index 28ddbb16c..a3503f941 100644 --- a/lib/client.js +++ b/lib/client.js @@ -825,7 +825,6 @@ MatrixClient.prototype.scrollback = function(room, limit, callback) { }; var defer = q.defer(); this._http.authedRequest(callback, "GET", path, params).done(function(res) { - // res.chunk end start room.addEventsToTimeline( utils.map(res.chunk, _PojoToMatrixEventMapper), true ); diff --git a/spec/integ/matrix-client-room-timeline.spec.js b/spec/integ/matrix-client-room-timeline.spec.js index 9de7d1d07..78325bd84 100644 --- a/spec/integ/matrix-client-room-timeline.spec.js +++ b/spec/integ/matrix-client-room-timeline.spec.js @@ -165,16 +165,17 @@ describe("MatrixClient room timelines", function() { beforeEach(function() { sbEvents = []; - httpBackend.when("GET", "/messages").respond(200, { - chunk: sbEvents, - start: "pagin_start", - end: "pagin_end" + httpBackend.when("GET", "/messages").respond(200, function() { + return { + chunk: sbEvents, + start: "pagin_start", + end: "pagin_end" + }; }); }); it("should set Room.oldState.paginationToken to null at the start" + " of the timeline.", function(done) { - client.on("syncComplete", function() { var room = client.getRoom(roomId); expect(room.timeline.length).toEqual(1); @@ -191,14 +192,78 @@ describe("MatrixClient room timelines", function() { client.startClient(); httpBackend.flush("/initialSync", 1); }); -/* - it("should set the right event.sender values", function() { + it("should set the right event.sender values", function(done) { + // make an m.room.member event with prev_content + var oldMshipEvent = utils.mkMembership({ + mship: "join", user: userId, room: roomId, name: "Alice", + url: "mxc://some/url" + }); + oldMshipEvent.prev_content = { + displayname: "Old Alice", + avatar_url: null, + membership: "join" + }; + + // set the list of events to return on scrollback + sbEvents = [ + utils.mkMessage({ + user: userId, room: roomId, msg: "I'm alice" + }), + oldMshipEvent, + utils.mkMessage({ + user: userId, room: roomId, msg: "I'm old alice" + }) + ]; + + client.on("syncComplete", function() { + var room = client.getRoom(roomId); + expect(room.timeline.length).toEqual(1); + + client.scrollback(room).done(function() { + expect(room.timeline.length).toEqual(4); + var oldMsg = room.timeline[0]; + expect(oldMsg.sender.name).toEqual("Old Alice"); + var newMsg = room.timeline[2]; + expect(newMsg.sender.name).toEqual("Alice"); + done(); + }); + + httpBackend.flush("/messages", 1); + httpBackend.flush("/events", 1); + }); + client.startClient(); + httpBackend.flush("/initialSync", 1); }); - it("should add it them to the right place in the timeline", function() { + it("should add it them to the right place in the timeline", function(done) { + // set the list of events to return on scrollback + sbEvents = [ + utils.mkMessage({ + user: userId, room: roomId, msg: "I am new" + }), + utils.mkMessage({ + user: userId, room: roomId, msg: "I am old" + }) + ]; - }); */ + client.on("syncComplete", function() { + var room = client.getRoom(roomId); + expect(room.timeline.length).toEqual(1); + + client.scrollback(room).done(function() { + expect(room.timeline.length).toEqual(3); + expect(room.timeline[0].event).toEqual(sbEvents[1]); + expect(room.timeline[1].event).toEqual(sbEvents[0]); + done(); + }); + + httpBackend.flush("/messages", 1); + httpBackend.flush("/events", 1); + }); + client.startClient(); + httpBackend.flush("/initialSync", 1); + }); }); describe("new events", function() { diff --git a/spec/mock-request.js b/spec/mock-request.js index 987c0484e..1ca36c9f1 100644 --- a/spec/mock-request.js +++ b/spec/mock-request.js @@ -94,9 +94,12 @@ HttpBackend.prototype = { } testResponse = matchingReq.response; console.log(" responding to %s", matchingReq.path); - + var body = testResponse.body; + if (Object.prototype.toString.call(body) == "[object Function]") { + body = body(); + } req.callback( - testResponse.err, testResponse.response, testResponse.body + testResponse.err, testResponse.response, body ); matchingReq = null; } @@ -165,7 +168,8 @@ Request.prototype = { /** * Respond with the given data when this request is satisfied. * @param {Number} code The HTTP status code. - * @param {Object} data The HTTP JSON body. + * @param {Object|Function} data The HTTP JSON body. If this is a function, + * it will be invoked when the JSON body is required (which should be returned). */ respond: function(code, data) { this.response = {