forked from PatrickJS/angular-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-websocket.spec.js
923 lines (680 loc) · 27.3 KB
/
angular-websocket.spec.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
describe('angular-websocket', function() {
describe('$websocketBackend', function() {
var $window, $websocket, $websocketBackend, WSMock, localMocks = {};
beforeEach(module('ngWebSocket'));
beforeEach(inject(function (_$window_, _$websocket_, _$websocketBackend_) {
$window = _$window_;
$websocket = _$websocket_;
$websocketBackend = _$websocketBackend_;
localMocks.sendMock = function() {};
localMocks.closeMock = function() {};
WSMock = function(url) {
this.send = localMocks.sendMock;
this.close = localMocks.closeMock;
};
$window.WebSocket = WSMock;
}));
it('should complain if not given a valid url', function() {
expect(function() {
$websocketBackend.create('%foobar/baz');
})
.toThrowError('Invalid url provided');
});
});
describe('$websocket', function() {
var $window, $websocket, $websocketBackend, WSMock, localMocks = {};
beforeEach(module('ngWebSocket', 'ngWebSocketMock'));
beforeEach(inject(function (_$window_, _$websocket_, _$websocketBackend_) {
$window = _$window_;
$websocket = _$websocket_;
$websocketBackend = _$websocketBackend_;
localMocks.sendMock = function() {};
localMocks.closeMock = function() {};
WSMock = function(url) {
this.send = localMocks.sendMock;
this.close = localMocks.closeMock;
};
$window.WebSocket = WSMock;
}));
afterEach(function() {
$websocketBackend.verifyNoOutstandingRequest();
$websocketBackend.verifyNoOutstandingExpectation();
});
it('should accept a wss url', function() {
var url = 'wss://foo/secure';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
expect(ws.ssl).toEqual(true);
$websocketBackend.flush();
});
it('should accept protocols', function() {
var url = 'ws://foo/secure';
var protocols1 = 'Swag-Protocol';
$websocketBackend.expectConnect(url, protocols1);
var ws1 = $websocket(url, protocols1);
expect(ws1.protocols).toEqual('Swag-Protocol');
var url2 = 'ws://foo/secure';
$websocketBackend.expectConnect(url2);
var ws2 = $websocket(url2);
expect(ws2.protocols).toEqual(undefined);
var url3 = 'ws://foo/secure';
var protocols3 = ['Swag-Protocol', 'Sec-WebSocket-Protocol'];
$websocketBackend.expectConnect(url3, protocols3);
var ws3 = $websocket(url3, protocols3);
expect(ws3.protocols).toEqual(['Swag-Protocol', 'Sec-WebSocket-Protocol']);
$websocketBackend.flush();
});
it('should return an object containing a reference to the WebSocket instance', function() {
var url = 'ws://reference';
$websocketBackend.expectConnect(url);
expect(typeof $websocket(url).socket.send).toBe('function');
$websocketBackend.flush();
});
it('should have a separate sendQueue for each instance', function() {
var url1 = 'ws://foo/one';
var url2 = 'ws://foo/two';
$websocketBackend.expectConnect(url1);
var ws1 = $websocket(url1);
$websocketBackend.expectConnect(url2);
var ws2 = $websocket(url2);
ws1.send('baz');
expect(ws1.sendQueue.length).toBe(1);
expect(ws2.sendQueue.length).toBe(0);
$websocketBackend.flush();
});
describe('._connect()', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo/bar';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
});
afterEach(function() {
$websocketBackend.flush();
});
it('should attempt connecting to a socket if provided a valid URL', function() {
ws.socket = null;
ws._connect();
});
it('should not connect if a socket has a readyState of 1', function() {
ws.socket.readyState = 1;
ws._connect();
});
it('should force reconnect if force parameter is true', function() {
ws.socket.readyState = 1;
$websocketBackend.expectConnect(url);
ws._connect(true);
});
it('should attach handlers to socket event attributes', function() {
expect(typeof ws.socket.onopen).toBe('function');
expect(typeof ws.socket.onmessage).toBe('function');
expect(typeof ws.socket.onerror).toBe('function');
expect(typeof ws.socket.onclose).toBe('function');
});
});
describe('.close()', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
$websocketBackend.flush();
});
afterEach(function() {
$websocketBackend.flush();
});
it('should call close on the underlying socket', function() {
$websocketBackend.expectClose();
ws.close();
});
it('should not call close if the bufferedAmount is greater than 0', function() {
ws.socket.bufferedAmount = 5;
ws.close();
});
it('should accept a force param to close the socket even if bufferedAmount is greater than 0', function() {
$websocketBackend.expectClose();
ws.socket.bufferedAmount = 5;
ws.close(true);
});
});
describe('.reconnect', function() {
it('should call .close', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
$websocketBackend.expectClose();
ws.reconnect();
$websocketBackend.flush();
});
it('should call ._connect if the _getBackoffDelay is 0', inject(function($timeout) {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
var spy = spyOn(ws, '_connect');
ws.reconnect();
$timeout.flush();
expect(spy).toHaveBeenCalled();
$websocketBackend.flush();
}));
it('should not call ._connect if the _getBackoffDelay is not 0', inject(function($timeout) {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
var spy = spyOn(ws, '_connect');
var spyBackoff = spyOn(ws, '_getBackoffDelay').and.callFake(function() {
return 9001;
});
ws.reconnect();
$timeout.flush(9000);
expect(spy).not.toHaveBeenCalled();
$timeout.flush(1);
expect(spy).toHaveBeenCalled();
$websocketBackend.flush();
}));
});
describe('.safeDigest', function() {
it('should force digest if force parameter is true', inject(function($rootScope) {
var digest = spyOn($rootScope, '$digest');
var url = 'ws://foo/safeDigest';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
expect(digest).not.toHaveBeenCalled();
ws.safeDigest(true);
expect(digest).toHaveBeenCalled();
$websocketBackend.flush();
}));
it('should not digest if force parameter and $$phase are true', inject(function($rootScope) {
var digest = spyOn($rootScope, '$digest');
var url = 'ws://foo/safeDigest';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
expect(digest).not.toHaveBeenCalled();
$rootScope.$$phase = true;
ws.safeDigest(true);
expect(digest).not.toHaveBeenCalled();
$websocketBackend.flush();
}));
it('should digest scope parameter', inject(function($rootScope) {
var isolateScope = $rootScope.$new(true);
var digest = spyOn($rootScope, '$digest');
var childDigest = spyOn(isolateScope, '$digest');
var url = 'ws://foo/safeDigest';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
ws.scope = isolateScope;
expect(digest).not.toHaveBeenCalled();
expect(childDigest).not.toHaveBeenCalled();
ws.safeDigest(true);
expect(childDigest).toHaveBeenCalled();
expect(digest).not.toHaveBeenCalled();
$websocketBackend.flush();
}));
});
describe('.bindToScope', function() {
it('should not bind to scope if no parameters are provided', inject(function($rootScope) {
// var digest = spyOn($rootScope, '$digest');
var url = 'ws://foo/bindToScope';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
expect(ws.scope).toBe($rootScope);
ws.bindToScope();
expect(ws.scope).toBe($rootScope);
$websocketBackend.flush();
}));
it('should bind to scope if a scope is provided', inject(function($rootScope) {
var isolateScope = $rootScope.$new(true);
var url = 'ws://foo/bindToScope';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
expect(ws.scope).toBe($rootScope);
ws.bindToScope(isolateScope);
expect(ws.scope).toBe(isolateScope);
$websocketBackend.flush();
}));
it('should bind to rootScope if a scope is provided and is destroyed', inject(function($rootScope) {
var isolateScope = $rootScope.$new(true);
var url = 'ws://foo/bindToScope';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
ws.rootScopeFailover = true;
expect(ws.scope).toBe($rootScope);
ws.bindToScope(isolateScope);
expect(ws.scope).toBe(isolateScope);
isolateScope.$destroy();
expect(ws.scope).toBe($rootScope);
$websocketBackend.flush();
}));
});
describe('._onCloseHandler', function() {
it('should call .reconnect if the CloseEvent contains a reconnectable status code and the reconnectIfNotNormalClose is false', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url, {reconnectIfNotNormalClose: false});
var spy = spyOn(ws, 'reconnect');
ws._onCloseHandler({code: 4000});
expect(spy).toHaveBeenCalled();
$websocketBackend.flush();
});
it('should call .reconnect if the CloseEvent contains a reconnectable status code and the reconnectIfNotNormalClose is true', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url, {reconnectIfNotNormalClose: true});
var spy = spyOn(ws, 'reconnect');
ws._onCloseHandler({code: 4000});
expect(spy).toHaveBeenCalled();
$websocketBackend.flush();
});
it('should not call .reconnect if the CloseEvent indicates an intentional close and the reconnectIfNotNormalClose flag is false', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url, {reconnectIfNotNormalClose: false});
var spy = spyOn(ws, 'reconnect');
ws._onCloseHandler({code: 1000});
expect(spy).not.toHaveBeenCalled();
$websocketBackend.flush();
});
it('should not call .reconnect if the CloseEvent indicates an intentional close and the reconnectIfNotNormalClose flag is true', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url, {reconnectIfNotNormalClose: true});
var spy = spyOn(ws, 'reconnect');
ws._onCloseHandler({code: 1000});
expect(spy).not.toHaveBeenCalled();
$websocketBackend.flush();
});
it('should not call .reconnect if the CloseEvent indicates a non-intentional close and the reconnectIfNotNormalClose flag is false', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url, {reconnectIfNotNormalClose: false});
var spy = spyOn(ws, 'reconnect');
ws._onCloseHandler({code: 1001});
expect(spy).not.toHaveBeenCalled();
$websocketBackend.flush();
});
it('should call .reconnect if the CloseEvent indicates a non-intentional close and the reconnectIfNotNormalClose flag is true', function() {
var url = 'ws://foo/onclose';
$websocketBackend.expectConnect(url);
var ws = $websocket(url, {reconnectIfNotNormalClose: true});
var spy = spyOn(ws, 'reconnect');
ws._onCloseHandler({code: 1001});
expect(spy).toHaveBeenCalled();
$websocketBackend.flush();
});
});
describe('.onOpen()', function() {
it('should add the passed in function to the onOpenCallbacks array', function() {
var cb = function() {};
var url = 'ws://foo';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
ws.onOpen(cb);
expect(ws.onOpenCallbacks[0]).toBe(cb);
$websocketBackend.flush();
});
});
describe('.send()', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo/bar';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
});
afterEach(function() {
$websocketBackend.flush();
});
it('should queue change if the "onopen" event has not yet occurred', function() {
var data = {message: 'Send me'};
ws.send(data);
expect(ws.sendQueue.length).toBe(1);
expect(ws.sendQueue[0].message).toBe(data);
});
it('should accept a string as data', function() {
var data = 'I am a string';
ws.send(data);
expect(ws.sendQueue[0].message).toBe(data);
});
it('should call fireQueue immediately', function() {
var spy = spyOn(ws, 'fireQueue');
ws.send('send me');
expect(spy).toHaveBeenCalled();
});
it('should return a promise', function() {
expect(typeof ws.send('promise?').then).toBe('function');
});
it('should return a cancelable promise', function() {
expect(typeof ws.send('promise?').cancel).toBe('function');
});
it('should return reject a cancelled send with reason if provided', inject(function($rootScope) {
var reason = 'bad data';
var spy = jasmine.createSpy('reject');
ws.send('foo').then(null, spy).cancel(reason);
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith('bad data');
}));
it('should remove the request from the queue when calling cancel', function() {
ws.sendQueue = ['bar','baz'];
var sent = ws.send('foo');
expect(ws.sendQueue[2].message).toBe('foo');
sent.cancel();
expect(ws.sendQueue.length).toBe(2);
expect(ws.sendQueue.indexOf('foo')).toBe(-1);
});
it('should reject the promise when readyState is 4', inject(function($rootScope) {
var spy = jasmine.createSpy('reject');
ws._internalConnectionState = 4;
ws.send('hello').then(null, spy);
expect(ws.sendQueue.length).toBe(0);
$rootScope.$digest();
expect(spy).toHaveBeenCalledWith('Socket connection has been closed');
}));
});
describe('._setInternalState()', function() {
it('should change the private _internalConnectionState property', function() {
var ws = $websocket('ws://foo');
$websocketBackend.flush();
ws._setInternalState(4);
expect(ws._internalConnectionState).toBe(4);
});
it('should only allow integer values from 0-4', function() {
var ws = $websocket('ws://foo');
$websocketBackend.flush();
ws._internalConnectionState = 4;
expect(function() {
ws._setInternalState(5);
}).toThrowError('state must be an integer between 0 and 4, got: 5');
expect(ws._internalConnectionState).toBe(4);
});
it('should cancel everything inside the sendQueue if the state is 4', inject(function($q) {
var ws = $websocket('ws://foo');
$websocketBackend.flush();
var deferred = $q.defer();
var spy = spyOn(deferred, 'reject');
ws.sendQueue.push({
deferred: deferred,
});
ws._setInternalState(4);
expect(spy).toHaveBeenCalled();
}));
});
describe('.onMessage()', function() {
var fn, url, ws;
beforeEach(function() {
url = 'ws://foo';
fn = function() {};
$websocketBackend.expectConnect(url);
ws = $websocket(url);
});
afterEach(function() {
$websocketBackend.flush();
});
it('should add the callback to a queue', function() {
ws.onMessage(fn);
expect(ws.onMessageCallbacks[0].fn).toBe(fn);
});
it('should complain if not given a function', function() {
expect(function() {ws.onMessage('lol');}).toThrowError('Callback must be a function');
});
it('should accept an options argument as the second argument', function() {
ws.onMessage(fn, {filter: 'foo'});
});
it('should accept an optional RegEx pattern as the filter', function() {
ws.onMessage(fn, {filter: /baz/});
});
it('should complain if the filter option is anything but RegEx or string', function() {
expect(function() {
ws.onMessage(fn, { filter: 5 });
}).toThrowError('Pattern must be a string or regular expression');
});
it('should set the autoApply property to true if undefined in options object', function() {
ws.onMessage(angular.noop);
expect(ws.onMessageCallbacks[0].autoApply).toBe(true);
});
it('should set the autoApply property to false if specified in options object', function() {
ws.onMessage(angular.noop, {autoApply: false});
expect(ws.onMessageCallbacks[0].autoApply).toBe(false);
});
}); // end .onMessage()
describe('._onMessageHandler()', function() {
var fn, url, ws;
beforeEach(function() {
url = 'ws://foo';
fn = function() {};
$websocketBackend.expectConnect(url);
ws = $websocket(url);
});
afterEach(function() {
$websocketBackend.flush();
});
it('should call callback if message matches filter pattern', function() {
var spy = jasmine.createSpy('onResolve');
ws.onMessageCallbacks.push({fn: spy, pattern: /baz[0-9]{2}/});
ws._onMessageHandler({data: 'bar'});
expect(spy).not.toHaveBeenCalled();
ws._onMessageHandler({data: 'baz21'});
expect(spy).toHaveBeenCalled();
});
it('should only call callback if message matches filter string exactly', function() {
var spy = jasmine.createSpy('onResolve');
ws.onMessageCallbacks.push({fn: spy, pattern: 'foo'});
ws._onMessageHandler({data: 'bar'});
expect(spy).not.toHaveBeenCalled();
ws._onMessageHandler({data: 'foo'});
expect(spy).toHaveBeenCalled();
});
it('should call $rootScope.$digest() if autoApply is set to true', inject(function($rootScope) {
var digest = spyOn($rootScope, '$digest');
ws.onMessageCallbacks.push({fn: angular.noop, autoApply: true});
ws._onMessageHandler({data: 'Hello'});
expect(digest).toHaveBeenCalled();
}));
it('should not call $rootScope.$digest() if autoApply is set to false', inject(function($rootScope) {
var digest = spyOn($rootScope, '$digest');
ws.onMessageCallbacks.push({fn: angular.noop, autoApply: false});
ws._onMessageHandler({data: 'Hello'});
expect(digest).not.toHaveBeenCalled();
}));
it('should not call $rootScope.$digest() if a digest is already in progress', inject(function($rootScope){
$rootScope.$$phase = '$digest';
var digest = spyOn($rootScope, '$digest');
ws.onMessageCallbacks.push({fn: angular.noop, autoApply: true});
ws._onMessageHandler({data: 'Hello'});
expect(digest).not.toHaveBeenCalled();
}));
}); // end ._onMessageHandler()
describe('._onOpenHandler()', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
$websocketBackend.flush();
});
it('should call fireQueue to flush any queued send() calls', function() {
var spy = spyOn(ws, 'fireQueue');
ws._onOpenHandler.call(ws);
expect(spy).toHaveBeenCalled();
});
it('should call the passed-in function when a socket first connects', function() {
var spy = jasmine.createSpy('callback');
ws.onOpenCallbacks.push(spy);
ws._onOpenHandler.call(ws);
expect(spy).toHaveBeenCalled();
});
it('should call the passed-in function when a socket re-connects', function() {
var spy = jasmine.createSpy('callback');
ws.onOpenCallbacks.push(spy);
ws._onOpenHandler.call(ws);
ws._onOpenHandler.call(ws);
expect(spy.calls.count()).toBe(2);
});
it('should call multiple callbacks when connecting', function() {
var spy1 = jasmine.createSpy('callback1');
var spy2 = jasmine.createSpy('callback2');
ws.onOpenCallbacks.push(spy1);
ws.onOpenCallbacks.push(spy2);
ws._onOpenHandler.call(ws);
expect(spy1).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});
}); // end ._onOpenHandler()
describe('.fireQueue()', function() {
var ws;
beforeEach(function() {
var url = 'ws://foo/bar';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
$websocketBackend.flush();
});
it('should not affect the queue if the readyState is not 1', function() {
var data = {message: 'Hello', deferred: {resolve: angular.noop}};
ws.socket.readyState = 0;
ws.send(data);
expect(ws.sendQueue.length).toBe(1);
ws.fireQueue();
expect(ws.sendQueue.length).toBe(1);
});
it('should call send for every item in the queue if readyState is 1', function() {
var data = {message: 'Hello', deferred: {resolve: angular.noop}};
var stringified = JSON.stringify(data);
$websocketBackend.expectSend(stringified);
ws.sendQueue.unshift(data);
$websocketBackend.expectSend(stringified);
ws.sendQueue.unshift(data);
$websocketBackend.expectSend(stringified);
ws.sendQueue.unshift(data);
ws.socket.readyState = 1;
expect(ws.sendQueue.length).toBe(3);
ws.fireQueue();
expect(ws.sendQueue.length).toBe(0);
$websocketBackend.flush();
});
it('should stringify an object when sending to socket', function() {
var data = {message: 'Send me', deferred: {resolve: angular.noop}};
var stringified = JSON.stringify(data);
ws.socket.readyState = 1;
$websocketBackend.expectSend(stringified);
ws.sendQueue.unshift(data);
ws.fireQueue();
$websocketBackend.flush();
});
it('should resolve the deferred when it has been sent to the underlying socket', inject(function($q, $rootScope) {
var message = 'Send me';
var deferred = $q.defer();
var spy = jasmine.createSpy('resolve');
deferred.promise.then(spy);
var data = {deferred: deferred, message: message};
ws.socket.readyState = 1;
$websocketBackend.expectSend(message);
ws.sendQueue.unshift(data);
ws.fireQueue();
$rootScope.$digest();
$websocketBackend.flush();
expect(spy).toHaveBeenCalled();
}));
}); // end .fireQueue()
describe('.readyState', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
$websocketBackend.flush();
});
it('should provide the readyState of the underlying socket', function() {
ws.socket.readyState = 1;
expect(ws.readyState).toBe(1);
});
it('should complain if I try to set the readyState', function() {
expect(function() {
ws.readyState = 5;
}).toThrowError('The readyState property is read-only');
});
it('should return a proprietary readyState if lib is in a special state', function() {
ws.socket.readyState = 1;
ws._internalConnectionState = 5;
expect(ws.readyState).toBe(5);
});
}); // end .readyState
describe('._readyStateConstants', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
$websocketBackend.flush();
});
it('should contain the basic readyState constants for WebSocket', function() {
var constants = ws._readyStateConstants;
expect(constants.CONNECTING).toBe(0);
expect(constants.OPEN).toBe(1);
expect(constants.CLOSING).toBe(2);
expect(constants.CLOSED).toBe(3);
});
it('should provide custom constants to represent lib state', function() {
var constants = ws._readyStateConstants;
expect(constants.RECONNECT_ABORTED).toBe(4);
});
it('should ignore attempts at changing constants', function() {
ws._readyStateConstants.CONNECTING = 'foo';
expect(ws._readyStateConstants.CONNECTING).toBe(0);
});
}); // end ._readyStateConstants
describe('._reconnectableStatusCodes', function() {
it('should contain status codes that warrant re-establishing a connection', function() {
var url = 'ws://foo';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
expect(ws._reconnectableStatusCodes.length).toBe(1);
expect(ws._reconnectableStatusCodes).toEqual([4000]);
$websocketBackend.flush();
});
});
// alias
xdescribe('WebSocket', function() {
it('should alias $websocket', inject(function(WebSocket) {
expect(WebSocket).toBe($websocket);
}));
});
// alias
xdescribe('WebSocketBackend', function() {
it('should alias $websocketBackend', inject(function(WebSocketBackend) {
expect(WebSocketBackend).toBe($websocketBackend);
}));
});
describe('.onError()', function() {
it('should add the passed in function to the onErrorCallbacks array', function() {
var cb = function() {};
var url = 'ws://foo';
$websocketBackend.expectConnect(url);
var ws = $websocket(url);
ws.onError(cb);
expect(ws.onErrorCallbacks[0]).toBe(cb);
$websocketBackend.flush();
});
}); // end .onError()
describe('._onErrorHandler()', function() {
var url, ws;
beforeEach(function() {
url = 'ws://foo';
$websocketBackend.expectConnect(url);
ws = $websocket(url);
$websocketBackend.flush();
});
it('should call the passed-in function when an error occurs', function() {
var spy = jasmine.createSpy('callback');
ws.onErrorCallbacks.push(spy);
ws._onErrorHandler.call(ws, new Error());
expect(spy).toHaveBeenCalled();
});
it('should call multiple callbacks when connecting', function() {
var spy1 = jasmine.createSpy('callback1');
var spy2 = jasmine.createSpy('callback2');
ws.onErrorCallbacks.push(spy1);
ws.onErrorCallbacks.push(spy2);
ws._onErrorHandler.call(ws);
expect(spy1).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
});
}); // end ._onErrorHandler()
}); // end $websocketBackend
}); // end $websocket