From 1fd6a76d74515b0a8d4bfc5c9fa22dbfb9d31697 Mon Sep 17 00:00:00 2001 From: Rafa Castelblanque <67455978+castelblanque@users.noreply.github.com> Date: Mon, 17 Oct 2022 13:17:56 +0200 Subject: [PATCH] [Dashboard] Fix for error flashing when deleting repo (#5461) * Fix for error flashing when deleting repo Signed-off-by: Rafa Castelblanque * Remove wrong check in test Signed-off-by: Rafa Castelblanque Signed-off-by: Rafa Castelblanque --- .../PkgRepoList/PkgRepoControl.test.tsx | 38 +++++++++++++++++++ .../Config/PkgRepoList/PkgRepoControl.tsx | 6 ++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.test.tsx b/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.test.tsx index 13a20cb2c8b..69f6fac006a 100644 --- a/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.test.tsx +++ b/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.test.tsx @@ -39,6 +39,10 @@ const defaultProps = { it("deletes the repo and refreshes list", async () => { const deleteRepo = jest.fn(); + // Mock the response for delete repo action + const mockDispatch = jest.fn(); + mockDispatch.mockReturnValue(true); + spyOnUseDispatch = jest.spyOn(ReactRedux, "useDispatch").mockReturnValue(mockDispatch); const refetchRepos = jest.fn(); actions.repos = { ...actions.repos, @@ -67,6 +71,40 @@ it("deletes the repo and refreshes list", async () => { expect(refetchRepos).toHaveBeenCalled(); }); +it("show error message when package repository deletion fails ", async () => { + const deleteRepo = jest.fn(); + // Mock the response for delete repo action + const mockDispatch = jest.fn(); + mockDispatch.mockReturnValue(false); + spyOnUseDispatch = jest.spyOn(ReactRedux, "useDispatch").mockReturnValue(mockDispatch); + const refetchRepos = jest.fn(); + actions.repos = { + ...actions.repos, + deleteRepo, + }; + const wrapper = mountWrapper( + defaultStore, + , + ); + const deleteButton = wrapper.find(CdsButton).filterWhere(b => b.text() === "Delete"); + act(() => { + (deleteButton.prop("onClick") as any)(); + }); + wrapper.update(); + expect(wrapper.find(ConfirmDialog)).toIncludeText( + "Are you sure you want to delete the repository", + ); + const confirmButton = wrapper + .find(ConfirmDialog) + .find(".btn") + .filterWhere(b => b.text() === "Delete"); + await act(async () => { + await (confirmButton.prop("onClick") as any)(); + }); + expect(deleteRepo).toHaveBeenCalled(); + expect(refetchRepos).not.toHaveBeenCalled(); +}); + it("renders the button to edit the repo", () => { const wrapper = mountWrapper(defaultStore, ); expect(wrapper.find(PkgRepoAddButton).prop("text")).toBe("Edit"); diff --git a/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.tsx b/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.tsx index 45b3a0715a2..53659bc6582 100644 --- a/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.tsx +++ b/dashboard/src/components/Config/PkgRepoList/PkgRepoControl.tsx @@ -36,8 +36,10 @@ export function PkgRepoControl({ const handleDeleteClick = (packageRepoRef: PackageRepositoryReference) => { return async () => { - await dispatch(actions.repos.deleteRepo(packageRepoRef)); - refetchRepos(); + const deleted = await dispatch(actions.repos.deleteRepo(packageRepoRef)); + if (deleted) { + refetchRepos(); + } closeModal(); }; };