File tree Expand file tree Collapse file tree 2 files changed +46
-2
lines changed
src/internal/observable/dom Expand file tree Collapse file tree 2 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -636,6 +636,48 @@ describe('webSocket', () => {
636
636
] ) ;
637
637
} ) ;
638
638
} ) ;
639
+
640
+ describe ( 'node constructor' , ( ) => {
641
+
642
+ it ( 'should send and receive messages' , ( ) => {
643
+ let messageReceived = false ;
644
+ const subject = webSocket < string > ( < any > {
645
+ url : 'ws://mysocket' ,
646
+ WebSocketCtor : ( url : string , protocol : string ) : MockWebSocket => {
647
+ return new MockWebSocket ( url , protocol ) ;
648
+ }
649
+ } ) ;
650
+
651
+ subject . next ( 'ping' ) ;
652
+
653
+ subject . subscribe ( x => {
654
+ expect ( x ) . to . equal ( 'pong' ) ;
655
+ messageReceived = true ;
656
+ } ) ;
657
+
658
+ const socket = MockWebSocket . lastSocket ;
659
+ expect ( socket . url ) . to . equal ( 'ws://mysocket' ) ;
660
+
661
+ socket . open ( ) ;
662
+ expect ( socket . lastMessageSent ) . to . equal ( JSON . stringify ( 'ping' ) ) ;
663
+
664
+ socket . triggerMessage ( JSON . stringify ( 'pong' ) ) ;
665
+ expect ( messageReceived ) . to . be . true ;
666
+
667
+ subject . unsubscribe ( ) ;
668
+ } ) ;
669
+
670
+ it ( 'should handle constructor errors if no WebSocketCtor' , ( ) => {
671
+
672
+ expect ( ( ) => {
673
+ const subject = webSocket < string > ( < any > {
674
+ url : 'ws://mysocket'
675
+ } ) ;
676
+ } ) . to . throw ( 'no WebSocket constructor can be found' ) ;
677
+
678
+ } ) ;
679
+ } ) ;
680
+
639
681
} ) ;
640
682
641
683
class MockWebSocket {
Original file line number Diff line number Diff line change @@ -80,7 +80,6 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
80
80
this . source = urlConfigOrSource as Observable < T > ;
81
81
} else {
82
82
const config = this . _config = { ...DEFAULT_WEBSOCKET_CONFIG } ;
83
- config . WebSocketCtor = WebSocket ;
84
83
this . _output = new Subject < T > ( ) ;
85
84
if ( typeof urlConfigOrSource === 'string' ) {
86
85
config . url = urlConfigOrSource ;
@@ -91,7 +90,10 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
91
90
}
92
91
}
93
92
}
94
- if ( ! config . WebSocketCtor ) {
93
+
94
+ if ( ! config . WebSocketCtor && WebSocket ) {
95
+ config . WebSocketCtor = WebSocket ;
96
+ } else if ( ! config . WebSocketCtor ) {
95
97
throw new Error ( 'no WebSocket constructor can be found' ) ;
96
98
}
97
99
this . destination = new ReplaySubject ( ) ;
You can’t perform that action at this time.
0 commit comments