| 1 |
import React from "react"; |
| 2 |
import { |
| 3 |
cleanup, |
| 4 |
fireEvent, |
| 5 |
render, |
| 6 |
screen, |
| 7 |
waitForElementToBeRemoved, |
| 8 |
} from "@testing-library/react"; |
| 9 |
import { afterEach, describe, expect, it, Mock, vi } from "vitest"; |
| 10 |
// Component |
| 11 |
import DualListLayout, { DualListProps } from "./DualListLayout"; |
| 12 |
|
| 13 |
interface MockReturn { |
| 14 |
data: { list: string[] } | { error: { message: string } }; |
| 15 |
} |
| 16 |
|
| 17 |
const mockOnOpenModal = vi.fn(); |
| 18 |
const mockOnCloseModal = vi.fn(); |
| 19 |
const mockOnAction = vi.fn(); |
| 20 |
|
| 21 |
const mockItems = ["first", "second", "third"]; |
| 22 |
|
| 23 |
/** |
| 24 |
* DualListLayout |
| 25 |
*/ |
| 26 |
const retrieveIDs: Mock<() => Promise<MockReturn>> = vi.fn(async () => { |
| 27 |
return { data: { list: mockItems } }; |
| 28 |
}); |
| 29 |
|
| 30 |
vi.mock("src/services/rpc", () => ({ |
| 31 |
useGetIDListMutation: () => [retrieveIDs], |
| 32 |
})); |
| 33 |
|
| 34 |
const initialProps: DualListProps = { |
| 35 |
entry: "", |
| 36 |
target: "user", |
| 37 |
showModal: true, |
| 38 |
onCloseModal: mockOnCloseModal, |
| 39 |
onOpenModal: mockOnOpenModal, |
| 40 |
spinning: false, |
| 41 |
title: "test", |
| 42 |
addBtnName: "Action", |
| 43 |
addSpinningBtnName: "Acting...", |
| 44 |
action: mockOnAction, |
| 45 |
tableElementsList: [], |
| 46 |
addExternalsOption: false, |
| 47 |
}; |
| 48 |
|
| 49 |
describe("DualListLayout Component", () => { |
| 50 |
afterEach(() => { |
| 51 |
vi.clearAllMocks(); |
| 52 |
cleanup(); |
| 53 |
}); |
| 54 |
|
| 55 |
it("renders the DualListLayout component", async () => { |
| 56 |
render(<DualListLayout {...initialProps} />); |
| 57 |
|
| 58 |
// Search exists |
| 59 |
const searchEntry = screen.getByRole("textbox", { |
| 60 |
name: /Search dual select list/i, |
| 61 |
}); |
| 62 |
expect(searchEntry).toBeInTheDocument(); |
| 63 |
|
| 64 |
// We can fetch new entries |
| 65 |
const clickButton = screen.getByRole("option", { |
| 66 |
name: /Click here.*/i, |
| 67 |
}); |
| 68 |
expect(clickButton).toBeInTheDocument(); |
| 69 |
|
| 70 |
// Action button exists and is disabled |
| 71 |
const actionButton = screen.getByRole("button", { |
| 72 |
name: initialProps.addBtnName, |
| 73 |
}); |
| 74 |
expect(actionButton).toBeDisabled(); |
| 75 |
|
| 76 |
// Cancel button exists |
| 77 |
const cancelButton = screen.getByRole("button", { |
| 78 |
name: "Cancel", |
| 79 |
}); |
| 80 |
expect(cancelButton).toBeEnabled(); |
| 81 |
}); |
| 82 |
|
| 83 |
it("renders the DualListLayout component without items", async () => { |
| 84 |
render(<DualListLayout {...initialProps} />); |
| 85 |
|
| 86 |
// Mock no entries |
| 87 |
retrieveIDs.mockReturnValueOnce( |
| 88 |
new Promise((resolve) => |
| 89 |
resolve({ |
| 90 |
data: { |
| 91 |
list: [], |
| 92 |
}, |
| 93 |
}) |
| 94 |
) |
| 95 |
); |
| 96 |
|
| 97 |
// Fetch no new entries |
| 98 |
const clickButton = screen.getByRole("option", { |
| 99 |
name: /Click here.*/i, |
| 100 |
}); |
| 101 |
expect(clickButton).toBeInTheDocument(); |
| 102 |
fireEvent.click(clickButton); |
| 103 |
|
| 104 |
// Await search disappearance |
| 105 |
await waitForElementToBeRemoved(() => |
| 106 |
screen.getByRole("option", { name: "Searching ..." }) |
| 107 |
); |
| 108 |
|
| 109 |
// No entries |
| 110 |
const noEntriesButton = screen.getByRole("option", { |
| 111 |
name: /No matching.*/i, |
| 112 |
}); |
| 113 |
expect(noEntriesButton).toBeInTheDocument(); |
| 114 |
}); |
| 115 |
|
| 116 |
it("renders the DualListLayout component with items", async () => { |
| 117 |
render(<DualListLayout {...initialProps} />); |
| 118 |
|
| 119 |
// Fetch new entries |
| 120 |
const clickButton = screen.getByRole("option", { |
| 121 |
name: /Click here.*/i, |
| 122 |
}); |
| 123 |
expect(clickButton).toBeInTheDocument(); |
| 124 |
fireEvent.click(clickButton); |
| 125 |
|
| 126 |
// Await search disappearance |
| 127 |
await waitForElementToBeRemoved(() => |
| 128 |
screen.getByRole("option", { name: "Searching ..." }) |
| 129 |
); |
| 130 |
|
| 131 |
// View entries |
| 132 |
for (const item of mockItems) { |
| 133 |
const itemButton = screen.getByRole("option", { |
| 134 |
name: item, |
| 135 |
}); |
| 136 |
expect(itemButton).toBeInTheDocument(); |
| 137 |
} |
| 138 |
}); |
| 139 |
|
| 140 |
it("renders the DualListLayout component using search", async () => { |
| 141 |
render(<DualListLayout {...initialProps} />); |
| 142 |
|
| 143 |
// Use search bar |
| 144 |
const searchEntry = screen.getByRole("textbox", { |
| 145 |
name: /Search dual select list/i, |
| 146 |
}); |
| 147 |
fireEvent.change(searchEntry, { target: { value: mockItems[0] } }); |
| 148 |
|
| 149 |
// Click search |
| 150 |
fireEvent.click(screen.getByRole("button", { name: /Search/i })); |
| 151 |
|
| 152 |
// Await search disappearance |
| 153 |
await waitForElementToBeRemoved(() => |
| 154 |
screen.getByRole("option", { name: "Searching ..." }) |
| 155 |
); |
| 156 |
|
| 157 |
for (const item of mockItems) { |
| 158 |
const itemButton = screen.getByRole("option", { |
| 159 |
name: item, |
| 160 |
}); |
| 161 |
expect(itemButton).toBeInTheDocument(); |
| 162 |
} |
| 163 |
}); |
| 164 |
|
| 165 |
it("renders the DualListLayout with moved items", async () => { |
| 166 |
render(<DualListLayout {...initialProps} />); |
| 167 |
|
| 168 |
// Fetch new entries |
| 169 |
const clickButton = screen.getByRole("option", { |
| 170 |
name: /Click here.*/i, |
| 171 |
}); |
| 172 |
expect(clickButton).toBeInTheDocument(); |
| 173 |
fireEvent.click(clickButton); |
| 174 |
|
| 175 |
// Await search disappearance |
| 176 |
await waitForElementToBeRemoved(() => |
| 177 |
screen.getByRole("option", { name: "Searching ..." }) |
| 178 |
); |
| 179 |
|
| 180 |
// Select entries |
| 181 |
for (const item of mockItems) { |
| 182 |
const itemButton = screen.getByRole("option", { |
| 183 |
name: item, |
| 184 |
}); |
| 185 |
expect(itemButton).toBeInTheDocument(); |
| 186 |
fireEvent.click(itemButton); |
| 187 |
} |
| 188 |
|
| 189 |
// Click move button |
| 190 |
fireEvent.click(screen.getByRole("button", { name: "Add selected" })); |
| 191 |
|
| 192 |
// Action button exists and is enabled |
| 193 |
const actionButton = screen.getByRole("button", { |
| 194 |
name: initialProps.addBtnName, |
| 195 |
}); |
| 196 |
expect(actionButton).toBeEnabled(); |
| 197 |
fireEvent.click(actionButton); |
| 198 |
|
| 199 |
expect(mockOnAction).toHaveBeenCalled(); |
| 200 |
}); |
| 201 |
|
| 202 |
it("renders the DualListLayout with externals correctly", async () => { |
| 203 |
render(<DualListLayout {...initialProps} addExternalsOption={true} />); |
| 204 |
|
| 205 |
const TEST_EXTERNAL = "test"; |
| 206 |
|
| 207 |
// Find external input |
| 208 |
const externalTextbox = screen.getByRole("textbox", { |
| 209 |
name: "dual list external", |
| 210 |
}); |
| 211 |
expect(externalTextbox).toBeEnabled(); |
| 212 |
|
| 213 |
// Add external item |
| 214 |
fireEvent.change(externalTextbox, { target: { value: TEST_EXTERNAL } }); |
| 215 |
fireEvent.click(screen.getByRole("button", { name: "Add" })); |
| 216 |
|
| 217 |
const testButton = screen.getByRole("option", { |
| 218 |
name: TEST_EXTERNAL, |
| 219 |
}); |
| 220 |
expect(testButton).toBeInTheDocument(); |
| 221 |
}); |
| 222 |
}); |