1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Add filter UTs and fix bugs

This commit is contained in:
Kegan Dougal
2015-12-08 15:39:55 +00:00
parent 1987726a95
commit 86a162c818
3 changed files with 63 additions and 12 deletions

View File

@@ -17,7 +17,7 @@ function setProp(obj, keyNesting, val) {
}
currentObj = currentObj[nestedKeys[i]];
}
currentObj[nestedKeys.length - 1] = val;
currentObj[nestedKeys[nestedKeys.length - 1]] = val;
}
/**
@@ -25,29 +25,29 @@ function setProp(obj, keyNesting, val) {
* @constructor
* @param {string} userId The user ID for this filter.
* @param {string=} filterId The filter ID if known.
* @prop {string} userId The user ID of the filter
* @prop {?string} filterId The filter ID
*/
function Filter(userId, filterId) {
this.userId = userId;
this.filterId = filterId;
this.body = {
event_format: "client"
};
this.definition = {};
}
/**
* Get the JSON body of the filter.
* @return {Object} The JSON body
* @return {Object} The filter definition
*/
Filter.prototype.getBody = function() {
return this.body;
Filter.prototype.getDefinition = function() {
return this.definition;
};
/**
* Set the JSON body of the filter
* @param {Object} body
* @param {Object} body The filter definition
*/
Filter.prototype.setBody = function(body) {
this.body = body;
Filter.prototype.setDefinition = function(definition) {
this.definition = definition;
};
/**
@@ -55,7 +55,7 @@ Filter.prototype.setBody = function(body) {
* @param {Number} limit The max number of events to return for each room.
*/
Filter.prototype.setTimelineLimit = function(limit) {
setProp(this.body, "room.timeline.limit", limit);
setProp(this.definition, "room.timeline.limit", limit);
};
/**
@@ -68,7 +68,7 @@ Filter.prototype.setTimelineLimit = function(limit) {
*/
Filter.fromJson = function(userId, filterId, jsonObj) {
var filter = new Filter(userId, filterId);
filter.setBody(jsonObj);
filter.setDefinition(jsonObj);
return filter;
};

View File

@@ -34,6 +34,7 @@ module.exports.CRYPTO_ENABLED = require("./client").CRYPTO_ENABLED;
module.exports.ContentRepo = require("./content-repo");
/** The {@link module:filter~Filter|Filter} class. */
module.exports.Filter = require("./filter");
/**
* Create a new Matrix Call.
* @function

50
spec/unit/filter.spec.js Normal file
View File

@@ -0,0 +1,50 @@
"use strict";
var sdk = require("../..");
var Filter = sdk.Filter;
var utils = require("../test-utils");
describe("Filter", function() {
var filterId = "f1lt3ring15g00d4ursoul";
var userId = "@sir_arthur_david:humming.tiger";
var filter;
beforeEach(function() {
utils.beforeEach(this);
filter = new Filter(userId);
});
describe("fromJson", function() {
it("create a new Filter from the provided values", function() {
var definition = {
event_fields: ["type", "content"]
};
var f = Filter.fromJson(userId, filterId, definition);
expect(f.getDefinition()).toEqual(definition);
expect(f.userId).toEqual(userId);
expect(f.filterId).toEqual(filterId);
});
});
describe("setTimelineLimit", function() {
it("should set room.timeline.limit of the filter definition", function() {
filter.setTimelineLimit(10);
expect(filter.getDefinition()).toEqual({
room: {
timeline: {
limit: 10
}
}
});
});
});
describe("setDefinition/getDefinition", function() {
it("should set and get the filter body", function() {
var definition = {
event_format: "client"
};
filter.setDefinition(definition);
expect(filter.getDefinition()).toEqual(definition);
});
});
});