1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-18 05:42:00 +03:00

Add filter integration tests; more bug fixes.

This commit is contained in:
Kegan Dougal
2015-12-08 16:08:04 +00:00
parent 86a162c818
commit c65f32f6a6
3 changed files with 101 additions and 4 deletions

View File

@@ -2072,11 +2072,10 @@ MatrixClient.prototype.search = function(opts, callback) {
/** /**
* Create a new filter. * Create a new filter.
* @param {Object} content The HTTP body for the request * @param {Object} content The HTTP body for the request
* @param {module:client.callback} callback Optional.
* @return {Filter} Resolves to a Filter object. * @return {Filter} Resolves to a Filter object.
* @return {module:http-api.MatrixError} Rejects: with an error response. * @return {module:http-api.MatrixError} Rejects: with an error response.
*/ */
MatrixClient.prototype.createFilter = function(content, callback) { MatrixClient.prototype.createFilter = function(content) {
var self = this; var self = this;
var path = utils.encodeUri("/user/$userId/filter", { var path = utils.encodeUri("/user/$userId/filter", {
$userId: this.credentials.userId $userId: this.credentials.userId
@@ -2106,7 +2105,7 @@ MatrixClient.prototype.getFilter = function(userId, filterId, allowCached) {
if (allowCached) { if (allowCached) {
var filter = this.store.getFilter(userId, filterId); var filter = this.store.getFilter(userId, filterId);
if (filter) { if (filter) {
return filter; return q(filter);
} }
} }

View File

@@ -17,6 +17,11 @@ module.exports.MatrixInMemoryStore = function MatrixInMemoryStore() {
// userId: User // userId: User
}; };
this.syncToken = null; this.syncToken = null;
this.filters = {
// userId: {
// filterId: Filter
// }
};
}; };
module.exports.MatrixInMemoryStore.prototype = { module.exports.MatrixInMemoryStore.prototype = {
@@ -116,6 +121,11 @@ module.exports.MatrixInMemoryStore.prototype = {
* @param {Filter} filter * @param {Filter} filter
*/ */
storeFilter: function(filter) { storeFilter: function(filter) {
if (!filter) { return; }
if (!this.filters[filter.userId]) {
this.filters[filter.userId] = {};
}
this.filters[filter.userId][filter.filterId] = filter;
}, },
/** /**
@@ -125,7 +135,10 @@ module.exports.MatrixInMemoryStore.prototype = {
* @return {?Filter} A filter or null. * @return {?Filter} A filter or null.
*/ */
getFilter: function(userId, filterId) { getFilter: function(userId, filterId) {
return null; if (!this.filters[userId] || !this.filters[userId][filterId]) {
return null;
}
return this.filters[userId][filterId];
} }
// TODO // TODO

View File

@@ -4,6 +4,7 @@ var HttpBackend = require("../mock-request");
var publicGlobals = require("../../lib/matrix"); var publicGlobals = require("../../lib/matrix");
var Room = publicGlobals.Room; var Room = publicGlobals.Room;
var MatrixInMemoryStore = publicGlobals.MatrixInMemoryStore; var MatrixInMemoryStore = publicGlobals.MatrixInMemoryStore;
var Filter = publicGlobals.Filter;
var utils = require("../test-utils"); var utils = require("../test-utils");
describe("MatrixClient", function() { describe("MatrixClient", function() {
@@ -44,6 +45,90 @@ describe("MatrixClient", function() {
}); });
}); });
describe("getFilter", function() {
var filterId = "f1lt3r1d";
it("should return a filter from the store if allowCached", function(done) {
var filter = Filter.fromJson(userId, filterId, {
event_format: "client"
});
store.storeFilter(filter);
client.getFilter(userId, filterId, true).done(function(gotFilter) {
expect(gotFilter).toEqual(filter);
done();
});
httpBackend.verifyNoOutstandingRequests();
});
it("should do an HTTP request if !allowCached even if one exists", function(done) {
var httpFilterDefinition = {
event_format: "federation"
};
httpBackend.when(
"GET", "/user/" + encodeURIComponent(userId) + "/filter/" + filterId
).respond(200, httpFilterDefinition);
var storeFilter = Filter.fromJson(userId, filterId, {
event_format: "client"
});
store.storeFilter(storeFilter);
client.getFilter(userId, filterId, false).done(function(gotFilter) {
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
done();
});
httpBackend.flush();
});
it("should do an HTTP request if nothing is in the cache and then store it",
function(done) {
var httpFilterDefinition = {
event_format: "federation"
};
expect(store.getFilter(userId, filterId)).toBeNull();
httpBackend.when(
"GET", "/user/" + encodeURIComponent(userId) + "/filter/" + filterId
).respond(200, httpFilterDefinition);
client.getFilter(userId, filterId, true).done(function(gotFilter) {
expect(gotFilter.getDefinition()).toEqual(httpFilterDefinition);
expect(store.getFilter(userId, filterId)).toBeDefined();
done();
});
httpBackend.flush();
});
});
describe("createFilter", function() {
var filterId = "f1llllllerid";
it("should do an HTTP request and then store the filter", function(done) {
expect(store.getFilter(userId, filterId)).toBeNull();
var filterDefinition = {
event_format: "client"
};
httpBackend.when(
"POST", "/user/" + encodeURIComponent(userId) + "/filter"
).check(function(req) {
expect(req.data).toEqual(filterDefinition);
}).respond(200, {
filter_id: filterId
});
client.createFilter(filterDefinition).done(function(gotFilter) {
expect(gotFilter.getDefinition()).toEqual(filterDefinition);
expect(store.getFilter(userId, filterId)).toEqual(gotFilter);
done();
});
httpBackend.flush();
});
});
describe("searching", function() { describe("searching", function() {
var response = { var response = {