-
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
Components: add tests for Navigator*
#35163
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f3e01f2
Add basic tests
ciampo c93f0bd
Mock framer motion
ciampo 6b0d16f
Add more initialPath tests
ciampo c32a280
Add screen navigation tests
ciampo cc0ef1d
Add way to spy on `navigator.push()`
ciampo dca0283
Spy on `navigator.push()`
ciampo 1c66c6e
Add tests for paths not matching any screens
ciampo 866b9c2
Remove comments
ciampo 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Navigator, NavigatorScreen, useNavigator } from '../'; | ||
|
||
jest.mock( 'framer-motion', () => { | ||
const actual = jest.requireActual( 'framer-motion' ); | ||
return { | ||
__esModule: true, | ||
...actual, | ||
AnimatePresence: ( { children } ) => <div>{ children }</div>, | ||
motion: { | ||
...actual.motion, | ||
div: require( 'react' ).forwardRef( ( { children }, ref ) => ( | ||
<div ref={ ref }>{ children }</div> | ||
) ), | ||
}, | ||
}; | ||
} ); | ||
|
||
const PATHS = { | ||
HOME: '/', | ||
CHILD: '/child', | ||
NOT_FOUND: '/not-found', | ||
}; | ||
|
||
function NavigatorButton( { path, isBack = false, onClick, ...props } ) { | ||
const navigator = useNavigator(); | ||
return ( | ||
<button | ||
onClick={ () => { | ||
navigator.push( path, { isBack } ); | ||
// Used to spy on the values passed to `navigator.push` | ||
onClick?.( { path, isBack } ); | ||
} } | ||
{ ...props } | ||
/> | ||
); | ||
} | ||
|
||
const MyNavigation = ( { | ||
initialPath = PATHS.HOME, | ||
onNavigatorButtonClick, | ||
} ) => ( | ||
<Navigator initialPath={ initialPath }> | ||
<NavigatorScreen path={ PATHS.HOME }> | ||
<p>This is the home screen.</p> | ||
<NavigatorButton | ||
path={ PATHS.CHILD } | ||
onClick={ onNavigatorButtonClick } | ||
> | ||
Navigate to child screen. | ||
</NavigatorButton> | ||
<NavigatorButton | ||
path={ PATHS.NOT_FOUND } | ||
onClick={ onNavigatorButtonClick } | ||
> | ||
Navigate to non-existing screen. | ||
</NavigatorButton> | ||
</NavigatorScreen> | ||
|
||
<NavigatorScreen path={ PATHS.CHILD }> | ||
<p>This is the child screen.</p> | ||
<NavigatorButton | ||
path={ PATHS.HOME } | ||
isBack | ||
onClick={ onNavigatorButtonClick } | ||
> | ||
Go back | ||
</NavigatorButton> | ||
</NavigatorScreen> | ||
|
||
{ /* A `NavigatorScreen` with `path={ PATHS.NOT_FOUND }` is purposefully not included */ } | ||
</Navigator> | ||
); | ||
|
||
const getNavigationScreenByText = ( text, { throwIfNotFound = true } = {} ) => { | ||
const fnName = throwIfNotFound ? 'getByText' : 'queryByText'; | ||
return screen[ fnName ]( text ); | ||
}; | ||
const getHomeScreen = ( { throwIfNotFound } = {} ) => | ||
getNavigationScreenByText( 'This is the home screen.', { | ||
throwIfNotFound, | ||
} ); | ||
const getChildScreen = ( { throwIfNotFound } = {} ) => | ||
getNavigationScreenByText( 'This is the child screen.', { | ||
throwIfNotFound, | ||
} ); | ||
|
||
const getNavigationButtonByText = ( text, { throwIfNotFound = true } = {} ) => { | ||
const fnName = throwIfNotFound ? 'getByRole' : 'queryByRole'; | ||
return screen[ fnName ]( 'button', { name: text } ); | ||
}; | ||
const getToNonExistingScreenButton = ( { throwIfNotFound } = {} ) => | ||
getNavigationButtonByText( 'Navigate to non-existing screen.', { | ||
throwIfNotFound, | ||
} ); | ||
const getToChildScreenButton = ( { throwIfNotFound } = {} ) => | ||
getNavigationButtonByText( 'Navigate to child screen.', { | ||
throwIfNotFound, | ||
} ); | ||
const getToHomeScreenButton = ( { throwIfNotFound } = {} ) => | ||
getNavigationButtonByText( 'Go back', { | ||
throwIfNotFound, | ||
} ); | ||
|
||
describe( 'Navigator', () => { | ||
it( 'should render', () => { | ||
render( <MyNavigation /> ); | ||
|
||
expect( getHomeScreen() ).toBeInTheDocument(); | ||
expect( | ||
getChildScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
} ); | ||
|
||
it( 'should show a different screen on the first render depending on the value of `initialPath`', () => { | ||
render( <MyNavigation initialPath={ PATHS.CHILD } /> ); | ||
|
||
expect( | ||
getHomeScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
expect( getChildScreen() ).toBeInTheDocument(); | ||
} ); | ||
|
||
it( 'should ignore changes to `initialPath` after the first render', () => { | ||
const { rerender } = render( <MyNavigation /> ); | ||
|
||
expect( getHomeScreen() ).toBeInTheDocument(); | ||
expect( | ||
getChildScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
|
||
rerender( <MyNavigation initialPath={ PATHS.CHILD } /> ); | ||
|
||
expect( getHomeScreen() ).toBeInTheDocument(); | ||
expect( | ||
getChildScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
} ); | ||
|
||
it( 'should not rended anything if the `initialPath` does not match any available screen', () => { | ||
render( <MyNavigation initialPath={ PATHS.NOT_FOUND } /> ); | ||
|
||
expect( | ||
getHomeScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
expect( | ||
getChildScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
} ); | ||
|
||
it( 'should navigate across screens', () => { | ||
const spy = jest.fn(); | ||
|
||
render( <MyNavigation onNavigatorButtonClick={ spy } /> ); | ||
|
||
expect( getToChildScreenButton() ).toBeInTheDocument(); | ||
|
||
// Navigate to child screen | ||
fireEvent.click( getToChildScreenButton() ); | ||
|
||
expect( | ||
getHomeScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
expect( getChildScreen() ).toBeInTheDocument(); | ||
expect( getToHomeScreenButton() ).toBeInTheDocument(); | ||
|
||
// Navigate back to home screen | ||
fireEvent.click( getToHomeScreenButton() ); | ||
|
||
expect( | ||
getChildScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
expect( getHomeScreen() ).toBeInTheDocument(); | ||
|
||
// Check the values passed to `navigator.push()` | ||
expect( spy ).toHaveBeenCalledTimes( 2 ); | ||
expect( spy ).toHaveBeenNthCalledWith( 1, { | ||
path: PATHS.CHILD, | ||
isBack: false, | ||
} ); | ||
expect( spy ).toHaveBeenNthCalledWith( 2, { | ||
path: PATHS.HOME, | ||
isBack: true, | ||
} ); | ||
} ); | ||
|
||
it( 'should not rended anything if the path does not match any available screen', () => { | ||
const spy = jest.fn(); | ||
|
||
render( <MyNavigation onNavigatorButtonClick={ spy } /> ); | ||
|
||
expect( getToChildScreenButton() ).toBeInTheDocument(); | ||
|
||
// Attempt to navigate to non-existing screen. No screens get rendered. | ||
fireEvent.click( getToNonExistingScreenButton() ); | ||
|
||
expect( | ||
getHomeScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
expect( | ||
getChildScreen( { throwIfNotFound: false } ) | ||
).not.toBeInTheDocument(); | ||
|
||
// Check the values passed to `navigator.push()` | ||
expect( spy ).toHaveBeenCalledTimes( 1 ); | ||
expect( spy ).toHaveBeenNthCalledWith( 1, { | ||
path: PATHS.NOT_FOUND, | ||
isBack: false, | ||
} ); | ||
} ); | ||
} ); |
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 is necessary to "ignore" the transitions in the tests