Repository navigation
Expand file tree
/
Copy pathswitchboard.test.js
More file actions
140 lines (107 loc) · 4.36 KB
/
Copy pathswitchboard.test.js
File metadata and controls
140 lines (107 loc) · 4.36 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
import {CompositeDisposable} from 'event-kit';
import Switchboard from '../lib/switchboard';
describe('Switchboard', function() {
let switchboard;
beforeEach(function() {
switchboard = new Switchboard();
});
describe('events', function() {
let sub;
afterEach(function() {
sub && sub.dispose();
});
it('synchronously broadcasts events', function() {
let observed = 0;
sub = switchboard.onDid('test', () => observed++);
assert.equal(observed, 0);
switchboard.did('test');
assert.equal(observed, 1);
});
});
describe('promises', function() {
it('creates and resolves a Promise for an event', async function() {
const promise = switchboard.getPromise('testing');
const payload = {};
switchboard.did('testing', payload);
const result = await promise;
assert.strictEqual(result, payload);
});
it('supports multiple consumers of the same Promise', async function() {
const promise0 = switchboard.getPromise('testing');
const promise1 = switchboard.getPromise('testing');
assert.strictEqual(promise0, promise1);
const payload = {};
switchboard.did('testing', payload);
assert.strictEqual(await promise0, payload);
assert.strictEqual(await promise1, payload);
});
it('creates new Promises for repeated events', async function() {
const promise0 = switchboard.getPromise('testing');
switchboard.did('testing', 0);
assert.equal(await promise0, 0);
const promise1 = switchboard.getPromise('testing');
switchboard.did('testing', 1);
assert.equal(await promise1, 1);
});
it('"resolves" an event that has no Promise', function() {
switchboard.did('anybody-there', {});
});
});
// Ensure that all of the `didXyz`, `onDidXyz`, and `getXyzPromise` method triplets are aligned correctly.
describe('function triplets', function() {
const baseNames = Object.getOwnPropertyNames(Switchboard.prototype)
.map(methodName => /^did(.+)$/.exec(methodName))
.filter(match => match !== null)
.map(match => match[1]);
let functionTriples;
beforeEach(function() {
functionTriples = baseNames.map(baseName => {
return {
baseName,
subscriber: switchboard[`onDid${baseName}`].bind(switchboard),
getter: switchboard[`get${baseName}Promise`].bind(switchboard),
resolver: switchboard[`did${baseName}`].bind(switchboard),
};
});
});
baseNames.forEach(baseName => {
it(`resolves the correct Promise for ${baseName}`, async function() {
const allPromises = [];
const positiveResults = [];
const negativeResults = [];
let positiveResolver = null;
const negativeResolvers = [];
const subscriptions = new CompositeDisposable();
for (let i = 0; i < functionTriples.length; i++) {
const functionTriple = functionTriples[i];
if (functionTriple.baseName === baseName) {
const positivePromise = functionTriple.getter().then(payload => {
positiveResults.push(payload);
});
allPromises.push(positivePromise);
positiveResolver = functionTriple.resolver;
const positiveSubscription = functionTriple.subscriber(payload => positiveResults.push(payload));
subscriptions.add(positiveSubscription);
} else {
const negativePromise = functionTriple.getter().then(payload => {
negativeResults.push(payload);
});
allPromises.push(negativePromise);
negativeResolvers.push(functionTriple.resolver);
const negativeSubscription = functionTriple.subscriber(payload => negativeResults.push(payload));
subscriptions.add(negativeSubscription);
}
}
// Resolve positive resolvers with "yes" and negative resolvers with "no"
positiveResolver('yes');
negativeResolvers.forEach(resolver => resolver('no'));
await Promise.all(allPromises);
subscriptions.dispose();
assert.lengthOf(positiveResults, 2);
assert.isTrue(positiveResults.every(result => result === 'yes'));
assert.lengthOf(negativeResults, (baseNames.length - 1) * 2);
assert.isTrue(negativeResults.every(result => result === 'no'));
});
});
});
});