web: add backend tests

This commit is contained in:
Maximilian Hils 2021-08-23 09:35:47 +02:00
parent 03606fb0c0
commit c5e3e3d636
2 changed files with 72 additions and 0 deletions

View File

@ -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"}
]))
});

View File

@ -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();
});