Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(popover-container): rewrite tests to use RTL instead of Enzyme #6973

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 31 additions & 40 deletions src/components/popover-container/popover-container.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
useMemo,
} from "react";
import { PaddingProps } from "styled-system";
import { Transition, TransitionStatus } from "react-transition-group";
import { CSSTransition } from "react-transition-group";
import { flip, offset } from "@floating-ui/dom";

import useMediaQuery from "../../hooks/useMediaQuery";
Expand Down Expand Up @@ -210,15 +210,13 @@ export const PopoverContainer = ({
| React.KeyboardEvent<HTMLElement>
| KeyboardEvent
) => {
if (!isControlled) {
setIsOpenInternal(false);
}
if (onClose) {
onClose(ev);
}
if (isOpen && openButtonRef.current) {
openButtonRef.current.focus();
}
/* istanbul ignore else */
if (!isControlled) setIsOpenInternal(false);

onClose?.(ev);

/* istanbul ignore else */
if (isOpen) openButtonRef.current?.focus();
},
[isControlled, isOpen, onClose]
);
Expand Down Expand Up @@ -251,12 +249,12 @@ export const PopoverContainer = ({
const handleOpenButtonClick = (
e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>
) => {
/* istanbul ignore else */
if (!isControlled) setIsOpenInternal(!isOpen);

// We want the open button to close the popover if it is already open
if (!isOpen) {
if (onOpen) onOpen(e);
} else if (onClose) onClose(e);
if (!isOpen) onOpen?.(e);
else onClose?.(e);
};

const handleCloseButtonClick = (
Expand Down Expand Up @@ -294,17 +292,16 @@ export const PopoverContainer = ({

const handleClickAway = (e: Event) => {
if (!isControlled) setIsOpenInternal(false);
if (onClose && isOpen) onClose(e);
if (isOpen) onClose?.(e);
};

const handleClick = useClickAwayListener(handleClickAway, "mousedown");
const [isAnimationComplete, setIsAnimationComplete] = useState(false);

const popover = (state: TransitionStatus) => (
const popover = () => (
<PopoverContainerContentStyle
data-element="popover-container-content"
role="dialog"
animationState={state}
aria-labelledby={popoverContainerId}
aria-label={containerAriaLabel}
aria-describedby={ariaDescribedBy}
Expand All @@ -327,15 +324,15 @@ export const PopoverContainer = ({
</PopoverContainerContentStyle>
);

const childrenToRender = (state: TransitionStatus) =>
const childrenToRender = () =>
shouldCoverButton ? (
<ModalContext.Provider value={{ isAnimationComplete }}>
<FocusTrap wrapperRef={popoverContentNodeRef} isOpen={isOpen}>
{popover(state)}
{popover()}
</FocusTrap>
</ModalContext.Provider>
) : (
popover(state)
popover()
);

return (
Expand All @@ -346,13 +343,11 @@ export const PopoverContainer = ({
<div ref={popoverReference}>
{renderOpenComponent(renderOpenComponentProps)}
</div>
<Transition
<CSSTransition
nodeRef={popoverContentNodeRef}
timeout={{ exit: disableAnimation ? 0 : 300 }}
in={isOpen}
timeout={{ exit: 300 }}
appear
mountOnEnter
unmountOnExit
nodeRef={popoverContentNodeRef}
onEntered={
shouldCoverButton
? /* istanbul ignore next */ () => setIsAnimationComplete(true)
Expand All @@ -364,22 +359,18 @@ export const PopoverContainer = ({
: undefined
}
>
{(state: TransitionStatus) =>
isOpen && (
<Popover
reference={popoverReference}
placement={position === "right" ? "bottom-start" : "bottom-end"}
popoverStrategy={
disableAnimation || reduceMotion ? "fixed" : "absolute"
}
middleware={popoverMiddleware}
childRefOverride={popoverContentNodeRef}
>
{childrenToRender(state)}
</Popover>
)
}
</Transition>
<Popover
reference={popoverReference}
placement={position === "right" ? "bottom-start" : "bottom-end"}
popoverStrategy={
disableAnimation || reduceMotion ? "fixed" : "absolute"
}
middleware={popoverMiddleware}
childRefOverride={popoverContentNodeRef}
>
{childrenToRender()}
</Popover>
</CSSTransition>
</PopoverContainerWrapperStyle>
);
};
Expand Down
Loading
Loading