diff --git a/web/src/js/__tests__/backends/staticSpec.tsx b/web/src/js/__tests__/backends/staticSpec.tsx new file mode 100644 index 000000000..003906b00 --- /dev/null +++ b/web/src/js/__tests__/backends/staticSpec.tsx @@ -0,0 +1,17 @@ +import {enableFetchMocks} from "jest-fetch-mock"; +import {TStore} from "../ducks/tutils"; +import StaticBackend from "../../backends/static"; +import {waitFor} from "../test-utils"; + +enableFetchMocks(); + +test("static backend", async () => { + fetchMock.mockOnceIf("./flows", "[]"); + fetchMock.mockOnceIf("./options", "{}"); + const store = TStore(); + const backend = new StaticBackend(store); + await waitFor(() => expect(store.getActions()).toEqual([ + {type: "FLOWS_RECEIVE", cmd: "receive", data: [], resource: "flows"}, + {type: "OPTIONS_RECEIVE", cmd: "receive", data: {}, resource: "options"} + ])) +}); diff --git a/web/src/js/__tests__/backends/websocketSpec.tsx b/web/src/js/__tests__/backends/websocketSpec.tsx new file mode 100644 index 000000000..aedc80c63 --- /dev/null +++ b/web/src/js/__tests__/backends/websocketSpec.tsx @@ -0,0 +1,55 @@ +import {enableFetchMocks} from "jest-fetch-mock"; +import {TStore} from "../ducks/tutils"; +import WebSocketBackend from "../../backends/websocket"; +import {waitFor} from "../test-utils"; +import * as connectionActions from "../../ducks/connection"; + +enableFetchMocks(); + +test("websocket backend", async () => { + // @ts-ignore + jest.spyOn(global, 'WebSocket').mockImplementation(() => ({addEventListener: () => 0})); + + fetchMock.mockOnceIf("./flows", "[]"); + fetchMock.mockOnceIf("./events", "[]"); + fetchMock.mockOnceIf("./options", "{}"); + const store = TStore(); + const backend = new WebSocketBackend(store); + + backend.onOpen(); + + await waitFor(() => expect(store.getActions()).toEqual([ + connectionActions.startFetching(), + {type: "FLOWS_RECEIVE", cmd: "receive", data: [], resource: "flows"}, + {type: "EVENTS_RECEIVE", cmd: "receive", data: [], resource: "events"}, + {type: "OPTIONS_RECEIVE", cmd: "receive", data: {}, resource: "options"}, + connectionActions.connectionEstablished(), + ])) + + store.clearActions(); + backend.onMessage({ + "resource": "events", + "cmd": "add", + "data": {"id": "42", "message": "test", "level": "info"} + }); + expect(store.getActions()).toEqual([{ + "cmd": "add", + "data": {"id": "42", "level": "info", "message": "test"}, + "resource": "events", + "type": "EVENTS_ADD" + }]); + store.clearActions(); + + fetchMock.mockOnceIf("./events", "[]"); + backend.onMessage({ + "resource": "events", + "cmd": "reset", + }); + await waitFor(() => expect(store.getActions()).toEqual([ + {type: "EVENTS_RECEIVE", cmd: "receive", data: [], resource: "events"}, + connectionActions.connectionEstablished(), + ])) + expect(fetchMock.mock.calls).toHaveLength(4); + + jest.restoreAllMocks(); +});