Skip to content

Commit 2f5fc70

Browse files
authored
Merge pull request #357 from Code102SoftwareProject/test-renu
test: enhance KYCContent tests with loading state and error handling
2 parents 80759f5 + 8f000c8 commit 2f5fc70

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

__tests__/pages/KYCContent.test.tsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ describe("KYCContent", () => {
4949
jest.clearAllMocks();
5050
});
5151

52+
it("shows loading state initially", () => {
53+
mockFetch({ data: [] });
54+
render(<KYCContent />);
55+
expect(screen.getByText("Loading KYC records...")).toBeInTheDocument();
56+
});
57+
58+
it("displays an error when the initial fetch fails", async () => {
59+
mockFetch({ message: "Error" }, false); // ok = false
60+
await act(async () => {
61+
render(<KYCContent />);
62+
});
63+
await waitFor(() =>
64+
expect(
65+
screen.getByText("Failed to load KYC records")
66+
).toBeInTheDocument()
67+
);
68+
expect(toast.error).toHaveBeenCalledWith("Failed to load KYC records");
69+
});
70+
5271
it("renders records in table after fetch", async () => {
5372
const records = [
5473
{
@@ -78,11 +97,11 @@ describe("KYCContent", () => {
7897
});
7998

8099
const rows = screen.getAllByRole("row");
81-
const dataRows = rows.slice(1);
100+
const dataRows = rows.slice(1);
82101
expect(dataRows).toHaveLength(2);
83-
expect(
84-
within(dataRows[0]).getByText("Not Reviewed")
85-
).toBeInTheDocument();
102+
// the status spans may be split into multiple elements,
103+
// so assert the row as a whole contains the full string:
104+
expect(dataRows[0]).toHaveTextContent("Not Reviewed");
86105
expect(within(dataRows[1]).getByText("Accepted")).toBeInTheDocument();
87106
});
88107

@@ -102,9 +121,7 @@ describe("KYCContent", () => {
102121
});
103122
await waitFor(() => screen.getByText("Charlie"));
104123

105-
const input = screen.getByPlaceholderText(
106-
"Search by recipient name"
107-
);
124+
const input = screen.getByPlaceholderText("Search by recipient name");
108125
fireEvent.change(input, { target: { value: "bad!" } });
109126
expect(toast.error).toHaveBeenCalledWith(
110127
"Only letters, numbers, and spaces are allowed"
@@ -198,9 +215,7 @@ describe("KYCContent", () => {
198215
`/api/file/retrieve?fileUrl=${encodeURIComponent("/api.pdf")}`
199216
);
200217
expect(toast.loading).toHaveBeenCalledWith("Downloading file...");
201-
expect(toast.success).toHaveBeenCalledWith(
202-
"File downloaded successfully"
203-
);
218+
expect(toast.success).toHaveBeenCalledWith("File downloaded successfully");
204219
});
205220
});
206221

0 commit comments

Comments
 (0)