2018-11-02 18:02:53 +00:00
|
|
|
/* global _, $, Whisper */
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('NetworkStatusView', () => {
|
|
|
|
describe('getNetworkStatus', () => {
|
|
|
|
let networkStatusView;
|
|
|
|
let socketStatus = WebSocket.OPEN;
|
|
|
|
|
|
|
|
let oldGetSocketStatus;
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
/* BEGIN stubbing globals */
|
2018-11-02 18:02:53 +00:00
|
|
|
before(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
oldGetSocketStatus = window.getSocketStatus;
|
2018-11-02 18:02:53 +00:00
|
|
|
window.getSocketStatus = () => socketStatus;
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
after(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
window.getSocketStatus = oldGetSocketStatus;
|
2018-04-23 22:36:47 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
// It turns out that continued calls to window.getSocketStatus happen
|
|
|
|
// because we host NetworkStatusView in three mock interfaces, and the view
|
|
|
|
// checks every N seconds. That results in infinite errors unless there is
|
|
|
|
// something to call.
|
2018-11-02 18:02:53 +00:00
|
|
|
window.getSocketStatus = () => WebSocket.OPEN;
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
|
|
|
/* END stubbing globals */
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
beforeEach(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView = new Whisper.NetworkStatusView();
|
|
|
|
$('.network-status-container').append(networkStatusView.el);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
afterEach(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
// prevents huge number of errors on console after running tests
|
|
|
|
clearInterval(networkStatusView.renderIntervalHandle);
|
|
|
|
networkStatusView = null;
|
|
|
|
});
|
2017-05-18 17:42:20 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('initialization', () => {
|
|
|
|
it('should have an empty interval', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
assert.equal(
|
|
|
|
networkStatusView.socketReconnectWaitDuration.asSeconds(),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('network status with no connection', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
networkStatusView.navigatorOnLine = () => false;
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should be interrupted', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView.update();
|
2018-11-02 18:02:53 +00:00
|
|
|
const status = networkStatusView.getNetworkStatus();
|
2018-04-27 21:25:04 +00:00
|
|
|
assert(status.hasInterruption);
|
|
|
|
assert.equal(status.instructions, 'Check your network connection.');
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should display an offline message', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView.update();
|
|
|
|
assert.match(networkStatusView.$el.text(), /Offline/);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should override socket status', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
_([
|
|
|
|
WebSocket.CONNECTING,
|
|
|
|
WebSocket.OPEN,
|
|
|
|
WebSocket.CLOSING,
|
|
|
|
WebSocket.CLOSED,
|
2018-11-02 18:02:53 +00:00
|
|
|
]).forEach(socketStatusVal => {
|
2018-04-27 21:25:04 +00:00
|
|
|
socketStatus = socketStatusVal;
|
|
|
|
networkStatusView.update();
|
|
|
|
assert.match(networkStatusView.$el.text(), /Offline/);
|
2017-04-13 18:59:33 +00:00
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should override registration status', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.Registration.remove();
|
|
|
|
networkStatusView.update();
|
|
|
|
assert.match(networkStatusView.$el.text(), /Offline/);
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('network status when registration is not done', () => {
|
|
|
|
beforeEach(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.Registration.remove();
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should display an unlinked message', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView.update();
|
|
|
|
assert.match(networkStatusView.$el.text(), /Relink/);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should override socket status', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
_([
|
|
|
|
WebSocket.CONNECTING,
|
|
|
|
WebSocket.OPEN,
|
|
|
|
WebSocket.CLOSING,
|
|
|
|
WebSocket.CLOSED,
|
2018-11-02 18:02:53 +00:00
|
|
|
]).forEach(socketStatusVal => {
|
2018-04-27 21:25:04 +00:00
|
|
|
socketStatus = socketStatusVal;
|
|
|
|
networkStatusView.update();
|
|
|
|
assert.match(networkStatusView.$el.text(), /Relink/);
|
2017-01-04 03:37:56 +00:00
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('network status when registration is done', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
networkStatusView.navigatorOnLine = () => true;
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.Registration.markDone();
|
|
|
|
networkStatusView.update();
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should not display an unlinked message', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView.update();
|
|
|
|
assert.notMatch(networkStatusView.$el.text(), /Relink/);
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('network status when socket is connecting', () => {
|
|
|
|
beforeEach(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.Registration.markDone();
|
|
|
|
socketStatus = WebSocket.CONNECTING;
|
|
|
|
networkStatusView.update();
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('it should display a connecting string if connecting and not in the connecting grace period', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView.withinConnectingGracePeriod = false;
|
2018-11-02 18:02:53 +00:00
|
|
|
networkStatusView.getNetworkStatus();
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
assert.match(networkStatusView.$el.text(), /Connecting/);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('it should not be interrupted if in connecting grace period', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
assert(networkStatusView.withinConnectingGracePeriod);
|
2018-11-02 18:02:53 +00:00
|
|
|
const status = networkStatusView.getNetworkStatus();
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
assert.match(networkStatusView.$el.text(), /Connecting/);
|
|
|
|
assert(!status.hasInterruption);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('it should be interrupted if connecting grace period is over', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
networkStatusView.withinConnectingGracePeriod = false;
|
2018-11-02 18:02:53 +00:00
|
|
|
const status = networkStatusView.getNetworkStatus();
|
2017-01-04 03:37:56 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
assert(status.hasInterruption);
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('network status when socket is open', () => {
|
|
|
|
before(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
socketStatus = WebSocket.OPEN;
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should not be interrupted', () => {
|
|
|
|
const status = networkStatusView.getNetworkStatus();
|
2018-04-27 21:25:04 +00:00
|
|
|
assert(!status.hasInterruption);
|
|
|
|
assert.match(
|
|
|
|
networkStatusView.$el
|
|
|
|
.find('.network-status-message')
|
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
/^$/
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('network status when socket is closed or closing', () => {
|
|
|
|
_([WebSocket.CLOSED, WebSocket.CLOSING]).forEach(socketStatusVal => {
|
|
|
|
it('should be interrupted', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
socketStatus = socketStatusVal;
|
|
|
|
networkStatusView.update();
|
2018-11-02 18:02:53 +00:00
|
|
|
const status = networkStatusView.getNetworkStatus();
|
2018-04-27 21:25:04 +00:00
|
|
|
assert(status.hasInterruption);
|
2017-01-04 03:37:56 +00:00
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('the socket reconnect interval', () => {
|
|
|
|
beforeEach(() => {
|
2018-04-27 21:25:04 +00:00
|
|
|
socketStatus = WebSocket.CLOSED;
|
|
|
|
networkStatusView.setSocketReconnectInterval(61000);
|
|
|
|
networkStatusView.update();
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should format the message based on the socketReconnectWaitDuration property', () => {
|
2018-04-27 21:25:04 +00:00
|
|
|
assert.equal(
|
|
|
|
networkStatusView.socketReconnectWaitDuration.asSeconds(),
|
|
|
|
61
|
|
|
|
);
|
|
|
|
assert.match(
|
|
|
|
networkStatusView.$('.network-status-message:last').text(),
|
|
|
|
/Attempting reconnect/
|
|
|
|
);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('should be reset by changing the socketStatus to CONNECTING', () => {});
|
2017-01-04 03:37:56 +00:00
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2017-01-04 03:37:56 +00:00
|
|
|
});
|