Skip to content

Commit

Permalink
fix component test from failing due to change in open close button be…
Browse files Browse the repository at this point in the history
…haviour
  • Loading branch information
alasdairwilson committed Nov 27, 2024
1 parent aa2d423 commit dab3409
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions components/content/Thread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,18 @@ const Thread = ({ thread, active, setActive, onDelete, finaliseThread }: ThreadP
const calculatePosition = () => {
if (triggerRef.current) {
const rect = triggerRef.current.getBoundingClientRect()
const popupWidth = 355 // Adjust this value based on your actual popup width
console.log(rect)
const popupWidth = 355
const viewportWidth = window.innerWidth

let leftPosition = rect.right + 10 // Position to the right of the thread
let leftPosition = rect.left + 40
if (leftPosition + popupWidth > viewportWidth) {
// If the popup overflows the viewport, adjust to the left
// if the popup is going to expand the viewport then we move it to the left
leftPosition = rect.left - popupWidth - 10
}

setPopupPosition({
top: rect.top + window.scrollY, // Account for vertical scroll
top: rect.top + window.scrollY,
left: leftPosition,
})
}
Expand All @@ -124,7 +125,7 @@ const Thread = ({ thread, active, setActive, onDelete, finaliseThread }: ThreadP
if (active) {
calculatePosition() // Calculate the position when the popup is active
}
}, [active]) // Recalculate when the active state changes
}, [active])

const sortedComments = useMemo(() => {
if (!commentThread) return []
Expand Down Expand Up @@ -207,12 +208,12 @@ const Thread = ({ thread, active, setActive, onDelete, finaliseThread }: ThreadP

const renderPopup = () => {
if (!active) return null

console.log(popupPosition)
return createPortal(
<div
ref={popupRef}
className="absolute w-[355px] border border-gray-200 rounded-lg bg-slate-50 dark:bg-slate-900 dark:border-gray-700 z-50"
style={{ top: `${popupPosition.top}px`, left: `${popupPosition.left}px` }} // Apply calculated position
style={{ top: `${popupPosition.top}px`, left: `${popupPosition.left}px` }}
data-cy={`Thread:${threadId}:Main`}
>
<div className="absolute -top-1 left-0 not-prose">
Expand Down Expand Up @@ -243,7 +244,7 @@ const Thread = ({ thread, active, setActive, onDelete, finaliseThread }: ThreadP
<div className={`flex items-center justify-between rounded-t-lg`}>
<h3 className="w-full mx-2 my-0 text-base text-slate-100 dark:text-slate-900">{}</h3>
<Stack direction="row-reverse">
<TinyButton onClick={() => setActive(false)}>
<TinyButton data-cy={`Thread:${threadId}:CloseButton`} onClick={() => setActive(false)}>
<IoClose />
</TinyButton>
{!isPlaceholder && (
Expand Down
4 changes: 2 additions & 2 deletions cypress/component/CommentThread.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ describe("CommentThread component", () => {
)
})

it("should open and close the thread", () => {
cy.get('[data-cy="Thread:1:OpenCloseButton"]').click()
it("should close the thread", () => {
cy.get('[data-cy="Thread:1:CloseButton"]').click()
cy.get("@setActive").should("be.calledWith", false)
})

Expand Down

0 comments on commit dab3409

Please sign in to comment.