Repository navigation
[HOLD] Reposition three-dots popover menus after a viewport resize #101077
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
Draft
MelvinBot
wants to merge
3
commits into
main
Choose a base branch
from
claude-walletPopoverResizeReposition
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import {act, fireEvent, render, screen} from '@testing-library/react-native'; | ||
|
|
||
| import ThreeDotsMenu from '@components/ThreeDotsMenu'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
|
|
||
| import type {ReactNode} from 'react'; | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| type MockPopoverMenuProps = {isVisible?: boolean; anchorPosition?: {horizontal: number; vertical: number}}; | ||
|
|
||
| const mockMeasuredAnchor = {horizontal: 0, vertical: 0, width: 40, height: 40}; | ||
| const mockWindowDimensions = {windowWidth: 1400, windowHeight: 900}; | ||
| const mockMeasureCount = {current: 0}; | ||
|
|
||
| /** Records every `PopoverMenu` render so assertions can read the `anchorPosition` it was last given while open. */ | ||
| const mockPopoverRender = jest.fn<void, [MockPopoverMenuProps]>(); | ||
|
|
||
| jest.mock('@hooks/useWindowDimensions', () => () => mockWindowDimensions); | ||
|
|
||
| jest.mock('@hooks/usePopoverPosition', () => () => ({ | ||
| calculatePopoverPosition: () => { | ||
| mockMeasureCount.current++; | ||
| return Promise.resolve({...mockMeasuredAnchor}); | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@hooks/useOnyx', () => () => [undefined, {status: 'loaded'}]); | ||
|
|
||
| jest.mock('@components/PopoverMenu', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- jest.requireActual returns an untyped module; standard RN-mock pattern in this repo. | ||
| const {View: RNView} = jest.requireActual('react-native'); | ||
| function MockPopoverMenu({isVisible, anchorPosition}: MockPopoverMenuProps) { | ||
| mockPopoverRender({isVisible, anchorPosition}); | ||
| return <RNView testID={isVisible ? 'popover-open' : 'popover-closed'} />; | ||
| } | ||
| return MockPopoverMenu; | ||
| }); | ||
|
|
||
| jest.mock('@components/Icon', () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- jest.requireActual returns an untyped module; standard RN-mock pattern in this repo. | ||
| const {View: RNView} = jest.requireActual('react-native'); | ||
| function MockIcon() { | ||
| return <RNView />; | ||
| } | ||
| return MockIcon; | ||
| }); | ||
|
|
||
| jest.mock('@components/Tooltip/PopoverAnchorTooltip', () => { | ||
| function MockTooltip({children}: {children?: ReactNode}) { | ||
| return children; | ||
| } | ||
| return MockTooltip; | ||
| }); | ||
|
|
||
| jest.mock('@components/Tooltip/EducationalTooltip', () => { | ||
| function MockEducationalTooltip({children}: {children?: ReactNode}) { | ||
| return children; | ||
| } | ||
| return MockEducationalTooltip; | ||
| }); | ||
|
|
||
| const setMeasuredAnchor = (horizontal: number, vertical: number) => { | ||
| mockMeasuredAnchor.horizontal = horizontal; | ||
| mockMeasuredAnchor.vertical = vertical; | ||
| }; | ||
|
|
||
| const menuElement = ( | ||
| <ThreeDotsMenu | ||
| shouldSelfPosition | ||
| testID="three-dots-menu" | ||
| menuItems={[{text: 'Delete', onSelected: () => {}}]} | ||
| /> | ||
| ); | ||
|
|
||
| /** The `anchorPosition` from the most recent render in which the popover was open. */ | ||
| const lastOpenAnchorPosition = () => mockPopoverRender.mock.calls.map(([props]) => props).findLast((props) => props.isVisible)?.anchorPosition; | ||
|
|
||
| /** Lets a pending `calculatePopoverPosition` promise settle without running the reposition debounce. */ | ||
| const flushPendingMeasurements = () => | ||
| act(async () => { | ||
| jest.advanceTimersByTime(0); | ||
| }); | ||
|
|
||
| describe('ThreeDotsMenu repositioning', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| setMeasuredAnchor(1000, 300); | ||
| mockWindowDimensions.windowWidth = 1400; | ||
| mockWindowDimensions.windowHeight = 900; | ||
| mockPopoverRender.mockClear(); | ||
| mockMeasureCount.current = 0; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('re-measures the anchor only after the layout has settled following a viewport change', async () => { | ||
| const {rerender} = render(menuElement); | ||
|
|
||
| fireEvent.press(screen.getByTestId('three-dots-menu')); | ||
| await flushPendingMeasurements(); | ||
|
|
||
| expect(screen.getByTestId('popover-open')).toBeOnTheScreen(); | ||
| expect(lastOpenAnchorPosition()?.horizontal).toBe(1000); | ||
|
|
||
| // The viewport shrinks. Surrounding layout (a virtualized list, for instance) has not repositioned the | ||
| // anchor yet, so measuring at this point would still report the pre-resize coordinates. | ||
| mockWindowDimensions.windowWidth = 1000; | ||
| rerender(menuElement); | ||
| await flushPendingMeasurements(); | ||
|
|
||
| // The surrounding layout settles and moves the anchor to its post-resize position. | ||
| setMeasuredAnchor(700, 300); | ||
|
|
||
| await act(async () => { | ||
| jest.advanceTimersByTime(CONST.TIMING.RESIZE_DEBOUNCE_TIME); | ||
| }); | ||
|
|
||
| expect(lastOpenAnchorPosition()?.horizontal).toBe(700); | ||
| }); | ||
|
|
||
| it('does not re-measure the anchor while the menu is closed', async () => { | ||
| const {rerender} = render(menuElement); | ||
| mockMeasureCount.current = 0; | ||
|
|
||
| mockWindowDimensions.windowWidth = 1000; | ||
| rerender(menuElement); | ||
| await act(async () => { | ||
| jest.advanceTimersByTime(CONST.TIMING.RESIZE_DEBOUNCE_TIME); | ||
| }); | ||
|
|
||
| expect(mockMeasureCount.current).toBe(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both added
itblocks omit the required literal// Given,// When, and// Thensections explaining why their setup, action, and assertions exist. Add all three documented sections to each test so this regression coverage follows the repository's mandatory test-documentation convention.AGENTS.md reference: AGENTS.md:L1-L3
Useful? React with 👍 / 👎.