mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-16 22:43:00 +00:00
refactor mock dashboard in tests
reusable mock function with overrides for props (mostly API methods), rather than duplicate invocations makes updating to v6 way easier
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import React from "react";
|
||||
import { withProps } from "recompose";
|
||||
import "@testing-library/jest-dom";
|
||||
import { act } from "react-dom/test-utils";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
@@ -28,24 +29,25 @@ jest.mock("react-router-dom", () => ({
|
||||
useSearchParams: jest.fn(),
|
||||
}));
|
||||
|
||||
var serverDashboardJsx = (spy) => (
|
||||
<Provider store={createStore(mockReducers, mockAppState())}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={spy}
|
||||
stopServer={spy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>
|
||||
);
|
||||
const serverDashboardJsx = (props) => {
|
||||
// create mock ServerDashboard
|
||||
// spies is a dict of properties to mock in
|
||||
// any API calls that will fire during the test should be mocked
|
||||
props = props || {};
|
||||
const defaultSpy = mockAsync();
|
||||
if (!props.updateUsers) {
|
||||
props.updateUsers = defaultSpy;
|
||||
}
|
||||
return (
|
||||
<Provider store={createStore(mockReducers, mockAppState())}>
|
||||
<HashRouter>
|
||||
<Routes>
|
||||
<Route path="/" element={withProps(props)(ServerDashboard)()} />
|
||||
</Routes>
|
||||
</HashRouter>
|
||||
</Provider>
|
||||
);
|
||||
};
|
||||
|
||||
var mockAsync = (data) =>
|
||||
jest.fn().mockImplementation(() => Promise.resolve(data ? data : { k: "v" }));
|
||||
@@ -166,20 +168,16 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
test("Renders", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
expect(screen.getByTestId("container")).toBeVisible();
|
||||
});
|
||||
|
||||
test("Renders users from props.user_data into table", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let foo = screen.getByTestId("user-name-div-foo");
|
||||
@@ -192,10 +190,8 @@ test("Renders users from props.user_data into table", async () => {
|
||||
});
|
||||
|
||||
test("Renders correctly the status of a single-user server", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let start_elems = screen.getAllByText("Start Server");
|
||||
@@ -209,10 +205,8 @@ test("Renders correctly the status of a single-user server", async () => {
|
||||
});
|
||||
|
||||
test("Renders spawn page link", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
for (let server in bar_servers) {
|
||||
@@ -227,7 +221,7 @@ test("Invokes the startServer event on button click", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx({ startServer: callbackSpy }));
|
||||
});
|
||||
|
||||
let start_elems = screen.getAllByText("Start Server");
|
||||
@@ -244,7 +238,7 @@ test("Invokes the stopServer event on button click", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx({ stopServer: callbackSpy }));
|
||||
});
|
||||
|
||||
let stop = screen.getByText("Stop Server");
|
||||
@@ -260,7 +254,7 @@ test("Invokes the shutdownHub event on button click", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx({ shutdownHub: callbackSpy }));
|
||||
});
|
||||
|
||||
let shutdown = screen.getByText("Shutdown Hub");
|
||||
@@ -273,10 +267,8 @@ test("Invokes the shutdownHub event on button click", async () => {
|
||||
});
|
||||
|
||||
test("Sorts according to username", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let handler = screen.getByTestId("user-sort");
|
||||
@@ -292,10 +284,8 @@ test("Sorts according to username", async () => {
|
||||
});
|
||||
|
||||
test("Sorts according to admin", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let handler = screen.getByTestId("admin-sort");
|
||||
@@ -311,10 +301,8 @@ test("Sorts according to admin", async () => {
|
||||
});
|
||||
|
||||
test("Sorts according to last activity", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let handler = screen.getByTestId("last-activity-sort");
|
||||
@@ -330,10 +318,8 @@ test("Sorts according to last activity", async () => {
|
||||
});
|
||||
|
||||
test("Sorts according to server status (running/not running)", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let handler = screen.getByTestId("running-status-sort");
|
||||
@@ -349,10 +335,8 @@ test("Sorts according to server status (running/not running)", async () => {
|
||||
});
|
||||
|
||||
test("Shows server details with button click", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
let button = screen.getByTestId("foo-collapse-button");
|
||||
let collapse = screen.getByTestId("foo-collapse");
|
||||
@@ -394,10 +378,8 @@ test("Renders nothing if required data is not available", async () => {
|
||||
return callback({});
|
||||
});
|
||||
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
let noShow = screen.getByTestId("no-show");
|
||||
@@ -406,28 +388,8 @@ test("Renders nothing if required data is not available", async () => {
|
||||
});
|
||||
|
||||
test("Shows a UI error dialogue when start all servers fails", async () => {
|
||||
let spy = mockAsync();
|
||||
let rejectSpy = mockAsyncRejection;
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(() => {}, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={spy}
|
||||
stopServer={spy}
|
||||
startAll={rejectSpy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ startAll: mockAsyncRejection }));
|
||||
});
|
||||
|
||||
let startAll = screen.getByTestId("start-all");
|
||||
@@ -442,28 +404,8 @@ test("Shows a UI error dialogue when start all servers fails", async () => {
|
||||
});
|
||||
|
||||
test("Shows a UI error dialogue when stop all servers fails", async () => {
|
||||
let spy = mockAsync();
|
||||
let rejectSpy = mockAsyncRejection;
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(() => {}, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={spy}
|
||||
stopServer={spy}
|
||||
startAll={spy}
|
||||
stopAll={rejectSpy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ stopAll: mockAsyncRejection }));
|
||||
});
|
||||
|
||||
let stopAll = screen.getByTestId("stop-all");
|
||||
@@ -478,28 +420,8 @@ test("Shows a UI error dialogue when stop all servers fails", async () => {
|
||||
});
|
||||
|
||||
test("Shows a UI error dialogue when start user server fails", async () => {
|
||||
let spy = mockAsync();
|
||||
let rejectSpy = mockAsyncRejection();
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(() => {}, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={rejectSpy}
|
||||
stopServer={spy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ startServer: mockAsyncRejection() }));
|
||||
});
|
||||
|
||||
let start_elems = screen.getAllByText("Start Server");
|
||||
@@ -515,28 +437,9 @@ test("Shows a UI error dialogue when start user server fails", async () => {
|
||||
});
|
||||
|
||||
test("Shows a UI error dialogue when start user server returns an improper status code", async () => {
|
||||
let spy = mockAsync();
|
||||
let rejectSpy = mockAsync({ status: 403 });
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(() => {}, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={rejectSpy}
|
||||
stopServer={spy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ startServer: rejectSpy }));
|
||||
});
|
||||
|
||||
let start_elems = screen.getAllByText("Start Server");
|
||||
@@ -556,24 +459,7 @@ test("Shows a UI error dialogue when stop user servers fails", async () => {
|
||||
let rejectSpy = mockAsyncRejection();
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(() => {}, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={spy}
|
||||
stopServer={rejectSpy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ stopServer: rejectSpy }));
|
||||
});
|
||||
|
||||
let stop = screen.getByText("Stop Server");
|
||||
@@ -592,24 +478,7 @@ test("Shows a UI error dialogue when stop user server returns an improper status
|
||||
let rejectSpy = mockAsync({ status: 403 });
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(() => {}, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={spy}
|
||||
shutdownHub={spy}
|
||||
startServer={spy}
|
||||
stopServer={rejectSpy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ stopServer: rejectSpy }));
|
||||
});
|
||||
|
||||
let stop = screen.getByText("Stop Server");
|
||||
@@ -640,24 +509,7 @@ test("Search for user calls updateUsers with name filter", async () => {
|
||||
});
|
||||
});
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(mockReducers, mockAppState())}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={mockUpdateUsers}
|
||||
shutdownHub={spy}
|
||||
startServer={spy}
|
||||
stopServer={spy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
);
|
||||
render(serverDashboardJsx({ updateUsers: mockUpdateUsers }));
|
||||
});
|
||||
|
||||
let search = screen.getByLabelText("user-search");
|
||||
@@ -683,13 +535,13 @@ test("Search for user calls updateUsers with name filter", async () => {
|
||||
});
|
||||
|
||||
test("Interacting with PaginationFooter causes state update and refresh via useEffect call", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
let updateUsers = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx({ updateUsers: updateUsers }));
|
||||
});
|
||||
|
||||
expect(callbackSpy).toBeCalledWith(0, 100, "");
|
||||
expect(updateUsers).toBeCalledWith(0, 100, "");
|
||||
|
||||
var n = 3;
|
||||
expect(searchParams.get("offset")).toEqual(null);
|
||||
@@ -712,10 +564,8 @@ test("Interacting with PaginationFooter causes state update and refresh via useE
|
||||
});
|
||||
|
||||
test("Server delete button exists for named servers", async () => {
|
||||
let callbackSpy = mockAsync();
|
||||
|
||||
await act(async () => {
|
||||
render(serverDashboardJsx(callbackSpy));
|
||||
render(serverDashboardJsx());
|
||||
});
|
||||
|
||||
for (let server in bar_servers) {
|
||||
@@ -729,8 +579,6 @@ test("Server delete button exists for named servers", async () => {
|
||||
});
|
||||
|
||||
test("Start server and confirm pending state", async () => {
|
||||
let spy = mockAsync();
|
||||
|
||||
let mockStartServer = jest.fn(() => {
|
||||
return new Promise(async (resolve) =>
|
||||
setTimeout(() => {
|
||||
@@ -743,22 +591,10 @@ test("Start server and confirm pending state", async () => {
|
||||
|
||||
await act(async () => {
|
||||
render(
|
||||
<Provider store={createStore(mockReducers, {})}>
|
||||
<HashRouter>
|
||||
<CompatRouter>
|
||||
<Switch>
|
||||
<ServerDashboard
|
||||
updateUsers={mockUpdateUsers}
|
||||
shutdownHub={spy}
|
||||
startServer={mockStartServer}
|
||||
stopServer={spy}
|
||||
startAll={spy}
|
||||
stopAll={spy}
|
||||
/>
|
||||
</Switch>
|
||||
</CompatRouter>
|
||||
</HashRouter>
|
||||
</Provider>,
|
||||
serverDashboardJsx({
|
||||
updateUsers: mockUpdateUsers,
|
||||
startServer: mockStartServer,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user