-
Notifications
You must be signed in to change notification settings - Fork 131
fix: Reset lastClicked in NavLink when route matches #3796
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
Open
doubleface
wants to merge
2
commits into
master
Choose a base branch
from
fix/highlight-proper-section-in-sidebar-even-with-back-button
base: master
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.
+177
−1
Open
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 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,161 @@ | ||
| import { render, fireEvent, act } from '@testing-library/react' | ||
| import React from 'react' | ||
| import { useLocation } from 'react-router-dom' | ||
|
|
||
| import { NavLink } from './NavLink' | ||
|
|
||
| jest.mock('react-router-dom', () => ({ | ||
| ...jest.requireActual('react-router-dom'), | ||
| useLocation: jest.fn() | ||
| })) | ||
|
|
||
| jest.mock('cozy-ui/transpiled/react/Nav', () => ({ | ||
| NavLink: { className: 'nav-link', activeClassName: 'active' } | ||
| })) | ||
|
|
||
| describe('NavLink', () => { | ||
| beforeEach(() => { | ||
| useLocation.mockReturnValue({ pathname: '/recent' }) | ||
| }) | ||
|
|
||
| describe('click handler', () => { | ||
| it('calls setLastClicked with to on click', () => { | ||
| const setLastClicked = jest.fn() | ||
| const { getByRole } = render( | ||
| <NavLink to="/folder" clickState={[null, setLastClicked]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
| fireEvent.click(getByRole('link')) | ||
| expect(setLastClicked).toHaveBeenCalledWith('/folder') | ||
| }) | ||
|
|
||
| it('prevents default when no to prop', () => { | ||
| const setLastClicked = jest.fn() | ||
| const { getByRole } = render( | ||
| <NavLink clickState={[null, setLastClicked]}>Drive</NavLink> | ||
| ) | ||
| const event = new MouseEvent('click', { bubbles: true, cancelable: true }) | ||
| getByRole('link').dispatchEvent(event) | ||
| expect(event.defaultPrevented).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('optimistic active state', () => { | ||
| it('shows as active when lastClicked matches rx', () => { | ||
| const rx = /\/(folder|nextcloud)(\/.*)?/ | ||
| const { getByRole } = render( | ||
| <NavLink to="/folder" rx={rx} clickState={['/folder', jest.fn()]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
| expect(getByRole('link').className).toContain('active') | ||
| }) | ||
|
|
||
| it('is not active when lastClicked does not match rx', () => { | ||
| const rx = /\/(folder|nextcloud)(\/.*)?/ | ||
| const { getByRole } = render( | ||
| <NavLink to="/folder" rx={rx} clickState={['/recent', jest.fn()]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
| expect(getByRole('link').className).not.toContain('active') | ||
| }) | ||
| }) | ||
|
|
||
| describe('useEffect: reset lastClicked when route arrives', () => { | ||
| it('does not reset lastClicked while the route has not yet changed', () => { | ||
| const setLastClicked = jest.fn() | ||
| useLocation.mockReturnValue({ pathname: '/recent' }) | ||
|
|
||
| render( | ||
| <NavLink | ||
| to="/folder" | ||
| rx={/\/(folder|nextcloud)(\/.*)?/} | ||
| clickState={['/folder', setLastClicked]} | ||
| > | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
|
|
||
| expect(setLastClicked).not.toHaveBeenCalledWith(null) | ||
| }) | ||
|
|
||
| it('resets lastClicked once the route matches rx', async () => { | ||
| const setLastClicked = jest.fn() | ||
| useLocation.mockReturnValue({ pathname: '/recent' }) | ||
| const rx = /\/(folder|nextcloud)(\/.*)?/ | ||
|
|
||
| const { rerender } = render( | ||
| <NavLink to="/folder" rx={rx} clickState={['/folder', setLastClicked]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
|
|
||
| useLocation.mockReturnValue({ pathname: '/folder' }) | ||
| await act(async () => { | ||
| rerender( | ||
| <NavLink | ||
| to="/folder" | ||
| rx={rx} | ||
| clickState={['/folder', setLastClicked]} | ||
| > | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
| }) | ||
|
|
||
| expect(setLastClicked).toHaveBeenCalledWith(null) | ||
| }) | ||
|
|
||
| it('resets lastClicked once the route matches without rx', async () => { | ||
| const setLastClicked = jest.fn() | ||
| useLocation.mockReturnValue({ pathname: '/recent' }) | ||
|
|
||
| const { rerender } = render( | ||
| <NavLink to="folder" clickState={['folder', setLastClicked]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
|
|
||
| useLocation.mockReturnValue({ pathname: '/folder' }) | ||
| await act(async () => { | ||
| rerender( | ||
| <NavLink to="folder" clickState={['folder', setLastClicked]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
| }) | ||
|
|
||
| expect(setLastClicked).toHaveBeenCalledWith(null) | ||
| }) | ||
|
|
||
| it('resets lastClicked when route changes to a different destination (e.g. back button)', async () => { | ||
| const setLastClicked = jest.fn() | ||
| useLocation.mockReturnValue({ pathname: '/recent' }) | ||
| const rx = /\/(folder|nextcloud)(\/.*)?/ | ||
|
|
||
| const { rerender } = render( | ||
| <NavLink to="/folder" rx={rx} clickState={['/folder', setLastClicked]}> | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
|
|
||
| // Route changed but to /trash, not /folder (e.g. back button or unexpected navigation) | ||
| useLocation.mockReturnValue({ pathname: '/trash' }) | ||
| await act(async () => { | ||
| rerender( | ||
| <NavLink | ||
| to="/folder" | ||
| rx={rx} | ||
| clickState={['/folder', setLastClicked]} | ||
| > | ||
| Drive | ||
| </NavLink> | ||
| ) | ||
| }) | ||
|
|
||
| expect(setLastClicked).toHaveBeenCalledWith(null) | ||
| }) | ||
| }) | ||
| }) | ||
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.
❌ New issue: Code Duplication
The module contains 3 functions with similar structure: 'resets lastClicked once the route matches rx','resets lastClicked once the route matches without rx','resets lastClicked when route changes to a different destination (e.g. back button)'
Suppress