Add front end tests for user search

This commit is contained in:
Narek Amirbekian
2022-03-14 17:54:51 -07:00
parent 1b3b005ca4
commit 3a59a15164
6 changed files with 130 additions and 13 deletions

View File

@@ -9,6 +9,9 @@ import { createStore } from "redux";
import regeneratorRuntime from "regenerator-runtime";
import ServerDashboard from "./ServerDashboard";
import * as sinon from "sinon";
let clock;
jest.mock("react-redux", () => ({
...jest.requireActual("react-redux"),
@@ -45,6 +48,7 @@ var mockAppState = () => ({
});
beforeEach(() => {
clock = sinon.useFakeTimers();
useSelector.mockImplementation((callback) => {
return callback(mockAppState());
});
@@ -52,6 +56,7 @@ beforeEach(() => {
afterEach(() => {
useSelector.mockClear();
clock.restore();
});
test("Renders", async () => {
@@ -435,3 +440,42 @@ test("Shows a UI error dialogue when stop user server returns an improper status
expect(errorDialog).toBeVisible();
});
test("Search for user calls updateUsers with name filter", async () => {
let spy = mockAsync();
let mockUpdateUsers = jest.fn((offset, limit, name_filter) => {
return Promise.resolve([]);
});
await act(async () => {
render(
<Provider store={createStore(() => {}, {})}>
<HashRouter>
<Switch>
<ServerDashboard
updateUsers={mockUpdateUsers}
shutdownHub={spy}
startServer={spy}
stopServer={spy}
startAll={spy}
stopAll={spy}
/>
</Switch>
</HashRouter>
</Provider>
);
});
let search = screen.getByLabelText("user-search");
fireEvent.change(search, { target: { value: "a" } });
clock.tick(400);
expect(mockUpdateUsers.mock.calls).toHaveLength(2);
expect(mockUpdateUsers.mock.calls[1][3]).toEqual("a");
expect(search.value).toEqual("a");
fireEvent.change(search, { target: { value: "ab" } });
clock.tick(400);
expect(mockUpdateUsers.mock.calls).toHaveLength(3);
expect(mockUpdateUsers.mock.calls[1][3]).toEqual("ab");
expect(search.value).toEqual("ab");
});