diff --git a/packages/core/src/polymorphic/polymorphic.test.tsx b/packages/core/src/polymorphic/polymorphic.test.tsx index f5bdc133..56dd9892 100644 --- a/packages/core/src/polymorphic/polymorphic.test.tsx +++ b/packages/core/src/polymorphic/polymorphic.test.tsx @@ -6,459 +6,36 @@ * https://github.com/radix-ui/primitives/blob/b14ac1fff0cdaf45d1ea3e65c28c320ac0f743f2/packages/react/slot/src/Slot.test.tsx */ -import { fireEvent, render } from "@solidjs/testing-library"; -import { ComponentProps, JSX, splitProps } from "solid-js"; -import { vi } from "vitest"; +import { render } from "@solidjs/testing-library"; +import { expect } from "vitest"; -import { As, AsChildProp, Polymorphic } from "./polymorphic"; +import { Polymorphic } from "./polymorphic"; -type ButtonExampleProps = ComponentProps<"button"> & { - leftIcon?: JSX.Element; - rightIcon?: JSX.Element; -}; +describe("Polymorphic", () => { + it("should render the 'as' string prop", () => { + const { getByTestId } = render(() => ( + + Button + + )); -function ButtonExample(props: ButtonExampleProps & AsChildProp) { - const [local, others] = splitProps(props, [ - "leftIcon", - "rightIcon", - "children", - ]); + const polymorphic = getByTestId("polymorphic"); - return ( - - {local.leftIcon} - {local.children} - {local.rightIcon} - - ); -} - -// Skipped: error with vitest implementation -describe.skip("Polymorphic", () => { - describe("render", () => { - it("should render the fallback if no 'asChild' prop", () => { - const { getByTestId } = render(() => ( - - Button - - )); - - const polymorphic = getByTestId("polymorphic"); - - expect(polymorphic).toBeInstanceOf(HTMLButtonElement); - }); - - it("should render the component from 'As' when 'asChild' prop is true and the only direct child is 'As'", () => { - const { getByTestId } = render(() => ( - - Link - - )); - - const polymorphic = getByTestId("polymorphic"); - - expect(polymorphic).toBeInstanceOf(HTMLAnchorElement); - }); - - it("should render the component from 'As' when 'asChild' prop is true and one of the direct children is 'As'", () => { - const { getByTestId } = render(() => ( - - before - Link - after - - )); - - const polymorphic = getByTestId("polymorphic"); - - expect(polymorphic).toBeInstanceOf(HTMLAnchorElement); - }); - }); - - describe("style", () => { - it("should apply Polymorphic string 'style' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply Polymorphic string 'style' on child when child's 'style' is undefined", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply child's string 'style' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply child's string 'style' on child when Polymorphic 'style' is undefined", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply both Polymorphic and child's string 'style' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle( - "background-color:red;color:white", - ); - }); - - it("support overriding same style attribute by child when using string 'sytle'", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:blue"); - }); - - it("should apply Polymorphic object 'style' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply Polymorphic object 'style' on child when child's style is undefined", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply child's object 'style' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply child's object 'style' on child when Polymorphic 'style' is undefined", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:red"); - }); - - it("should apply both Polymorphic and child's object 'style' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle( - "background-color:red;color:white", - ); - }); - - it("support overriding same style attribute by child when using object 'sytle'", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle("background-color:blue"); - }); - - it("support mixing object and string 'style'", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveStyle( - "background-color:blue;padding:14px;font-size:18px", - ); - }); + expect(polymorphic).toBeInstanceOf(HTMLDivElement); }); - describe("class", () => { - it("should apply Polymorphic 'class' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveClass("foo"); - }); - - it("should apply Polymorphic 'class' on child when child's 'class' is undefined", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveClass("foo"); - }); - - it("should apply child's 'class' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveClass("foo"); - }); - - it("should apply child's 'class' on child when Polymorphic 'class' is undefined", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - expect(getByRole("button")).toHaveClass("foo"); - }); - - it("should apply both Polymorphic and child's 'class' on child", () => { - const { getByRole } = render(() => ( - - - Click me - - - )); - - const button = getByRole("button"); - - expect(button).toHaveClass("foo"); - expect(button).toHaveClass("bar"); - }); - }); - - describe("handlers", () => { - it("should call the 'onClick' passed to the Polymorphic", () => { - const onPolymorphicClickSpy = vi.fn(); - - const { getByRole } = render(() => ( - - - Click me - - - )); - - fireEvent.click(getByRole("button")); - - expect(onPolymorphicClickSpy).toBeCalledTimes(1); - }); - - it("should call the child's 'onClick'", () => { - const onChildClickSpy = vi.fn(); - - const { getByRole } = render(() => ( - - - Click me - - - )); - - fireEvent.click(getByRole("button")); - - expect(onChildClickSpy).toBeCalledTimes(1); - }); - - it("should call both the Polymorphic and child's 'onClick' when provided", () => { - const onPolymorphicClickSpy = vi.fn(); - const onChildClickSpy = vi.fn(); - - const { getByRole } = render(() => ( - - - Click me - - - )); - - fireEvent.click(getByRole("button")); - - expect(onChildClickSpy).toBeCalledTimes(1); - expect(onPolymorphicClickSpy).toBeCalledTimes(1); - }); - - it("should call the Polymorphic 'onClick' even if child's 'onClick' is undefined", () => { - const onPolymorphicClickSpy = vi.fn(); - - const { getByRole } = render(() => ( - - - Click me - - - )); - - fireEvent.click(getByRole("button")); - - expect(onPolymorphicClickSpy).toBeCalledTimes(1); - }); - - it("should call the child's 'onClick' even if Polymorphic 'onClick' is undefined", () => { - const onChildClickSpy = vi.fn(); - - const { getByRole } = render(() => ( - - - Click me - - - )); - - fireEvent.click(getByRole("button")); - - expect(onChildClickSpy).toBeCalledTimes(1); - }); - }); - - describe("With slottable content", () => { - it("should render a button with icon on the left/right when no 'asChild' prop", () => { - const { getByRole } = render(() => ( - left} - rightIcon={right} - > - Button text - - )); - - const button = getByRole("button"); - - expect(button).toBeInstanceOf(HTMLButtonElement); - expect(button).toContainHTML( - "leftButton textright", - ); - }); + it("should render the 'as' custom component prop", () => { + const CustomButton = (props: any) =>