-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Anchor): add
scrollToHash
feature (#2290)
* feat(Anchor): add `scrollToHash` feature Closes #2286 * Make the import optional, so its not imported when tree-shaked
- Loading branch information
1 parent
8414ae7
commit 3136367
Showing
4 changed files
with
202 additions
and
27 deletions.
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
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
138 changes: 138 additions & 0 deletions
138
packages/dnb-eufemia/src/components/anchor/__tests__/AnchorScroll.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,138 @@ | ||
/** | ||
* Element Test | ||
* | ||
*/ | ||
|
||
import React from 'react' | ||
import { fireEvent, render } from '@testing-library/react' | ||
import Anchor, { scrollToHashHandler } from '../Anchor' | ||
|
||
describe('Anchor with scrollToHashHandler', () => { | ||
let location: Location | ||
|
||
beforeEach(() => { | ||
location = window.location | ||
}) | ||
|
||
it('should call window.scroll', () => { | ||
const onScoll = jest.fn() | ||
|
||
jest.spyOn(window, 'scroll').mockImplementationOnce(onScoll) | ||
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({ | ||
...location, | ||
href: 'http://localhost/path', | ||
}) | ||
|
||
render( | ||
<> | ||
<Anchor onClick={scrollToHashHandler} href="/path/#hash-id"> | ||
text | ||
</Anchor> | ||
<span id="hash-id" /> | ||
</> | ||
) | ||
|
||
const element = document.querySelector('a') | ||
fireEvent.click(element) | ||
|
||
expect(onScoll).toHaveBeenCalledTimes(1) | ||
expect(onScoll).toHaveBeenCalledWith({ top: 0 }) | ||
}) | ||
|
||
it('should use last hash', () => { | ||
const onScoll = jest.fn() | ||
|
||
jest.spyOn(window, 'scroll').mockImplementationOnce(onScoll) | ||
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({ | ||
...location, | ||
href: 'http://localhost/path', | ||
}) | ||
|
||
render( | ||
<> | ||
<Anchor | ||
onClick={scrollToHashHandler} | ||
href="/path/#first-hash#hash-id" | ||
> | ||
text | ||
</Anchor> | ||
<span id="hash-id" /> | ||
</> | ||
) | ||
|
||
const element = document.querySelector('a') | ||
fireEvent.click(element) | ||
|
||
expect(onScoll).toHaveBeenCalledTimes(1) | ||
expect(onScoll).toHaveBeenCalledWith({ top: 0 }) | ||
}) | ||
|
||
it('should not call window.scroll when no hash-id found', () => { | ||
const onScoll = jest.fn() | ||
|
||
jest.spyOn(window, 'scroll').mockImplementationOnce(onScoll) | ||
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({ | ||
...location, | ||
href: 'http://localhost/path', | ||
}) | ||
|
||
render( | ||
<> | ||
<Anchor onClick={scrollToHashHandler} href="/path/#hash-id"> | ||
text | ||
</Anchor> | ||
<span id="other-id" /> | ||
</> | ||
) | ||
|
||
const element = document.querySelector('a') | ||
fireEvent.click(element) | ||
|
||
expect(onScoll).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('will skip when no # exists in href', () => { | ||
const onScoll = jest.fn() | ||
|
||
jest.spyOn(window, 'scroll').mockImplementationOnce(onScoll) | ||
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({ | ||
...location, | ||
href: 'http://localhost/path', | ||
}) | ||
|
||
render( | ||
<Anchor onClick={scrollToHashHandler} href="/path"> | ||
text | ||
</Anchor> | ||
) | ||
|
||
const element = document.querySelector('a') | ||
fireEvent.click(element) | ||
|
||
expect(onScoll).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should not call window.scroll when not on same page', () => { | ||
const onScoll = jest.fn() | ||
|
||
jest.spyOn(window, 'scroll').mockImplementationOnce(onScoll) | ||
jest.spyOn(window, 'location', 'get').mockReturnValueOnce({ | ||
...location, | ||
href: 'http://localhost/path', | ||
}) | ||
|
||
render( | ||
<> | ||
<Anchor onClick={scrollToHashHandler} href="/other-path/#hash-id"> | ||
text | ||
</Anchor> | ||
<span id="hash-id" /> | ||
</> | ||
) | ||
|
||
const element = document.querySelector('a') | ||
fireEvent.click(element) | ||
|
||
expect(onScoll).toHaveBeenCalledTimes(0) | ||
}) | ||
}) |