Skip to content

Commit 9d11928

Browse files
committed
Merge pull request #6 from cujojs/add-unit-tests
Add unit tests
2 parents f00c76a + 71269ef commit 9d11928

File tree

5 files changed

+148
-8
lines changed

5 files changed

+148
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ If the WebSocket closes before the stream ends, the returned promise will fulfil
5454
5555
####`fromEventSource(eventSource [, dispose]) -> Stream`
5656
57+
### fromEventSourceOn
58+
59+
####`fromEventSourceOn(eventName, eventSource [, dispose]) -> Stream`
60+
5761
### fromMessagePort
5862
5963
####`fromMessagePort(port [, dispose]) -> Stream`

most-w3msg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @license MIT License (c) copyright 2010-2014 original author or authors */
1+
/** @license MIT License (c) copyright 2010-2015 original author or authors */
22
/** @author Brian Cavalier */
33
/** @author John Hann */
44

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"author": "brian@hovercraftstudios.com",
1717
"license": "MIT",
1818
"dependencies": {
19-
"most": "~0.8"
19+
"most": "^0.13.0"
2020
},
2121
"devDependencies": {
2222
"jshint": "~2.5",

test/helper/fakes.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
exports.FakeEventTarget = FakeEventTarget;
2+
exports.FakeEventSource = FakeEventSource;
3+
exports.FakeWebSocket = FakeWebSocket;
4+
exports.FakeMessagePort = FakeMessagePort;
5+
6+
function FakeEventTarget() {
7+
this._events = {};
8+
}
9+
10+
FakeEventTarget.prototype.emit = function(e, x) {
11+
var handler = this._events[e];
12+
if(typeof handler !== 'function') {
13+
return;
14+
}
15+
handler(x);
16+
};
17+
18+
FakeEventTarget.prototype.addEventListener = function(e, handler) {
19+
this._events[e] = handler;
20+
};
21+
22+
FakeEventTarget.prototype.removeEventListener = function(e, handler) {
23+
if(handler !== this._events[e]) {
24+
throw new Error('removed wrong handler');
25+
}
26+
this._events[e] = void 0;
27+
};
28+
29+
function FakeEventSource() {
30+
FakeEventTarget.call(this);
31+
this.isOpen = true;
32+
}
33+
34+
FakeEventSource.prototype = Object.create(FakeEventTarget.prototype);
35+
36+
FakeEventSource.prototype.close = function() {
37+
if(!this.isOpen) {
38+
throw new Error('closed more than once');
39+
}
40+
this.isOpen = false;
41+
this.emit('close', void 0);
42+
};
43+
44+
function FakeWebSocket() {
45+
FakeEventSource.call(this);
46+
}
47+
48+
FakeWebSocket.prototype = Object.create(FakeEventSource.prototype);
49+
50+
FakeWebSocket.prototype.send = function(x) {
51+
this.emit('message', x);
52+
};
53+
54+
function FakeMessagePort() {
55+
FakeEventTarget.call(this);
56+
}
57+
58+
FakeMessagePort.prototype = Object.create(FakeEventTarget.prototype);
59+
60+
FakeMessagePort.prototype.postMessage = function(x) {
61+
this.emit('message', x);
62+
};

test/most-w3msg-test.js

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,88 @@
11
require('buster').spec.expose();
2-
//var expect = require('buster').expect;
2+
var expect = require('buster').expect;
3+
var fakes = require('./helper/fakes');
34

45
var mws = require('../most-w3msg');
5-
var fromWebSocket = mws.fromWebSocket;
6-
var toWebSocket = mws.toWebSocket;
6+
7+
var sentinel = { value: 'sentinel' };
78

89
describe('fromWebSocket', function() {
9-
it('should contain messages received by WebSocket');
10+
it('should contain messages received by WebSocket', function() {
11+
var ws = new fakes.FakeWebSocket();
12+
13+
var s = mws.fromWebSocket(ws, ws.close.bind(ws));
14+
var spy = this.spy();
15+
16+
setTimeout(function() {
17+
ws.send(sentinel);
18+
}, 0);
19+
20+
return s.take(1).observe(spy).then(function() {
21+
expect(spy).toHaveBeenCalledOnceWith(sentinel);
22+
});
23+
});
24+
25+
it('should call disposer when stream ends', function() {
26+
var ws = new fakes.FakeWebSocket();
27+
28+
var spy = this.spy();
29+
var s = mws.fromWebSocket(ws, spy);
30+
31+
setTimeout(function() {
32+
ws.send(sentinel);
33+
}, 0);
34+
35+
return s.take(1).drain().then(function() {
36+
expect(spy).toHaveBeenCalledOnce();
37+
});
38+
});
39+
});
40+
41+
describe('fromEventSource', function() {
42+
it('should contain messages received by EventSource', function() {
43+
var es = new fakes.FakeEventSource();
44+
45+
var s = mws.fromEventSource(es, es.close.bind(es));
46+
var spy = this.spy();
47+
48+
setTimeout(function() {
49+
es.emit('message', sentinel);
50+
}, 0);
51+
52+
return s.take(1).observe(spy).then(function() {
53+
expect(spy).toHaveBeenCalledOnceWith(sentinel);
54+
});
55+
});
56+
57+
it('should call disposer when stream ends', function() {
58+
var es = new fakes.FakeEventSource();
59+
60+
var spy = this.spy();
61+
var s = mws.fromEventSource(es, spy);
62+
63+
setTimeout(function() {
64+
es.emit('message', sentinel);
65+
}, 0);
66+
67+
return s.take(1).drain().then(function() {
68+
expect(spy).toHaveBeenCalledOnce();
69+
});
70+
});
1071
});
1172

12-
describe('toWebSocket', function() {
13-
it('should send messages to WebSocket');
73+
describe('fromEventSourceOn', function() {
74+
it('should contain events received by EventSource', function() {
75+
var es = new fakes.FakeEventSource();
76+
77+
var s = mws.fromEventSourceOn('test', es, es.close.bind(es));
78+
var spy = this.spy();
79+
80+
setTimeout(function() {
81+
es.emit('test', sentinel);
82+
}, 0);
83+
84+
return s.take(1).observe(spy).then(function() {
85+
expect(spy).toHaveBeenCalledOnceWith(sentinel);
86+
});
87+
});
1488
});

0 commit comments

Comments
 (0)