| 1 |
import React, { act } from "react"; |
| 2 |
import "@testing-library/jest-dom"; |
| 3 |
import { render, screen, fireEvent } from "@testing-library/react"; |
| 4 |
import { Provider, useSelector } from "react-redux"; |
| 5 |
import { createStore } from "redux"; |
| 6 |
import { HashRouter, useSearchParams } from "react-router-dom"; |
| 7 |
// eslint-disable-next-line |
| 8 |
import regeneratorRuntime from "regenerator-runtime"; |
| 9 |
|
| 10 |
import { initialState, reducers } from "../../Store"; |
| 11 |
import Groups from "./Groups"; |
| 12 |
|
| 13 |
jest.mock("react-redux", () => ({ |
| 14 |
...jest.requireActual("react-redux"), |
| 15 |
useSelector: jest.fn(), |
| 16 |
})); |
| 17 |
|
| 18 |
jest.mock("react-router-dom", () => ({ |
| 19 |
...jest.requireActual("react-router-dom"), |
| 20 |
useSearchParams: jest.fn(), |
| 21 |
})); |
| 22 |
|
| 23 |
var mockAsync = () => |
| 24 |
jest.fn().mockImplementation(() => Promise.resolve({ key: "value" })); |
| 25 |
|
| 26 |
var groupsJsx = (callbackSpy) => ( |
| 27 |
<Provider store={createStore(mockReducers, mockAppState())}> |
| 28 |
<HashRouter> |
| 29 |
<Groups updateGroups={callbackSpy} /> |
| 30 |
</HashRouter> |
| 31 |
</Provider> |
| 32 |
); |
| 33 |
|
| 34 |
var mockReducers = jest.fn((state, action) => { |
| 35 |
if (action.type === "GROUPS_PAGE" && !action.value.data) { |
| 36 |
// no-op from mock, don't update state |
| 37 |
return state; |
| 38 |
} |
| 39 |
state = reducers(state, action); |
| 40 |
// mocked useSelector seems to cause a problem |
| 41 |
// this should get the right state back? |
| 42 |
// not sure |
| 43 |
// useSelector.mockImplementation((callback) => callback(state); |
| 44 |
return state; |
| 45 |
}); |
| 46 |
|
| 47 |
var mockAppState = () => |
| 48 |
Object.assign({}, initialState, { |
| 49 |
groups_data: [ |
| 50 |
{ kind: "group", name: "testgroup", users: [] }, |
| 51 |
{ kind: "group", name: "testgroup2", users: ["foo", "bar"] }, |
| 52 |
], |
| 53 |
groups_page: { |
| 54 |
offset: 0, |
| 55 |
limit: 2, |
| 56 |
total: 4, |
| 57 |
}, |
| 58 |
}); |
| 59 |
|
| 60 |
beforeEach(() => { |
| 61 |
useSelector.mockImplementation((callback) => { |
| 62 |
return callback(mockAppState()); |
| 63 |
}); |
| 64 |
useSearchParams.mockImplementation(() => { |
| 65 |
return [new URLSearchParams(), jest.fn()]; |
| 66 |
}); |
| 67 |
}); |
| 68 |
|
| 69 |
afterEach(() => { |
| 70 |
useSelector.mockClear(); |
| 71 |
mockReducers.mockClear(); |
| 72 |
useSearchParams.mockClear(); |
| 73 |
jest.runAllTimers(); |
| 74 |
}); |
| 75 |
|
| 76 |
test("Renders", async () => { |
| 77 |
let callbackSpy = mockAsync(); |
| 78 |
|
| 79 |
await act(async () => { |
| 80 |
render(groupsJsx(callbackSpy)); |
| 81 |
}); |
| 82 |
|
| 83 |
expect(screen.getByTestId("container")).toBeVisible(); |
| 84 |
}); |
| 85 |
|
| 86 |
test("Renders groups_data prop into links", async () => { |
| 87 |
let callbackSpy = mockAsync(); |
| 88 |
|
| 89 |
await act(async () => { |
| 90 |
render(groupsJsx(callbackSpy)); |
| 91 |
}); |
| 92 |
|
| 93 |
let testgroup = screen.getByText("testgroup"); |
| 94 |
let testgroup2 = screen.getByText("testgroup2"); |
| 95 |
|
| 96 |
expect(testgroup).toBeVisible(); |
| 97 |
expect(testgroup2).toBeVisible(); |
| 98 |
}); |
| 99 |
|
| 100 |
test("Renders nothing if required data is not available", async () => { |
| 101 |
useSelector.mockImplementation((callback) => { |
| 102 |
return callback({}); |
| 103 |
}); |
| 104 |
|
| 105 |
let callbackSpy = mockAsync(); |
| 106 |
|
| 107 |
await act(async () => { |
| 108 |
render(groupsJsx(callbackSpy)); |
| 109 |
}); |
| 110 |
|
| 111 |
let noShow = screen.getByTestId("no-show"); |
| 112 |
expect(noShow).toBeVisible(); |
| 113 |
}); |
| 114 |
|
| 115 |
test("Interacting with PaginationFooter causes page refresh", async () => { |
| 116 |
let updateGroupsSpy = mockAsync(); |
| 117 |
let setSearchParamsSpy = mockAsync(); |
| 118 |
let searchParams = new URLSearchParams({ limit: "2" }); |
| 119 |
useSearchParams.mockImplementation(() => [ |
| 120 |
searchParams, |
| 121 |
(callback) => { |
| 122 |
searchParams = callback(searchParams); |
| 123 |
setSearchParamsSpy(searchParams.toString()); |
| 124 |
}, |
| 125 |
]); |
| 126 |
let _, setSearchParams; |
| 127 |
await act(async () => { |
| 128 |
render(groupsJsx(updateGroupsSpy)); |
| 129 |
[_, setSearchParams] = useSearchParams(); |
| 130 |
}); |
| 131 |
|
| 132 |
expect(updateGroupsSpy).toBeCalledWith(0, 2); |
| 133 |
|
| 134 |
var lastState = |
| 135 |
mockReducers.mock.results[mockReducers.mock.results.length - 1].value; |
| 136 |
expect(lastState.groups_page.offset).toEqual(0); |
| 137 |
expect(lastState.groups_page.limit).toEqual(2); |
| 138 |
|
| 139 |
let next = screen.getByTestId("paginate-next"); |
| 140 |
await act(async () => { |
| 141 |
await fireEvent.click(next); |
| 142 |
}); |
| 143 |
expect(updateGroupsSpy).toBeCalledWith(2, 2); |
| 144 |
// mocked updateGroups means callback after load doesn't fire |
| 145 |
// expect(setSearchParamsSpy).toBeCalledWith("limit=2&offset=2"); |
| 146 |
}); |