This repository has been archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
tv.js
executable file
·153 lines (103 loc) · 3.81 KB
/
tv.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Load modules
var Chai = require('chai');
var Ws = require('ws');
var Tv = require('../lib/tv');
// Declare internals
var internals = {};
// Test shortcuts
var expect = Chai.expect;
describe('Tv', function () {
describe('#constructor', function () {
it('cannot be constructed without new', function (done) {
var fn = function () {
var tv = Tv();
};
expect(fn).to.throw(Error);
done();
});
it('can be constructed with new', function (done) {
var fn = function () {
var tv = new Tv({ websocketPort: 3001 });
};
expect(fn).to.not.throw(Error);
done();
});
it('uses the tv defaults when no config is passed in', function (done) {
var tv = new Tv();
expect(tv.settings.host).to.equal('0.0.0.0');
expect(tv.settings.websocketPort).to.equal(3000);
done();
});
it('uses the passed in config', function (done) {
var tv = new Tv({ host: 'localhost', websocketPort: 3002 });
expect(tv.settings.host).to.equal('localhost');
expect(tv.settings.websocketPort).to.equal(3002);
done();
});
it('adds message to subscribers list when receiving message', function (done) {
var config = { host: 'localhost', websocketPort: 3010 }
var tv = new Tv(config);
var ws = new Ws("ws://" + config.host + ':' + config.websocketPort);
ws.readyState = Ws.OPEN;
ws.on('open', function () {
ws.send("test1");
setTimeout(function () {
expect(tv._subscribers["test1"]).to.exist;
done();
}, 100);
});
});
});
describe('#report', function () {
it('sends the data to all subscribers when session is null', function (done) {
var tv = new Tv({ websocketPort: 3003 });
tv._subscribers['*'] = [{
readyState: Ws.OPEN,
send: function (message) {
expect(message).to.exist;
expect(message).to.equal('"test"');
done();
}
}];
tv.report('test');
});
it('only sends a message to the appropriate subscribers', function (done) {
var tv = new Tv({ websocketPort: 3004 });
tv._subscribers['*'] = [{
readyState: Ws.OPEN,
send: function (message) {
expect(message).to.not.exist;
}
}];
tv._subscribers['test'] = [{
readyState: Ws.OPEN,
send: function (message) {
expect(message).to.exist;
expect(message).to.equal('"test"');
done();
}
}];
tv.report('test', 'test');
});
it('only sends a message when the websocket exists', function (done) {
var tv = new Tv({ websocketPort: 3005 });
tv._subscribers['*'] = [{
readyState: 'none',
send: function (message) {
expect(message).to.not.exist;
}
}];
tv.report('test');
done();
});
});
describe('#getMarkup', function () {
it('includes the hostname and port in the source', function (done) {
var tv = new Tv({ host: 'localhost', websocketPort: 3006 });
var html = tv.getMarkup();
expect(html).to.contain('localhost');
expect(html).to.contain('3006');
done();
});
});
});