| 1 |
import React, { act } from "react"; |
| 2 |
import "@testing-library/jest-dom"; |
| 3 |
import { render, screen, fireEvent } from "@testing-library/react"; |
| 4 |
import userEvent from "@testing-library/user-event"; |
| 5 |
import { Provider, useDispatch, useSelector } from "react-redux"; |
| 6 |
import { createStore } from "redux"; |
| 7 |
import { HashRouter } from "react-router-dom"; |
| 8 |
// eslint-disable-next-line |
| 9 |
import regeneratorRuntime from "regenerator-runtime"; |
| 10 |
|
| 11 |
import AddUser from "./AddUser"; |
| 12 |
|
| 13 |
jest.mock("react-redux", () => ({ |
| 14 |
...jest.requireActual("react-redux"), |
| 15 |
useDispatch: jest.fn(), |
| 16 |
useSelector: jest.fn(), |
| 17 |
})); |
| 18 |
|
| 19 |
var mockAsync = (result) => |
| 20 |
jest.fn().mockImplementation(() => Promise.resolve(result)); |
| 21 |
|
| 22 |
var mockAsyncRejection = () => |
| 23 |
jest.fn().mockImplementation(() => Promise.reject()); |
| 24 |
|
| 25 |
var addUserJsx = (spy, spy2, spy3) => ( |
| 26 |
<Provider store={createStore(() => {}, {})}> |
| 27 |
<HashRouter> |
| 28 |
<AddUser addUsers={spy} updateUsers={spy3 || spy2 || spy} /> |
| 29 |
</HashRouter> |
| 30 |
</Provider> |
| 31 |
); |
| 32 |
|
| 33 |
var mockAppState = () => ({ |
| 34 |
limit: 3, |
| 35 |
}); |
| 36 |
|
| 37 |
beforeEach(() => { |
| 38 |
useDispatch.mockImplementation(() => { |
| 39 |
return () => {}; |
| 40 |
}); |
| 41 |
useSelector.mockImplementation((callback) => { |
| 42 |
return callback(mockAppState()); |
| 43 |
}); |
| 44 |
}); |
| 45 |
|
| 46 |
afterEach(() => { |
| 47 |
useDispatch.mockClear(); |
| 48 |
jest.runAllTimers(); |
| 49 |
}); |
| 50 |
|
| 51 |
test("Renders", async () => { |
| 52 |
await act(async () => { |
| 53 |
render(addUserJsx()); |
| 54 |
}); |
| 55 |
expect(screen.getByTestId("container")).toBeVisible(); |
| 56 |
}); |
| 57 |
|
| 58 |
test("Removes users when they fail Regex", async () => { |
| 59 |
let callbackSpy = mockAsync(); |
| 60 |
|
| 61 |
await act(async () => { |
| 62 |
render(addUserJsx(callbackSpy)); |
| 63 |
}); |
| 64 |
|
| 65 |
let textarea = screen.getByTestId("user-textarea"); |
| 66 |
let submit = screen.getByTestId("submit"); |
| 67 |
|
| 68 |
fireEvent.blur(textarea, { target: { value: "foo \n bar\[email protected]\n \n\n" } }); |
| 69 |
await act(async () => { |
| 70 |
await fireEvent.click(submit); |
| 71 |
}); |
| 72 |
|
| 73 |
expect(callbackSpy).toHaveBeenCalledWith(["foo", "bar", "[email protected]"], false); |
| 74 |
}); |
| 75 |
|
| 76 |
test("Correctly submits admin", async () => { |
| 77 |
let callbackSpy = mockAsync(); |
| 78 |
|
| 79 |
await act(async () => { |
| 80 |
render(addUserJsx(callbackSpy)); |
| 81 |
}); |
| 82 |
let textarea = screen.getByTestId("user-textarea"); |
| 83 |
let submit = screen.getByTestId("submit"); |
| 84 |
let check = screen.getByTestId("check"); |
| 85 |
|
| 86 |
await fireEvent.blur(textarea, { target: { value: "foo" } }); |
| 87 |
await fireEvent.click(check); |
| 88 |
await fireEvent.click(submit); |
| 89 |
await act(async () => { |
| 90 |
await jest.runAllTimers(); |
| 91 |
}); |
| 92 |
|
| 93 |
expect(callbackSpy).toHaveBeenCalledWith(["foo"], true); |
| 94 |
}); |
| 95 |
|
| 96 |
test("Shows a UI error dialogue when user creation fails", async () => { |
| 97 |
let callbackSpy = mockAsyncRejection(); |
| 98 |
|
| 99 |
await act(async () => { |
| 100 |
render(addUserJsx(callbackSpy)); |
| 101 |
}); |
| 102 |
|
| 103 |
let submit = screen.getByTestId("submit"); |
| 104 |
|
| 105 |
await act(async () => { |
| 106 |
await fireEvent.click(submit); |
| 107 |
}); |
| 108 |
|
| 109 |
let errorDialog = screen.getByText("Failed to create user."); |
| 110 |
|
| 111 |
expect(errorDialog).toBeVisible(); |
| 112 |
expect(callbackSpy).toHaveBeenCalled(); |
| 113 |
}); |
| 114 |
|
| 115 |
test("Shows a more specific UI error dialogue when user creation returns an improper status code", async () => { |
| 116 |
let callbackSpy = mockAsync({ status: 409 }); |
| 117 |
|
| 118 |
await act(async () => { |
| 119 |
render(addUserJsx(callbackSpy)); |
| 120 |
}); |
| 121 |
|
| 122 |
let submit = screen.getByTestId("submit"); |
| 123 |
|
| 124 |
await act(async () => { |
| 125 |
await fireEvent.click(submit); |
| 126 |
}); |
| 127 |
|
| 128 |
let errorDialog = screen.getByText( |
| 129 |
"Failed to create user. User already exists.", |
| 130 |
); |
| 131 |
|
| 132 |
expect(errorDialog).toBeVisible(); |
| 133 |
expect(callbackSpy).toHaveBeenCalled(); |
| 134 |
}); |