From 54c2e07c1fa5f03ce7a223fd606c5936a4001d15 Mon Sep 17 00:00:00 2001 From: ghdtjgus76 Date: Sun, 26 May 2024 03:03:19 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=ED=86=A0=EA=B8=80=20=EC=BB=B4=ED=8F=AC?= =?UTF-8?q?=EB=84=8C=ED=8A=B8=20=EB=B3=80=EA=B2=BD=EB=90=9C=20props?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/Switch/Switch.test.tsx | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/packages/wow-ui/src/components/Switch/Switch.test.tsx b/packages/wow-ui/src/components/Switch/Switch.test.tsx index 9dc82018..35fdffca 100644 --- a/packages/wow-ui/src/components/Switch/Switch.test.tsx +++ b/packages/wow-ui/src/components/Switch/Switch.test.tsx @@ -124,3 +124,50 @@ describe("disabled", () => { }); }); }); + +describe("external control and events", () => { + let rendered: RenderResult; + + it("should fire external onClick event", async () => { + rendered = render(); + const switchComponent = rendered.getByRole("checkbox"); + const onClickHandler = jest.fn(); + switchComponent.onclick = onClickHandler; + + fireEvent.click(switchComponent); + + await waitFor(() => { + expect(onClickHandler).toHaveBeenCalled(); + }); + }); + + it("should fire external onKeyDown event", async () => { + rendered = render(); + const switchComponent = rendered.getByRole("checkbox"); + const onKeyDownHandler = jest.fn(); + switchComponent.onkeydown = onKeyDownHandler; + + fireEvent.type(switchComponent, "{enter}"); + + await waitFor(() => { + expect(onKeyDownHandler).toHaveBeenCalled(); + }); + }); + + it("should toggle external checked state when onClick event fired", async () => { + let isChecked = false; + const handleChange = () => { + isChecked = !isChecked; + }; + const rendered = render(); + const switchComponent = rendered.getByRole("checkbox"); + switchComponent.onchange = handleChange; + + fireEvent.click(switchComponent); + + await waitFor(() => { + expect(switchComponent).toHaveAttribute("aria-checked", "true"); + expect(switchComponent).toHaveAttribute("aria-disabled", "false"); + }); + }); +});