From 7ea09ebe4aaab36eaddc9a64bce2d7b058d73bba Mon Sep 17 00:00:00 2001 From: nchamo Date: Fri, 17 Apr 2020 19:32:02 -0300 Subject: [PATCH] Add fix and test Signed-off-by: Nicolas Chamo --- spec/unit/filter-component.spec.js | 39 ++++++++++++++++++++++++++++++ src/filter-component.js | 5 ++-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 spec/unit/filter-component.spec.js diff --git a/spec/unit/filter-component.spec.js b/spec/unit/filter-component.spec.js new file mode 100644 index 000000000..156750ae6 --- /dev/null +++ b/spec/unit/filter-component.spec.js @@ -0,0 +1,39 @@ +import {FilterComponent} from "../../src/filter-component" +import {mkEvent} from '../test-utils' + +describe("Filter Component", function() { + + describe("types", function() { + it("should filter out events with other types", function() { + const filter = new FilterComponent({ types: [ 'm.room.message' ] }) + + const event = mkEvent({ + type: 'm.room.member', + content: { }, + room: 'roomId', + event: true, + }) + + const checkResult = filter.check(event) + + expect(checkResult).toBe(false); + }); + + it("should validate events with the same type", function() { + const filter = new FilterComponent({ types: [ 'm.room.message' ] }) + + const event = mkEvent({ + type: 'm.room.message', + content: { }, + room: 'roomId', + event: true, + }) + + const checkResult = filter.check(event) + + expect(checkResult).toBe(true); + }); + + }); + +}); diff --git a/src/filter-component.js b/src/filter-component.js index 7e8fd5244..4bf2a16c6 100644 --- a/src/filter-component.js +++ b/src/filter-component.js @@ -108,8 +108,9 @@ FilterComponent.prototype._checkFields = } const allowed_values = self[name]; - if (allowed_values) { - if (!allowed_values.map(match_func)) { + if (allowed_values && allowed_values.length > 0) { + const anyMatch = allowed_values.map(match_func).reduce((a, b) => a || b); + if (!anyMatch) { return false; } }