mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-21 22:58:24 +00:00
web: add backend tests
This commit is contained in:
parent
03606fb0c0
commit
c5e3e3d636
17
web/src/js/__tests__/backends/staticSpec.tsx
Normal file
17
web/src/js/__tests__/backends/staticSpec.tsx
Normal 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"}
|
||||
]))
|
||||
});
|
55
web/src/js/__tests__/backends/websocketSpec.tsx
Normal file
55
web/src/js/__tests__/backends/websocketSpec.tsx
Normal 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();
|
||||
});
|
Loading…
Reference in New Issue
Block a user