Skip to content

Commit

Permalink
fix(react/utils): check if env is jsdom (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Jan 16, 2024
1 parent 31bba5e commit 8b3c93b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-islands-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@floating-ui/react": patch
---

fix(utils): check if env is JSDOM for `isVirtualPointerEvent`. Fixes issue when testing `visibleOnly` prop in `useFocus`.
5 changes: 5 additions & 0 deletions packages/react/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export function isVirtualClick(event: MouseEvent | PointerEvent): boolean {
}

export function isVirtualPointerEvent(event: PointerEvent) {
if (isJSDOM()) return false;
return (
(!isAndroid() && event.width === 0 && event.height === 0) ||
(isAndroid() &&
Expand Down Expand Up @@ -121,6 +122,10 @@ export function isMac() {
);
}

export function isJSDOM() {
return getUserAgent().includes('jsdom/');
}

export function isMouseLikePointerType(
pointerType: string | undefined,
strict?: boolean,
Expand Down
39 changes: 39 additions & 0 deletions packages/react/test/unit/useFocus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,42 @@ test('reason string', async () => {
await act(async () => {});
fireEvent.focusOut(button);
});

describe('visibleOnly prop', () => {
function App({visibleOnly}: {visibleOnly: boolean}) {
const [isOpen, setIsOpen] = useState(false);
const {refs, context} = useFloating({
open: isOpen,
onOpenChange: setIsOpen,
});

const focus = useFocus(context, {visibleOnly});

const {getReferenceProps, getFloatingProps} = useInteractions([focus]);

return (
<>
<button ref={refs.setReference} {...getReferenceProps()} />
{isOpen && (
<div role="tooltip" ref={refs.setFloating} {...getFloatingProps()} />
)}
</>
);
}

test('true', async () => {
render(<App visibleOnly />);
const button = screen.getByRole('button');
await userEvent.click(button);
await act(async () => {});
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
});

test('false', async () => {
render(<App visibleOnly={false} />);
const button = screen.getByRole('button');
await userEvent.click(button);
await act(async () => {});
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});
});

0 comments on commit 8b3c93b

Please sign in to comment.