addon-sdk/source/test/test-dispatcher.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:bcfcee801830
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 { dispatcher } = require("sdk/util/dispatcher");
8
9 exports["test dispatcher API"] = assert => {
10 const dispatch = dispatcher();
11
12 assert.equal(typeof(dispatch), "function",
13 "dispatch is a function");
14
15 assert.equal(typeof(dispatch.define), "function",
16 "dispatch.define is a function");
17
18 assert.equal(typeof(dispatch.implement), "function",
19 "dispatch.implement is a function");
20
21 assert.equal(typeof(dispatch.when), "function",
22 "dispatch.when is a function");
23 };
24
25 exports["test dispatcher"] = assert => {
26 const isDuck = dispatcher();
27
28 const quacks = x => x && typeof(x.quack) === "function";
29
30 const Duck = function() {};
31 const Goose = function() {};
32
33 const True = _ => true;
34 const False = _ => false;
35
36
37
38 isDuck.define(Goose, False);
39 isDuck.define(Duck, True);
40 isDuck.when(quacks, True);
41
42 assert.equal(isDuck(new Goose()), false,
43 "Goose ain't duck");
44
45 assert.equal(isDuck(new Duck()), true,
46 "Ducks are ducks");
47
48 assert.equal(isDuck({ quack: () => "Quaaaaaack!" }), true,
49 "It's a duck if it quacks");
50
51
52 assert.throws(() => isDuck({}), /Type does not implements method/, "not implemneted");
53
54 isDuck.define(Object, False);
55
56 assert.equal(isDuck({}), false,
57 "Ain't duck if it does not quacks!");
58 };
59
60 exports["test redefining fails"] = assert => {
61 const isPM = dispatcher();
62 const isAfternoon = time => time.getHours() > 12;
63
64 isPM.when(isAfternoon, _ => true);
65
66 assert.equal(isPM(new Date(Date.parse("Jan 23, 1985, 13:20:00"))), true,
67 "yeap afternoon");
68 assert.equal(isPM({ getHours: _ => 17 }), true,
69 "seems like afternoon");
70
71 assert.throws(() => isPM.when(isAfternoon, x => x > 12 && x < 24),
72 /Already implemented for the given predicate/,
73 "can't redefine on same predicate");
74
75 };
76
77 require("sdk/test").run(exports);

mercurial