Index: test/filterStorage.js |
=================================================================== |
--- a/test/filterStorage.js |
+++ b/test/filterStorage.js |
@@ -34,16 +34,33 @@ |
{FilterNotifier} = sandboxedRequire("../lib/filterNotifier"), |
{FilterStorage} = sandboxedRequire("../lib/filterStorage"), |
{Subscription} = sandboxedRequire("../lib/subscriptionClasses") |
); |
callback(); |
}; |
+function addListener(listener) |
+{ |
+ let makeWrapper = name => (...args) => listener(name, ...args); |
+ |
+ FilterNotifier.on("subscription.added", makeWrapper("subscription.added")); |
Manish Jethani
2018/08/11 14:47:06
Since there is no catch-all event (unlike FilterNo
|
+ FilterNotifier.on("subscription.removed", |
+ makeWrapper("subscription.removed")); |
+ FilterNotifier.on("subscription.moved", makeWrapper("subscription.moved")); |
+ |
+ FilterNotifier.on("filter.added", makeWrapper("filter.added")); |
+ FilterNotifier.on("filter.removed", makeWrapper("filter.removed")); |
+ FilterNotifier.on("filter.moved", makeWrapper("filter.moved")); |
+ |
+ FilterNotifier.on("filter.hitCount", makeWrapper("filter.hitCount")); |
+ FilterNotifier.on("filter.lastHit", makeWrapper("filter.lastHit")); |
+} |
+ |
function compareSubscriptionList(test, testMessage, list) |
{ |
let result = FilterStorage.subscriptions.map(subscription => subscription.url); |
let expected = list.map(subscription => subscription.url); |
test.deepEqual(result, expected, testMessage); |
} |
function compareFiltersList(test, testMessage, list) |
@@ -67,17 +84,17 @@ |
let subscription2 = Subscription.fromURL("http://test2/"); |
let changes = []; |
function listener(action, subscription) |
{ |
if (action.indexOf("subscription.") == 0) |
changes.push(action + " " + subscription.url); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
compareSubscriptionList(test, "Initial state", []); |
test.deepEqual(changes, [], "Received changes"); |
changes = []; |
FilterStorage.addSubscription(subscription1); |
compareSubscriptionList(test, "Adding first subscription", [subscription1]); |
test.deepEqual(changes, ["subscription.added http://test1/"], "Received changes"); |
@@ -111,17 +128,17 @@ |
FilterStorage.addSubscription(subscription2); |
let changes = []; |
function listener(action, subscription) |
{ |
if (action.indexOf("subscription.") == 0) |
changes.push(action + " " + subscription.url); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
compareSubscriptionList(test, "Initial state", [subscription1, subscription2]); |
test.deepEqual(changes, [], "Received changes"); |
changes = []; |
FilterStorage.removeSubscription(subscription1); |
compareSubscriptionList(test, "Removing first subscription", [subscription2]); |
test.deepEqual(changes, ["subscription.removed http://test1/"], "Received changes"); |
@@ -158,17 +175,17 @@ |
FilterStorage.addSubscription(subscription3); |
let changes = []; |
function listener(action, subscription) |
{ |
if (action.indexOf("subscription.") == 0) |
changes.push(action + " " + subscription.url); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
compareSubscriptionList(test, "Initial state", [subscription1, subscription2, subscription3]); |
test.deepEqual(changes, [], "Received changes"); |
changes = []; |
FilterStorage.moveSubscription(subscription1); |
compareSubscriptionList(test, "Move without explicit position", [subscription2, subscription3, subscription1]); |
test.deepEqual(changes, ["subscription.moved http://test1/"], "Received changes"); |
@@ -219,17 +236,17 @@ |
FilterStorage.addSubscription(subscription3); |
let changes = []; |
function listener(action, filter) |
{ |
if (action.indexOf("filter.") == 0) |
changes.push(action + " " + filter.text); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
compareFiltersList(test, "Initial state", [[], [], []]); |
test.deepEqual(changes, [], "Received changes"); |
changes = []; |
FilterStorage.addFilter(Filter.fromText("foo")); |
compareFiltersList(test, "Adding blocking filter", [["foo"], [], []]); |
test.deepEqual(changes, ["filter.added foo"], "Received changes"); |
@@ -295,17 +312,17 @@ |
FilterStorage.addSubscription(subscription3); |
let changes = []; |
function listener(action, filter) |
{ |
if (action.indexOf("filter.") == 0) |
changes.push(action + " " + filter.text); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
compareFiltersList(test, "Initial state", [["foo", "foo", "bar"], ["foo", "bar", "foo"], ["foo", "bar"]]); |
test.deepEqual(changes, [], "Received changes"); |
changes = []; |
FilterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0); |
compareFiltersList(test, "Remove with explicit subscription and position", [["foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]); |
test.deepEqual(changes, ["filter.removed foo"], "Received changes"); |
@@ -350,17 +367,17 @@ |
FilterStorage.addSubscription(subscription2); |
let changes = []; |
function listener(action, filter) |
{ |
if (action.indexOf("filter.") == 0) |
changes.push(action + " " + filter.text); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
compareFiltersList(test, "Initial state", [["foo", "bar", "bas", "foo"], ["foo", "bar"]]); |
test.deepEqual(changes, [], "Received changes"); |
changes = []; |
FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 1); |
compareFiltersList(test, "Regular move", [["bar", "foo", "bas", "foo"], ["foo", "bar"]]); |
test.deepEqual(changes, ["filter.moved foo"], "Received changes"); |
@@ -391,17 +408,17 @@ |
exports.testHitCounts = function(test) |
{ |
let changes = []; |
function listener(action, filter) |
{ |
if (action.indexOf("filter.") == 0) |
changes.push(action + " " + filter.text); |
} |
- FilterNotifier.addListener(listener); |
+ addListener(listener); |
let filter1 = Filter.fromText("filter1"); |
let filter2 = Filter.fromText("filter2"); |
FilterStorage.addFilter(filter1); |
test.equal(filter1.hitCount, 0, "filter1 initial hit count"); |
test.equal(filter2.hitCount, 0, "filter2 initial hit count"); |