This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
125 lines (106 loc) · 3.29 KB
/
test.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
var expect = chai.expect;
describe('WebSocket', function () {
beforeEach(module('ws'));
describe('provider', function () {
describe('#setUrl', function () {
it('should define url', function () {
module(function (wsProvider) {
wsProvider.setUrl('ws://echo.websocket.org/');
});
inject(function (ws) {
ws.connect();
expect(ws.baseSocket.url).to.equal('ws://echo.websocket.org/');
});
});
});
describe('#setProtocols', function () {
it('should define protocols', function () {
module(function (wsProvider) {
wsProvider.setUrl('ws://echo.websocket.org/');
wsProvider.setProtocols('a');
});
inject(function (ws) {
ws.connect();
expect(ws.baseSocket.url).to.equal('ws://echo.websocket.org/');
});
});
});
describe('#setTransport', function () {
it('should define transport', function () {
var CustomWs = function () {};
CustomWs.prototype.addEventListener = function () {};
module(function (wsProvider) {
wsProvider.setTransport(CustomWs);
wsProvider.setProtocols('a');
});
inject(function (ws) {
ws.connect();
expect(ws.baseSocket).to.be.instanceOf(CustomWs);
});
});
});
});
describe('service', function () {
var ws;
beforeEach(inject(function ($injector) {
ws = $injector.get('ws');
}));
describe('#connect', function () {
describe('with error', function () {
it('should reject promise', function (done) {
ws.connect({url: 'ws://google.com'})
.catch(function (event) {
expect(event.type).to.equal('error');
done();
});
});
});
describe('without error', function () {
it('should resolve promise', function (done) {
ws.connect({url: 'ws://echo.websocket.org'})
.then(function (_ws) {
expect(_ws).to.equal(ws);
done();
});
});
});
});
describe('#getReadyState', function () {
it('should return null if the websocket is not here', function () {
expect(ws.getReadyState()).to.be.null;
});
it('should return the ready state of the WebSocket if connected', function () {
ws.connect({url: 'ws://echo.websocket.org'});
expect(ws.getReadyState()).to.equal(0);
});
});
describe('#on', function () {
it('should emit event correctly', function (done) {
ws.connect({url: 'ws://echo.websocket.org'});
ws.on('open', function () {
done();
});
});
});
describe('#send', function () {
it('should send and receive message', function (done) {
ws.connect({url: 'ws://echo.websocket.org'});
ws.send('hello');
ws.on('message', function (event) {
expect(event.data).to.equal('hello');
done();
});
});
});
describe('#close', function () {
it('should close the socket', function (done) {
ws.connect({url: 'ws://echo.websocket.org'});
ws.close();
ws.on('close', function () {
expect(ws.getReadyState()).to.equal(3);
done();
});
});
});
});
});