| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('NetworkStatusView', function() { | 
					
						
							|  |  |  |     describe('getNetworkStatus', function() { | 
					
						
							|  |  |  |         var networkStatusView; | 
					
						
							|  |  |  |         var socketStatus = WebSocket.OPEN; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var oldGetSocketStatus; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* BEGIN stubbing globals */ | 
					
						
							|  |  |  |         before(function() { | 
					
						
							|  |  |  |             oldGetSocketStatus = window.getSocketStatus; | 
					
						
							|  |  |  |             window.getSocketStatus = function() { return socketStatus; }; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         after(function() { | 
					
						
							|  |  |  |             window.getSocketStatus = oldGetSocketStatus; | 
					
						
							| 
									
										
										
										
											2018-04-20 12:03:54 -07:00
										 |  |  |             window.getSocketStatus = function() { return WebSocket.OPEN; }; | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |         }); | 
					
						
							|  |  |  |         /* END stubbing globals */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 10:42:20 -07:00
										 |  |  |         beforeEach(function() { | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             networkStatusView = new Whisper.NetworkStatusView(); | 
					
						
							|  |  |  |             $('.network-status-container').append(networkStatusView.el); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-05-18 10:42:20 -07:00
										 |  |  |         afterEach(function() { | 
					
						
							|  |  |  |             // prevents huge number of errors on console after running tests
 | 
					
						
							|  |  |  |             clearInterval(networkStatusView.renderIntervalHandle); | 
					
						
							|  |  |  |             networkStatusView = null; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |         describe('initialization', function() { | 
					
						
							|  |  |  |             it('should have an empty interval', function() { | 
					
						
							|  |  |  |                 assert.equal(networkStatusView.socketReconnectWaitDuration.asSeconds(), 0); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         describe('network status with no connection', function() { | 
					
						
							|  |  |  |             beforeEach(function() { | 
					
						
							|  |  |  |                 networkStatusView.navigatorOnLine = function() { return false; }; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             it('should be interrupted', function() { | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |                 var status = networkStatusView.getNetworkStatus(); | 
					
						
							|  |  |  |                 assert(status.hasInterruption); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.equal(status.instructions, "Check your network connection."); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('should display an offline message', function() { | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.match(networkStatusView.$el.text(), /Offline/); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('should override socket status', function() { | 
					
						
							|  |  |  |                 _([WebSocket.CONNECTING, | 
					
						
							|  |  |  |                    WebSocket.OPEN, | 
					
						
							|  |  |  |                    WebSocket.CLOSING, | 
					
						
							|  |  |  |                    WebSocket.CLOSED]).map(function(socketStatusVal) { | 
					
						
							|  |  |  |                     socketStatus = socketStatusVal; | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                     networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                     assert.match(networkStatusView.$el.text(), /Offline/); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |             it('should override registration status', function() { | 
					
						
							|  |  |  |                 Whisper.Registration.remove(); | 
					
						
							|  |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.match(networkStatusView.$el.text(), /Offline/); | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         describe('network status when registration is not done', function() { | 
					
						
							|  |  |  |             beforeEach(function() { | 
					
						
							|  |  |  |                 Whisper.Registration.remove(); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             it('should display an unlinked message', function() { | 
					
						
							|  |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.match(networkStatusView.$el.text(), /Relink/); | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('should override socket status', function() { | 
					
						
							|  |  |  |                 _([WebSocket.CONNECTING, | 
					
						
							|  |  |  |                    WebSocket.OPEN, | 
					
						
							|  |  |  |                    WebSocket.CLOSING, | 
					
						
							|  |  |  |                    WebSocket.CLOSED]).map(function(socketStatusVal) { | 
					
						
							|  |  |  |                     socketStatus = socketStatusVal; | 
					
						
							|  |  |  |                     networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                     assert.match(networkStatusView.$el.text(), /Relink/); | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         describe('network status when registration is done', function() { | 
					
						
							|  |  |  |             beforeEach(function() { | 
					
						
							|  |  |  |                 networkStatusView.navigatorOnLine = function() { return true; }; | 
					
						
							|  |  |  |                 Whisper.Registration.markDone(); | 
					
						
							|  |  |  |                 networkStatusView.update(); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             it('should not display an unlinked message', function() { | 
					
						
							|  |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.notMatch(networkStatusView.$el.text(), /Relink/); | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |         }); | 
					
						
							|  |  |  |         describe('network status when socket is connecting', function() { | 
					
						
							|  |  |  |             beforeEach(function() { | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                 Whisper.Registration.markDone(); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |                 socketStatus = WebSocket.CONNECTING; | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('it should display a connecting string if connecting and not in the connecting grace period', function() { | 
					
						
							|  |  |  |                 networkStatusView.withinConnectingGracePeriod = false; | 
					
						
							|  |  |  |                 var status = networkStatusView.getNetworkStatus(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.match(networkStatusView.$el.text(), /Connecting/); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('it should not be interrupted if in connecting grace period', function() { | 
					
						
							|  |  |  |                 assert(networkStatusView.withinConnectingGracePeriod); | 
					
						
							|  |  |  |                 var status = networkStatusView.getNetworkStatus(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.match(networkStatusView.$el.text(), /Connecting/); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |                 assert(!status.hasInterruption); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             it('it should be interrupted if connecting grace period is over', function() { | 
					
						
							|  |  |  |                 networkStatusView.withinConnectingGracePeriod = false; | 
					
						
							|  |  |  |                 var status = networkStatusView.getNetworkStatus(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 assert(status.hasInterruption); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         describe('network status when socket is open', function() { | 
					
						
							|  |  |  |             before(function() { | 
					
						
							|  |  |  |                 socketStatus = WebSocket.OPEN; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             it('should not be interrupted', function() { | 
					
						
							|  |  |  |                 var status = networkStatusView.getNetworkStatus(); | 
					
						
							|  |  |  |                 assert(!status.hasInterruption); | 
					
						
							|  |  |  |                 assert.match(networkStatusView.$el.find('.network-status-message').text().trim(), /^$/); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         describe('network status when socket is closed or closing', function() { | 
					
						
							|  |  |  |             _([WebSocket.CLOSED, WebSocket.CLOSING]).map(function(socketStatusVal) { | 
					
						
							|  |  |  |                 it('should be interrupted', function() { | 
					
						
							|  |  |  |                     socketStatus = socketStatusVal; | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                     networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |                     var status = networkStatusView.getNetworkStatus(); | 
					
						
							|  |  |  |                     assert(status.hasInterruption); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         describe('the socket reconnect interval', function() { | 
					
						
							|  |  |  |             beforeEach(function() { | 
					
						
							|  |  |  |                 socketStatus = WebSocket.CLOSED; | 
					
						
							|  |  |  |                 networkStatusView.setSocketReconnectInterval(61000); | 
					
						
							| 
									
										
										
										
											2017-04-13 11:59:33 -07:00
										 |  |  |                 networkStatusView.update(); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('should format the message based on the socketReconnectWaitDuration property', function() { | 
					
						
							|  |  |  |                 assert.equal(networkStatusView.socketReconnectWaitDuration.asSeconds(), 61); | 
					
						
							| 
									
										
										
										
											2017-05-16 09:05:35 -07:00
										 |  |  |                 assert.match(networkStatusView.$('.network-status-message:last').text(), /Attempting reconnect/); | 
					
						
							| 
									
										
										
										
											2017-01-03 21:37:56 -06:00
										 |  |  |             }); | 
					
						
							|  |  |  |             it('should be reset by changing the socketStatus to CONNECTING', function() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | }); |