|
| 1 | +import { |
| 2 | + type ElementType, |
| 3 | + type FC, |
| 4 | + type MouseEventHandler, |
| 5 | + type PropsWithChildren, |
| 6 | +} from "react"; |
| 7 | + |
| 8 | +import { type ClickableAriaRole, useClickable } from "./useClickable"; |
| 9 | +import { render, screen } from "@testing-library/react"; |
| 10 | +import userEvent from "@testing-library/user-event"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Since the point of the hook is to take a traditionally non-interactive HTML |
| 14 | + * element and make it interactive, it made the most sense to test against an |
| 15 | + * element directly. |
| 16 | + */ |
| 17 | +type NonNativeButtonProps<TElement extends HTMLElement = HTMLElement> = |
| 18 | + Readonly< |
| 19 | + PropsWithChildren<{ |
| 20 | + as?: Exclude<ElementType, "button">; |
| 21 | + role?: ClickableAriaRole; |
| 22 | + onInteraction: MouseEventHandler<TElement>; |
| 23 | + }> |
| 24 | + >; |
| 25 | + |
| 26 | +const NonNativeButton: FC<NonNativeButtonProps<HTMLElement>> = ({ |
| 27 | + as, |
| 28 | + onInteraction, |
| 29 | + children, |
| 30 | + role = "button", |
| 31 | +}) => { |
| 32 | + const clickableProps = useClickable(onInteraction, role); |
| 33 | + const Component = as ?? "div"; |
| 34 | + return <Component {...clickableProps}>{children}</Component>; |
| 35 | +}; |
| 36 | + |
| 37 | +describe(useClickable.name, () => { |
| 38 | + it("Always defaults to role 'button'", () => { |
| 39 | + render(<NonNativeButton onInteraction={jest.fn()} />); |
| 40 | + expect(() => screen.getByRole("button")).not.toThrow(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("Overrides the native role of any element that receives the hook result (be very careful with this behavior)", () => { |
| 44 | + const anchorText = "I'm a button that's secretly a link!"; |
| 45 | + render( |
| 46 | + <NonNativeButton as="a" role="button" onInteraction={jest.fn()}> |
| 47 | + {anchorText} |
| 48 | + </NonNativeButton>, |
| 49 | + ); |
| 50 | + |
| 51 | + const linkButton = screen.getByRole("button", { |
| 52 | + name: anchorText, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(linkButton).toBeInstanceOf(HTMLAnchorElement); |
| 56 | + }); |
| 57 | + |
| 58 | + it("Always returns out the same role override received via arguments", () => { |
| 59 | + const placeholderCallback = jest.fn(); |
| 60 | + const roles = [ |
| 61 | + "button", |
| 62 | + "switch", |
| 63 | + ] as const satisfies readonly ClickableAriaRole[]; |
| 64 | + |
| 65 | + for (const role of roles) { |
| 66 | + const { unmount } = render( |
| 67 | + <NonNativeButton role={role} onInteraction={placeholderCallback} />, |
| 68 | + ); |
| 69 | + |
| 70 | + expect(() => screen.getByRole(role)).not.toThrow(); |
| 71 | + unmount(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it("Allows an element to receive keyboard focus", async () => { |
| 76 | + const user = userEvent.setup(); |
| 77 | + const mockCallback = jest.fn(); |
| 78 | + |
| 79 | + render(<NonNativeButton role="button" onInteraction={mockCallback} />, { |
| 80 | + wrapper: ({ children }) => ( |
| 81 | + <> |
| 82 | + <button>Native button</button> |
| 83 | + {children} |
| 84 | + </> |
| 85 | + ), |
| 86 | + }); |
| 87 | + |
| 88 | + await user.keyboard("[Tab][Tab]"); |
| 89 | + await user.keyboard(" "); |
| 90 | + expect(mockCallback).toBeCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it("Allows an element to respond to clicks and Space/Enter, following all rules for native Button element interactions", async () => { |
| 94 | + const mockCallback = jest.fn(); |
| 95 | + const user = userEvent.setup(); |
| 96 | + render(<NonNativeButton role="button" onInteraction={mockCallback} />); |
| 97 | + |
| 98 | + await user.click(document.body); |
| 99 | + await user.keyboard(" "); |
| 100 | + await user.keyboard("[Enter]"); |
| 101 | + expect(mockCallback).not.toBeCalled(); |
| 102 | + |
| 103 | + const button = screen.getByRole("button"); |
| 104 | + await user.click(button); |
| 105 | + await user.keyboard(" "); |
| 106 | + await user.keyboard("[Enter]"); |
| 107 | + expect(mockCallback).toBeCalledTimes(3); |
| 108 | + }); |
| 109 | + |
| 110 | + it("Will keep firing events if the Enter key is held down", async () => { |
| 111 | + const mockCallback = jest.fn(); |
| 112 | + const user = userEvent.setup(); |
| 113 | + render(<NonNativeButton role="button" onInteraction={mockCallback} />); |
| 114 | + |
| 115 | + // Focus over to element, hold down Enter for specified keydown period |
| 116 | + // (count determined by browser/library), and then release the Enter key |
| 117 | + const keydownCount = 5; |
| 118 | + await user.keyboard(`[Tab]{Enter>${keydownCount}}{/Enter}`); |
| 119 | + expect(mockCallback).toBeCalledTimes(keydownCount); |
| 120 | + }); |
| 121 | + |
| 122 | + it("Will NOT keep firing events if the Space key is held down", async () => { |
| 123 | + const mockCallback = jest.fn(); |
| 124 | + const user = userEvent.setup(); |
| 125 | + render(<NonNativeButton role="button" onInteraction={mockCallback} />); |
| 126 | + |
| 127 | + // Focus over to element, and then hold down Space for 100 keydown cycles |
| 128 | + await user.keyboard("[Tab]{ >100}"); |
| 129 | + expect(mockCallback).not.toBeCalled(); |
| 130 | + |
| 131 | + // Then explicitly release the space bar |
| 132 | + await user.keyboard(`{/ }`); |
| 133 | + expect(mockCallback).toBeCalledTimes(1); |
| 134 | + }); |
| 135 | + |
| 136 | + test("If focus is lost while Space is held down, then releasing the key will do nothing", async () => { |
| 137 | + const mockCallback = jest.fn(); |
| 138 | + const user = userEvent.setup(); |
| 139 | + |
| 140 | + render(<NonNativeButton role="button" onInteraction={mockCallback} />, { |
| 141 | + wrapper: ({ children }) => ( |
| 142 | + <> |
| 143 | + {children} |
| 144 | + <button>Native button</button> |
| 145 | + </> |
| 146 | + ), |
| 147 | + }); |
| 148 | + |
| 149 | + // Focus over to element, hold down Space for an indefinite amount of time, |
| 150 | + // move focus away from element, and then release Space |
| 151 | + await user.keyboard("[Tab]{ >}[Tab]{/ }"); |
| 152 | + expect(mockCallback).not.toBeCalled(); |
| 153 | + }); |
| 154 | +}); |
0 commit comments