addon-sdk/source/test/event/helpers.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:b4cfd05e769f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 "use strict";
6
7 const { on, once, off, emit, count } = require("sdk/event/core");
8
9 function scenario(setup) {
10 return function(unit) {
11 return function(assert) {
12 let actual = [];
13 let input = {};
14 unit(input, function(output, events, expected, message) {
15 let result = setup(output, expected, actual);
16
17 events.forEach(function(event) emit(input, "data", event));
18
19 assert.deepEqual(actual, result, message);
20 });
21 }
22 }
23 }
24
25 exports.emits = scenario(function(output, expected, actual) {
26 on(output, "data", function(data) actual.push(this, data));
27
28 return expected.reduce(function($$, $) $$.concat(output, $), []);
29 });
30
31 exports.registerOnce = scenario(function(output, expected, actual) {
32 function listener(data) actual.push(data);
33 on(output, "data", listener);
34 on(output, "data", listener);
35 on(output, "data", listener);
36
37 return expected;
38 });
39
40 exports.ignoreNew = scenario(function(output, expected, actual) {
41 on(output, "data", function(data) {
42 actual.push(data + "#1");
43 on(output, "data", function(data) {
44 actual.push(data + "#2");
45 });
46 });
47
48 return expected.map(function($) $ + "#1");
49 });
50
51 exports.FIFO = scenario(function(target, expected, actual) {
52 on(target, "data", function($) actual.push($ + "#1"));
53 on(target, "data", function($) actual.push($ + "#2"));
54 on(target, "data", function($) actual.push($ + "#3"));
55
56 return expected.reduce(function(result, value) {
57 return result.concat(value + "#1", value + "#2", value + "#3");
58 }, []);
59 });

mercurial