-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(multiple): le bouton retour apparait tout le temps (#2324)
* fix(multiple): le bouton retour apparait tout le temps * fix: retour review
- Loading branch information
Showing
8 changed files
with
133 additions
and
34 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/client/components/features/ButtonRetour/BackButton.test.tsx
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,46 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { BackButton } from '~/client/components/features/ButtonRetour/BackButton'; | ||
import { mockUseRouter } from '~/client/components/useRouter.mock'; | ||
import { mockSessionStorage } from '~/client/components/window.mock'; | ||
|
||
describe('BackButton', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('Lorsque la variable PREVIOUS_PAGE est définie dans le sessionStorage', () => { | ||
it('affiche le bouton de retour', () => { | ||
// Given | ||
mockUseRouter({}); | ||
mockSessionStorage({ | ||
getItem: jest.fn().mockReturnValue('/page-1'), | ||
}); | ||
|
||
// When | ||
render(<BackButton />); | ||
|
||
// Then | ||
expect(screen.getByRole('button', { name: 'Retour vers la page précédente' })).toBeInTheDocument(); | ||
}); | ||
}); | ||
describe('Lorsque la variable PREVIOUS_PAGE n’est pas définie dans le sessionStorage', () => { | ||
it('n’affiche pas le bouton de retour', () => { | ||
// Given | ||
mockUseRouter({}); | ||
mockSessionStorage({ | ||
getItem: jest.fn().mockReturnValue(null), | ||
}); | ||
|
||
// When | ||
render(<BackButton />); | ||
|
||
// Then | ||
expect(screen.queryByRole('button', { name: 'Retour vers la page précédente' })).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
18 changes: 6 additions & 12 deletions
18
src/client/components/features/ButtonRetour/BackButton.tsx
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
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
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,54 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { render } from '@testing-library/react'; | ||
|
||
import { mockUseRouter } from '~/client/components/useRouter.mock'; | ||
import useDisplayBackButton from '~/client/hooks/useDisplayBackButton'; | ||
|
||
function TestComponent() { | ||
useDisplayBackButton(); | ||
return <></>; | ||
} | ||
|
||
describe('useDisplayBackButton', () => { | ||
beforeEach(() => { | ||
sessionStorage.clear(); | ||
jest.resetAllMocks(); | ||
}); | ||
describe('quand la page actuel est la première sur laquelle on navigue', () => { | ||
it('stocke le pathname de la page dans sessionStorage', () => { | ||
// Given | ||
mockUseRouter({ | ||
pathname: '/', | ||
}); | ||
const setItem = jest.spyOn(Object.getPrototypeOf(sessionStorage), 'setItem'); | ||
jest.spyOn(Object.getPrototypeOf(sessionStorage), 'getItem').mockReturnValue(null); | ||
|
||
// When | ||
render(<TestComponent />); | ||
|
||
// Then | ||
expect(setItem).toHaveBeenCalledWith('current-page', '/'); | ||
}); | ||
}); | ||
|
||
describe('quand la page actuel n’est pas la première sur laquelle on navigue', () => { | ||
it('stocke le pathname de la page dans sessionStorage', () => { | ||
// Given | ||
mockUseRouter({ | ||
pathname: '/other-page', | ||
}); | ||
const setItem = jest.spyOn(Object.getPrototypeOf(sessionStorage), 'setItem'); | ||
jest.spyOn(Object.getPrototypeOf(sessionStorage), 'getItem').mockReturnValue('/'); | ||
|
||
// When | ||
render(<TestComponent />); | ||
|
||
// Then | ||
expect(setItem).toHaveBeenCalledWith('previous-page', '/'); | ||
expect(setItem).toHaveBeenLastCalledWith('current-page', '/other-page'); | ||
}); | ||
}); | ||
}); |
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,19 @@ | ||
import { useRouter } from 'next/router'; | ||
import { useLayoutEffect } from 'react'; | ||
|
||
const CURRENT_PAGE = 'current-page'; | ||
export const PREVIOUS_PAGE = 'previous-page'; | ||
|
||
function useDisplayBackButton(): void { | ||
const router = useRouter(); | ||
|
||
useLayoutEffect(() => { | ||
const currentPage = sessionStorage.getItem(CURRENT_PAGE); | ||
if (currentPage && currentPage !== router.pathname) { | ||
sessionStorage.setItem(PREVIOUS_PAGE, currentPage); | ||
} | ||
sessionStorage.setItem(CURRENT_PAGE, router.pathname); | ||
}, [router.pathname]); | ||
} | ||
|
||
export default useDisplayBackButton; |
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
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