From 2d81c7b1d0b82d0097e78cd76f8b3a7ef0475b54 Mon Sep 17 00:00:00 2001 From: Mathieu Anderson Date: Fri, 16 Feb 2024 15:11:57 +0100 Subject: [PATCH] fix(coral): Fix tests Signed-off-by: Mathieu Anderson --- .../clusters/AddNewClusterForm.test.tsx | 105 ++++++++++++------ 1 file changed, 72 insertions(+), 33 deletions(-) diff --git a/coral/src/app/features/configuration/clusters/AddNewClusterForm.test.tsx b/coral/src/app/features/configuration/clusters/AddNewClusterForm.test.tsx index 7f8ccff3a3..5e66761381 100644 --- a/coral/src/app/features/configuration/clusters/AddNewClusterForm.test.tsx +++ b/coral/src/app/features/configuration/clusters/AddNewClusterForm.test.tsx @@ -26,58 +26,93 @@ describe("", () => { const originalConsoleError = console.error; beforeEach(() => { - console.error = jest.fn(); - customRender(, { queryClient: true, aquariumContext: true, }); }); - afterEach(() => { - console.error = originalConsoleError; + afterEach(() => { cleanup(); jest.clearAllMocks(); }); - it("renders all necessary elements by default", async () => { - const clusterTypeSelect = screen.getByRole("group", { - name: "Cluster type *", + describe("renders all necessary elements by default", () => { + it("renders Cluster type field", () => { + const clusterTypeSelect = screen.getByRole("group", { + name: "Cluster type *", + }); + const clusterTypeOptions = + within(clusterTypeSelect).getAllByRole("radio"); + + expect(clusterTypeSelect).toBeEnabled(); + expect(clusterTypeOptions).toHaveLength(3); }); - const clusterTypeOptions = within(clusterTypeSelect).getAllByRole("radio"); - const clusterNameInput = screen.getByRole("textbox", { - name: "Cluster name *", + + it("renders Cluster name field", () => { + const clusterNameInput = screen.getByRole("textbox", { + name: "Cluster name *", + }); + + expect(clusterNameInput).toBeEnabled(); }); - const protocolSelect = screen.getByRole("combobox", { - name: "Protocol *", + + it("renders Protocol field", () => { + const protocolSelect = screen.getByRole("combobox", { + name: "Protocol *", + }); + const protocolSelectOptions = + within(protocolSelect).getAllByRole("option"); + + expect(protocolSelect).toBeEnabled(); + expect(protocolSelectOptions).toHaveLength(7); }); - const protocolSelectOptions = within(protocolSelect).getAllByRole("option"); - const kafkaFlavorSelect = screen.getByRole("combobox", { - name: "Kafka flavor *", + + it("renders cluster type field", () => { + const clusterTypeSelect = screen.getByRole("group", { + name: "Cluster type *", + }); + const clusterTypeOptions = + within(clusterTypeSelect).getAllByRole("radio"); + + expect(clusterTypeSelect).toBeEnabled(); + expect(clusterTypeOptions).toHaveLength(3); }); - const kafkaFlavorSelectOptions = - within(kafkaFlavorSelect).getAllByRole("option"); - const bootstrapServersInput = screen.getByRole("textbox", { - name: "Bootstrap servers *", + + it("renders Kafka flavor field", () => { + const kafkaFlavorSelect = screen.getByRole("combobox", { + name: "Kafka flavor *", + }); + const kafkaFlavorSelectOptions = + within(kafkaFlavorSelect).getAllByRole("option"); + + expect(kafkaFlavorSelect).toBeEnabled(); + expect(kafkaFlavorSelectOptions).toHaveLength(6); }); - const restServersInput = screen.getByRole("textbox", { - name: "REST API servers (optional)", + + it("renders Bootstrap servers field", () => { + const bootstrapServersInput = screen.getByRole("textbox", { + name: "Bootstrap servers *", + }); + + expect(bootstrapServersInput).toBeEnabled(); }); - const submitButton = screen.getByRole("button", { - name: "Add new cluster", + it("renders REST API servers field", () => { + const restServersInput = screen.getByRole("textbox", { + name: "REST API servers (optional)", + }); + + expect(restServersInput).toBeEnabled(); }); - expect(clusterTypeSelect).toBeEnabled(); - expect(clusterTypeOptions).toHaveLength(3); - expect(clusterNameInput).toBeEnabled(); - expect(protocolSelect).toBeEnabled(); - expect(protocolSelectOptions).toHaveLength(7); - expect(kafkaFlavorSelect).toBeEnabled(); - expect(kafkaFlavorSelectOptions).toHaveLength(6); - expect(bootstrapServersInput).toBeEnabled(); - expect(restServersInput).toBeEnabled(); - expect(submitButton).toBeEnabled(); + it("renders Add new server button", () => { + const submitButton = screen.getByRole("button", { + name: "Add new cluster", + }); + + expect(submitButton).toBeEnabled(); + }); }); it("shows an error message when the cluster name is empty", async () => { @@ -250,6 +285,8 @@ describe("", () => { }); it("renders error message when submitting fails", async () => { + console.error = jest.fn(); + mockAddNewClusterRequest.mockRejectedValue(new Error("Request failed")); const clusterNameInput = screen.getByRole("textbox", { @@ -287,5 +324,7 @@ describe("", () => { position: "bottom-left", }); expect(mockedUsedNavigate).not.toHaveBeenCalled(); + + console.error = originalConsoleError; }); });