You've already forked homehub
Release 1.4.0
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import * as WebSocketLib from 'src/lib/websocket';
|
||||
|
||||
describe('src/lib/websocket', () => {
|
||||
describe('HomeHubWebSocket', () => {
|
||||
describe('BaseConnector', () => {
|
||||
const settings = {
|
||||
url: '/websocket',
|
||||
spam: true,
|
||||
};
|
||||
|
||||
it('includes the subscribable mixin', () => {
|
||||
// Given
|
||||
const webSocket = new WebSocketLib.HomeHubWebSocket(false, settings);
|
||||
const webSocket = new WebSocketLib.BaseConnector(false, settings);
|
||||
|
||||
// Then
|
||||
expect(webSocket.__mixins__).toContain('SubscribableMixin');
|
||||
@@ -16,7 +16,7 @@ describe('src/lib/websocket', () => {
|
||||
|
||||
it('includes the event source mixin', () => {
|
||||
// Given
|
||||
const webSocket = new WebSocketLib.HomeHubWebSocket(false, settings);
|
||||
const webSocket = new WebSocketLib.BaseConnector(false, settings);
|
||||
|
||||
// Then
|
||||
expect(webSocket.__mixins__).toContain('EventSourceMixin');
|
||||
@@ -25,14 +25,11 @@ describe('src/lib/websocket', () => {
|
||||
describe('constructor', () => {
|
||||
it('initializes the instance', () => {
|
||||
// Given
|
||||
const webSocket = new WebSocketLib.HomeHubWebSocket(false, settings);
|
||||
const webSocket = new WebSocketLib.BaseConnector(false, settings);
|
||||
|
||||
// Then
|
||||
expect(webSocket.debug).toBe(false);
|
||||
expect(webSocket.settings).toEqual(settings);
|
||||
expect(webSocket.socket).toBe(null);
|
||||
expect(webSocket.reconnectTimeout).toBe(null);
|
||||
expect(webSocket.reconnectCounter).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -63,6 +60,24 @@ describe('src/lib/websocket', () => {
|
||||
expect(console.log).not.toHaveBeenCalled(); // eslint-disable-line no-console
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('HomeHubWebSocket', () => {
|
||||
const settings = {
|
||||
url: '/websocket',
|
||||
};
|
||||
|
||||
describe('constructor', () => {
|
||||
it('initializes the instance', () => {
|
||||
// Given
|
||||
const webSocket = new WebSocketLib.HomeHubWebSocket(false, settings);
|
||||
|
||||
// Then
|
||||
expect(webSocket.socket).toBe(null);
|
||||
expect(webSocket.reconnectTimeout).toBe(null);
|
||||
expect(webSocket.reconnectCounter).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stopReconnect', () => {
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -94,6 +94,9 @@ describe('src/providers/DashboardsProvider', () => {
|
||||
expect(component.state('isWebSocketConnected')).toEqual(
|
||||
DEFAULT_DASHBOARDS_CONTEXT.isWebSocketConnected
|
||||
);
|
||||
expect(component.state('loadError')).toBe(
|
||||
DEFAULT_DASHBOARDS_CONTEXT.loadError
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -906,6 +909,7 @@ describe('src/providers/DashboardsProvider', () => {
|
||||
</DashboardsProvider>
|
||||
);
|
||||
spyOn(component.instance(), 'handleLoadedState').and.resolveTo([]);
|
||||
spyOn(component.instance(), 'throwLoadError');
|
||||
|
||||
// When
|
||||
await component.instance().loadDashboards();
|
||||
@@ -914,8 +918,11 @@ describe('src/providers/DashboardsProvider', () => {
|
||||
expect(component.state('dashboards')).toEqual([]);
|
||||
expect(component.state('currentDashboardId')).toBe(null);
|
||||
expect(component.state('lastSaveTimestamp')).toBe(null);
|
||||
expect(component.state('lastSaveError')).toEqual('FIAL');
|
||||
expect(component.state('loadError')).toEqual('FIAL');
|
||||
expect(component.instance().handleLoadedState).not.toHaveBeenCalled();
|
||||
expect(component.instance().throwLoadError).toHaveBeenCalledWith(
|
||||
component.state('loadError'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1018,6 +1025,46 @@ describe('src/providers/DashboardsProvider', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('throwLoadError', () => {
|
||||
it('throws the error as is if it is an Error', () => {
|
||||
// Given
|
||||
const error = new Error('FIAL');
|
||||
|
||||
const component = shallow(
|
||||
<DashboardsProvider
|
||||
loader={<Loader />}
|
||||
loadDashboards={loadDashboards}
|
||||
saveDashboards={saveDashboards}
|
||||
settings={settings}
|
||||
>
|
||||
<Children />
|
||||
</DashboardsProvider>
|
||||
);
|
||||
|
||||
// When
|
||||
expect(() => component.instance().throwLoadError(error)).toThrow(error);
|
||||
});
|
||||
|
||||
it('wraps a string in an error and throws it', () => {
|
||||
// Given
|
||||
const component = shallow(
|
||||
<DashboardsProvider
|
||||
loader={<Loader />}
|
||||
loadDashboards={loadDashboards}
|
||||
saveDashboards={saveDashboards}
|
||||
settings={settings}
|
||||
>
|
||||
<Children />
|
||||
</DashboardsProvider>
|
||||
);
|
||||
|
||||
// When
|
||||
expect(() => component.instance().throwLoadError('FIAL')).toThrowError(
|
||||
Error, 'FIAL',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('componentDidMount', () => {
|
||||
let fakeHomeHubWebSocket = null;
|
||||
let mockWebSocketUnsubscriber = null;
|
||||
@@ -1094,6 +1141,48 @@ describe('src/providers/DashboardsProvider', () => {
|
||||
expect(fakeHomeHubWebSocket.start).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('configures and starts the websocket connection using custom connector', () => {
|
||||
// Given
|
||||
settings.CONNECTOR = jasmine.createSpy().and.returnValue(
|
||||
fakeHomeHubWebSocket
|
||||
);
|
||||
|
||||
const component = shallow(
|
||||
<DashboardsProvider
|
||||
loader={<Loader />}
|
||||
loadDashboards={loadDashboards}
|
||||
saveDashboards={saveDashboards}
|
||||
settings={settings}
|
||||
>
|
||||
<Children />
|
||||
</DashboardsProvider>
|
||||
);
|
||||
spyOn(component.instance(), 'loadDashboards');
|
||||
|
||||
// When
|
||||
component.instance().componentDidMount();
|
||||
|
||||
// Then
|
||||
expect(component.instance().webSocket).toEqual(fakeHomeHubWebSocket);
|
||||
expect(WebSocketLib.HomeHubWebSocket).not.toHaveBeenCalled();
|
||||
expect(settings.CONNECTOR).toHaveBeenCalledWith(
|
||||
settings.DEBUG, settings.WEBSOCKET
|
||||
);
|
||||
expect(fakeHomeHubWebSocket.addEventListener).toHaveBeenCalledWith(
|
||||
'start', component.instance().onWebSocketStart
|
||||
);
|
||||
expect(fakeHomeHubWebSocket.addEventListener).toHaveBeenCalledWith(
|
||||
'stop', component.instance().onWebSocketStop
|
||||
);
|
||||
expect(component.instance().webSocketUnsubscriber).toEqual(
|
||||
mockWebSocketUnsubscriber
|
||||
);
|
||||
expect(fakeHomeHubWebSocket.subscribe).toHaveBeenCalledWith(
|
||||
component.instance().onWebSocketMessage
|
||||
);
|
||||
expect(fakeHomeHubWebSocket.start).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not start the websocket in offline mode', () => {
|
||||
// Given
|
||||
settings.OFFLINE_MODE = true;
|
||||
@@ -1235,6 +1324,7 @@ describe('src/providers/DashboardsProvider', () => {
|
||||
setCurrentDashboardId: component.instance().setCurrentDashboardId,
|
||||
dashboardsHash: component.instance().dashboardsHash,
|
||||
addDashboard: component.instance().addDashboard,
|
||||
loadError: component.state('loadError'),
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user