From a2bce22bdc459bf51ffbce124dac3e1715bee74a Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Wed, 7 Jul 2021 12:55:36 +0200 Subject: [PATCH] Convert MapperDialog test to use React Testing Library --- src/client-scopes/add/MapperDialog.test.tsx | 75 ++++++++++++++++ src/client-scopes/add/MapperDialog.tsx | 3 +- .../add/__tests__/MapperDialog.test.tsx | 90 ------------------- 3 files changed, 77 insertions(+), 91 deletions(-) create mode 100644 src/client-scopes/add/MapperDialog.test.tsx delete mode 100644 src/client-scopes/add/__tests__/MapperDialog.test.tsx diff --git a/src/client-scopes/add/MapperDialog.test.tsx b/src/client-scopes/add/MapperDialog.test.tsx new file mode 100644 index 0000000000..31ea66d1d2 --- /dev/null +++ b/src/client-scopes/add/MapperDialog.test.tsx @@ -0,0 +1,75 @@ +import { Button } from "@patternfly/react-core"; +import { fireEvent, render, screen } from "@testing-library/react"; +import type { ServerInfoRepresentation } from "keycloak-admin/lib/defs/serverInfoRepesentation"; +import React, { useState } from "react"; +import { ServerInfoContext } from "../../context/server-info/ServerInfoProvider"; +import serverInfo from "../../context/server-info/__tests__/mock.json"; +import { AddMapperDialog, AddMapperDialogModalProps } from "./MapperDialog"; + +describe("MapperDialog", () => { + const Test = (args: AddMapperDialogModalProps) => { + const [open, setOpen] = useState(false); + + return ( + + setOpen(!open)} + /> + + + ); + }; + + it("disables the add button when nothing is selected", () => { + render( {}} />); + + fireEvent.click(screen.getByText("Show")); + + expect(screen.getByTestId("modalConfirm")).toHaveClass("pf-m-disabled"); + }); + + it("returns array with selected build in protocol mapping", () => { + const onConfirm = jest.fn(); + const protocol = "openid-connect"; + + render(); + + fireEvent.click(screen.getByText("Show")); + fireEvent.click(screen.getByLabelText("Select row 0")); + fireEvent.click(screen.getByLabelText("Select row 1")); + fireEvent.click(screen.getByTestId("modalConfirm")); + + expect(onConfirm).toBeCalledWith([ + serverInfo.builtinProtocolMappers[protocol][0], + serverInfo.builtinProtocolMappers[protocol][1], + ]); + }); + + it("returns selected protocol mapping type on click", () => { + const onConfirm = jest.fn(); + const protocol = "openid-connect"; + + render(); + + fireEvent.click(screen.getByText("Show")); + fireEvent.click(screen.getByLabelText("User Realm Role")); + + expect(onConfirm).toBeCalledWith( + serverInfo.protocolMapperTypes[protocol][0] + ); + }); + + it("closes the dialog on 'X' click", () => { + render( {}} />); + + fireEvent.click(screen.getByText("Show")); + expect(screen.getByText("Hide")).toBeInTheDocument(); + + fireEvent.click(screen.getByLabelText("Close")); + expect(screen.getByText("Show")).toBeInTheDocument(); + }); +}); diff --git a/src/client-scopes/add/MapperDialog.tsx b/src/client-scopes/add/MapperDialog.tsx index f7df694467..f3fdb8264e 100644 --- a/src/client-scopes/add/MapperDialog.tsx +++ b/src/client-scopes/add/MapperDialog.tsx @@ -81,6 +81,7 @@ export const AddMapperDialog = (props: AddMapperDialogProps) => { ? [ - - ); - }; - - it("should have disabled add button when nothing is selected", () => { - const container = mount( - {}} /> - ); - - container.find("button#open").simulate("click"); - - const button = container.find("button#modal-confirm"); - expect(button.hasClass("pf-m-disabled")).toBe(true); - }); - - it("should return array with selected build in protocol mapping", () => { - const onConfirm = jest.fn(); - const protocol = "openid-connect"; - const container = mount( - - ); - - container.find("button#open").simulate("click"); - container - .find("input[name='checkrow0']") - .simulate("change", { target: { value: true } }); - container - .find("input[name='checkrow1']") - .simulate("change", { target: { value: true } }); - - container.find("button#modal-confirm").simulate("click"); - expect(onConfirm).toBeCalledWith([ - serverInfo.builtinProtocolMappers[protocol][0], - serverInfo.builtinProtocolMappers[protocol][1], - ]); - }); - - it("should return selected protocol mapping type on click", () => { - const onConfirm = jest.fn(); - const protocol = "openid-connect"; - const container = mount(); - - container.find("button#open").simulate("click"); - - container - .find("div.pf-c-data-list__item-content") - .first() - .simulate("click"); - expect(onConfirm).toBeCalledWith( - serverInfo.protocolMapperTypes[protocol][0] - ); - }); - - it("should close the dialog on 'X' click", () => { - const container = mount( - {}} /> - ); - - expect(container.find("button#open").text()).toBe("Show"); - container.find("button#open").simulate("click"); - expect(container.find("button#open").text()).toBe("Hide"); - container.find('button[aria-label="Close"]').simulate("click"); - - expect(container.find("button#open").text()).toBe("Show"); - }); -});