You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Fix tests; add logs and use promises when flushing.
This commit is contained in:
@@ -1,12 +1,14 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var sdk = require("../..");
|
var sdk = require("../..");
|
||||||
var HttpBackend = require("../mock-request");
|
var HttpBackend = require("../mock-request");
|
||||||
|
var utils = require("../test-utils");
|
||||||
|
|
||||||
describe("MatrixClient", function() {
|
describe("MatrixClient", function() {
|
||||||
var baseUrl = "http://localhost.or.something";
|
var baseUrl = "http://localhost.or.something";
|
||||||
var client, httpBackend;
|
var client, httpBackend;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
|
utils.beforeEach(this);
|
||||||
httpBackend = new HttpBackend();
|
httpBackend = new HttpBackend();
|
||||||
sdk.request(httpBackend.requestFn);
|
sdk.request(httpBackend.requestFn);
|
||||||
client = sdk.createClient(baseUrl);
|
client = sdk.createClient(baseUrl);
|
||||||
@@ -28,17 +30,19 @@ describe("MatrixClient", function() {
|
|||||||
chunk: []
|
chunk: []
|
||||||
};
|
};
|
||||||
|
|
||||||
it("should start with /initialSync then move onto /events.", function() {
|
it("should start with /initialSync then move onto /events.", function(done) {
|
||||||
httpBackend.when("GET", "/initialSync").respond(200, initialSync);
|
httpBackend.when("GET", "/initialSync").respond(200, initialSync);
|
||||||
httpBackend.when("GET", "/events").respond(200, eventData);
|
httpBackend.when("GET", "/events").respond(200, eventData);
|
||||||
|
|
||||||
client.startClient(function(err, data, isLive) {});
|
client.startClient(function(err, data, isLive) {});
|
||||||
|
|
||||||
httpBackend.flush();
|
httpBackend.flush().done(function() {
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should pass the 'end' token from /initialSync to the from= param " +
|
it("should pass the 'end' token from /initialSync to the from= param " +
|
||||||
" of /events", function() {
|
" of /events", function(done) {
|
||||||
httpBackend.when("GET", "/initialSync").respond(200, initialSync);
|
httpBackend.when("GET", "/initialSync").respond(200, initialSync);
|
||||||
httpBackend.when("GET", "/events").check(function(req) {
|
httpBackend.when("GET", "/events").check(function(req) {
|
||||||
expect(req.queryParams.from).toEqual(initialSync.end);
|
expect(req.queryParams.from).toEqual(initialSync.end);
|
||||||
@@ -46,7 +50,9 @@ describe("MatrixClient", function() {
|
|||||||
|
|
||||||
client.startClient(function(err, data, isLive) {});
|
client.startClient(function(err, data, isLive) {});
|
||||||
|
|
||||||
httpBackend.flush();
|
httpBackend.flush().done(function() {
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
var q = require("q");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a mock HTTP backend, heavily inspired by Angular.js.
|
* Construct a mock HTTP backend, heavily inspired by Angular.js.
|
||||||
@@ -18,37 +19,77 @@ function HttpBackend() {
|
|||||||
HttpBackend.prototype = {
|
HttpBackend.prototype = {
|
||||||
/**
|
/**
|
||||||
* Respond to all of the requests (flush the queue).
|
* Respond to all of the requests (flush the queue).
|
||||||
|
* @return Promise resolved when there is nothing left to flush.
|
||||||
*/
|
*/
|
||||||
flush: function() {
|
flush: function() {
|
||||||
// if there's more real requests and more expected requests, flush 'em.
|
var defer = q.defer();
|
||||||
while (this.requests.length > 0 && this.expectedRequests.length > 0) {
|
var self = this;
|
||||||
var req = this.requests.shift();
|
console.log("HTTP backend flushing...");
|
||||||
var i;
|
var tryFlush = function() {
|
||||||
|
// if there's more real requests and more expected requests, flush 'em.
|
||||||
|
console.log(
|
||||||
|
" trying to flush queue => reqs=%s expected=%s",
|
||||||
|
self.requests.length, self.expectedRequests.length
|
||||||
|
);
|
||||||
|
if (self._takeFromQueue()) {
|
||||||
|
// try again on the next tick.
|
||||||
|
console.log(" flushed. Trying for more.");
|
||||||
|
setTimeout(tryFlush, 0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
console.log(" no more flushes.");
|
||||||
|
defer.resolve();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var matchingReq = null;
|
setTimeout(tryFlush, 0);
|
||||||
for (i = 0; i < this.expectedRequests.length; i++) {
|
|
||||||
var expectedReq = this.expectedRequests[i];
|
return defer.promise;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to resolve requests/expected requests.
|
||||||
|
* @return true if something was resolved.
|
||||||
|
*/
|
||||||
|
_takeFromQueue: function() {
|
||||||
|
var req = null;
|
||||||
|
var i, j;
|
||||||
|
var matchingReq, expectedReq, testResponse = null;
|
||||||
|
for (i =0; i < this.requests.length; i++) {
|
||||||
|
req = this.requests[i];
|
||||||
|
for (j = 0; j < this.expectedRequests.length; j++) {
|
||||||
|
expectedReq = this.expectedRequests[j];
|
||||||
if (expectedReq.method === req.method &&
|
if (expectedReq.method === req.method &&
|
||||||
req.path.indexOf(expectedReq.path) !== -1) {
|
req.path.indexOf(expectedReq.path) !== -1) {
|
||||||
if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
|
if (!expectedReq.data || (JSON.stringify(expectedReq.data) ===
|
||||||
JSON.stringify(req.data))) {
|
JSON.stringify(req.data))) {
|
||||||
matchingReq = expectedReq;
|
matchingReq = expectedReq;
|
||||||
this.expectedRequests.splice(i, 1);
|
this.expectedRequests.splice(j, 1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchingReq) {
|
if (matchingReq) {
|
||||||
for (i = 0; i < matchingReq.checks.length; i++) {
|
// remove from request queue
|
||||||
matchingReq.checks[i](req);
|
this.requests.splice(i, 1);
|
||||||
|
i--;
|
||||||
|
|
||||||
|
for (j = 0; j < matchingReq.checks.length; j++) {
|
||||||
|
matchingReq.checks[j](req);
|
||||||
}
|
}
|
||||||
var testResponse = matchingReq.response;
|
testResponse = matchingReq.response;
|
||||||
|
console.log(" responding to %s", matchingReq.path);
|
||||||
|
|
||||||
req.callback(
|
req.callback(
|
||||||
testResponse.err, testResponse.response, testResponse.body
|
testResponse.err, testResponse.response, testResponse.body
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
if (testResponse) { // flushed something
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
7
spec/test-utils.js
Normal file
7
spec/test-utils.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
module.exports.beforeEach = function(testCase) {
|
||||||
|
var desc = testCase.suite.description + " : " + testCase.description;
|
||||||
|
console.log(desc);
|
||||||
|
console.log(new Array(1 + desc.length).join("="));
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user