This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathhterm_pubsub_tests.js
127 lines (100 loc) · 3.18 KB
/
hterm_pubsub_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview hterm.Pubsub unit tests.
*/
describe('hterm_pubsub_tests.js', () => {
/**
* Test that the appropriate methods are added to a hterm.PubSub target object.
*/
it('methods', () => {
const obj = {};
hterm.PubSub.addBehavior(obj);
assert.hasAllKeys(obj, ['subscribe', 'unsubscribe', 'publish']);
});
/**
* Test that subscribers are notified in the proper order.
*/
it('publish-order', (done) => {
let callbackCount = 0;
function one() { assert.equal(1, ++callbackCount); }
function two() { assert.equal(2, ++callbackCount); }
function three() { assert.equal(3, ++callbackCount); }
function last() { assert.equal(4, ++callbackCount); done(); }
const obj = /** @type {!hterm.PubSub} */ ({});
hterm.PubSub.addBehavior(obj);
obj.subscribe('test', one);
obj.subscribe('test', two);
obj.subscribe('test', three);
obj.publish('test', null, last);
});
/**
* Test that a published parameter is handed off to all subscribers.
*/
it('parameter', (done) => {
const expected = {};
function one(param) { assert.deepStrictEqual(expected, param); }
function two(param) { assert.deepStrictEqual(expected, param); }
function three(param) { assert.deepStrictEqual(expected, param); }
function last(param) { assert.deepStrictEqual(expected, param); done(); }
const obj = /** @type {!hterm.PubSub} */ ({});
hterm.PubSub.addBehavior(obj);
obj.subscribe('test', one);
obj.subscribe('test', two);
obj.subscribe('test', three);
obj.publish('test', expected, last);
});
/**
* Test that the final callback is invoked, even if nobody has subscribed.
*/
it('forever-alone', (done) => {
let calledLast = false;
function last(param) { calledLast = true; }
const obj = /** @type {!hterm.PubSub} */ ({});
hterm.PubSub.addBehavior(obj);
obj.publish('test', null, last);
const check = () => {
if (calledLast) {
done();
} else {
setTimeout(check, 1);
}
};
check();
});
/**
* Test that an exception raised by a subscriber does not stop the remaining
* notifications.
*/
it('exception', function(done) {
// We need to manually disable this.
// https://github.com/mochajs/mocha/issues/1985
const oldOnerror = window.onerror;
window.onerror = () => true;
const calledFoo = false;
let calledBar = false;
let calledLast = false;
function foo() { throw new Error('EXPECTED_EXCEPTION'); }
function bar() { calledBar = true; }
function last() { calledLast = true; }
const obj = /** @type {!hterm.PubSub} */ ({});
hterm.PubSub.addBehavior(obj);
obj.subscribe('test', foo);
obj.subscribe('test', bar);
obj.publish('test', null, last);
const check = () => {
if (calledLast) {
assert.isFalse(calledFoo);
assert.isTrue(calledBar);
assert.isTrue(calledLast);
window.onerror = oldOnerror;
done();
} else {
setTimeout(check, 1);
}
};
check();
});
});