-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Popover: convert unit tests to TypeScript and modern RTL assertions #44373
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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
34 changes: 0 additions & 34 deletions
34
packages/components/src/popover/test/__snapshots__/index.js.snap
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { act, render, screen } from '@testing-library/react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import type { CSSProperties } from 'react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -11,69 +12,83 @@ import { useState } from '@wordpress/element'; | |
/** | ||
* Internal dependencies | ||
*/ | ||
import Popover from '../'; | ||
|
||
import { positionToPlacement, placementToMotionAnimationProps } from '../utils'; | ||
import Popover from '..'; | ||
import type { PopoverProps } from '../types'; | ||
|
||
type PositionToPlacementTuple = [ | ||
NonNullable< PopoverProps[ 'position' ] >, | ||
NonNullable< PopoverProps[ 'placement' ] > | ||
]; | ||
type PlacementToAnimationOriginTuple = [ | ||
NonNullable< PopoverProps[ 'placement' ] >, | ||
number, | ||
number | ||
]; | ||
type PlacementToInitialTranslationTuple = [ | ||
NonNullable< PopoverProps[ 'placement' ] >, | ||
'translateY' | 'translateX', | ||
CSSProperties[ 'translate' ] | ||
]; | ||
|
||
describe( 'Popover', () => { | ||
describe( 'Component', () => { | ||
afterEach( () => { | ||
if ( document.activeElement ) { | ||
document.activeElement.blur(); | ||
} | ||
} ); | ||
describe( 'basic behavior', () => { | ||
it( 'should render content', () => { | ||
render( <Popover>Hello</Popover> ); | ||
|
||
it( 'should allow focus-on-open behavior to be disabled', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has been split into two separate tests ( |
||
expect( document.activeElement ).toBe( document.body ); | ||
expect( screen.getByText( 'Hello' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
act( () => { | ||
render( <Popover focusOnMount={ false } /> ); | ||
it( 'should forward additional props to portaled element', () => { | ||
render( <Popover role="tooltip">Hello</Popover> ); | ||
|
||
jest.advanceTimersByTime( 1 ); | ||
expect( screen.getByRole( 'tooltip' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
expect( document.activeElement ).toBe( document.body ); | ||
} ); | ||
|
||
it( 'should render content', () => { | ||
let result; | ||
act( () => { | ||
result = render( <Popover>Hello</Popover> ); | ||
} ); | ||
describe( 'anchor', () => { | ||
it( 'should render correctly when anchor is provided', () => { | ||
const PopoverWithAnchor = ( args: PopoverProps ) => { | ||
// Use internal state instead of a ref to make sure that the component | ||
// re-renders when the popover's anchor updates. | ||
const [ anchor, setAnchor ] = | ||
useState< HTMLParagraphElement | null >( null ); | ||
|
||
return ( | ||
<div> | ||
<p ref={ setAnchor }>Anchor</p> | ||
<Popover { ...args } anchor={ anchor } /> | ||
</div> | ||
); | ||
}; | ||
|
||
expect( | ||
result.container.querySelector( 'span' ) | ||
).toMatchSnapshot(); | ||
} ); | ||
render( | ||
<PopoverWithAnchor>Popover content</PopoverWithAnchor> | ||
); | ||
|
||
it( 'should pass additional props to portaled element', () => { | ||
let result; | ||
act( () => { | ||
result = render( <Popover role="tooltip">Hello</Popover> ); | ||
expect( | ||
screen.getByText( 'Popover content' ) | ||
).toBeInTheDocument(); | ||
} ); | ||
|
||
expect( | ||
result.container.querySelector( 'span' ) | ||
).toMatchSnapshot(); | ||
} ); | ||
|
||
it( 'should render correctly when anchor is provided', () => { | ||
const PopoverWithAnchor = ( args ) => { | ||
// Use internal state instead of a ref to make sure that the component | ||
// re-renders when the popover's anchor updates. | ||
const [ anchor, setAnchor ] = useState( null ); | ||
|
||
return ( | ||
<div> | ||
<p ref={ setAnchor }>Anchor</p> | ||
<Popover { ...args } anchor={ anchor } /> | ||
</div> | ||
); | ||
}; | ||
describe( 'focus behavior', () => { | ||
it( 'should focus the popover by default when opened', () => { | ||
render( <Popover>Popover content</Popover> ); | ||
|
||
render( <PopoverWithAnchor>Popover content</PopoverWithAnchor> ); | ||
expect( | ||
screen.getByText( 'Popover content' ).parentElement | ||
).toHaveFocus(); | ||
} ); | ||
|
||
expect( screen.getByText( 'Popover content' ) ).toBeInTheDocument(); | ||
it( 'should allow focus-on-open behavior to be disabled', () => { | ||
render( | ||
<Popover focusOnMount={ false }>Popover content</Popover> | ||
); | ||
|
||
expect( document.body ).toHaveFocus(); | ||
} ); | ||
} ); | ||
} ); | ||
|
||
|
@@ -88,11 +103,14 @@ describe( 'Popover', () => { | |
[ 'bottom left', 'bottom-end' ], | ||
[ 'bottom center', 'bottom' ], | ||
[ 'bottom right', 'bottom-start' ], | ||
] )( 'converts `%s` to `%s`', ( inputPosition, expectedPlacement ) => { | ||
expect( positionToPlacement( inputPosition ) ).toEqual( | ||
expectedPlacement | ||
); | ||
} ); | ||
] as PositionToPlacementTuple[] )( | ||
'converts `%s` to `%s`', | ||
( inputPosition, expectedPlacement ) => { | ||
expect( positionToPlacement( inputPosition ) ).toEqual( | ||
expectedPlacement | ||
); | ||
} | ||
); | ||
} ); | ||
|
||
describe( 'placementToMotionAnimationProps', () => { | ||
|
@@ -110,7 +128,7 @@ describe( 'Popover', () => { | |
[ 'left', 1, 0.5 ], | ||
[ 'left-start', 1, 0 ], | ||
[ 'left-end', 1, 1 ], | ||
] )( | ||
] as PlacementToAnimationOriginTuple[] )( | ||
'for the `%s` placement computes an animation origin of (%d, %d)', | ||
( inputPlacement, expectedOriginX, expectedOriginY ) => { | ||
expect( | ||
|
@@ -140,7 +158,7 @@ describe( 'Popover', () => { | |
[ 'left', 'translateX', '2em' ], | ||
[ 'left-start', 'translateX', '2em' ], | ||
[ 'left-end', 'translateX', '2em' ], | ||
] )( | ||
] as PlacementToInitialTranslationTuple[] )( | ||
'for the `%s` placement computes an initial `%s` of `%s', | ||
( | ||
inputPlacement, | ||
|
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.
This was not necessary anymore