-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4006 from ProjectMirador/scroll-to-fn
Rewrite ScrollTo as a function.
- Loading branch information
Showing
2 changed files
with
83 additions
and
167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,65 @@ | ||
import { render } from 'test-utils'; | ||
import { render, screen } from 'test-utils'; | ||
import { createRef } from 'react'; | ||
import { ScrollTo } from '../../../src/components/ScrollTo'; | ||
|
||
describe('ScrollTo', () => { | ||
let containerRef; | ||
const originalGetBoundingClientRect = Element.prototype.getBoundingClientRect; | ||
const originalScrollTo = Element.prototype.scrollTo; | ||
const originalOffsetTop = Element.prototype.offsetTop; | ||
|
||
const containerBoundingRect = { bottom: 500, height: 440, top: 0 }; | ||
let scrollTo; | ||
beforeEach(() => { | ||
scrollTo = jest.fn(); | ||
containerRef = createRef(); | ||
render(<div data-testid="container" ref={containerRef} />); | ||
Element.prototype.getBoundingClientRect = function mockBoundingClientRect() { | ||
if (this.dataset.mockboundingrect) { | ||
return JSON.parse(this.dataset.mockboundingrect); | ||
} | ||
|
||
containerRef.current = { | ||
getBoundingClientRect: () => containerBoundingRect, | ||
getElementsByClassName: () => [{ scrollTo }], | ||
return originalGetBoundingClientRect.call(this); | ||
}; | ||
}); | ||
|
||
const scrollToElAboveBoundingRect = { bottom: -200, top: -300 }; | ||
const scrollToElBelowBoundingRect = { bottom: 601, top: 501 }; | ||
const visibleScrollToElBoundingRect = { bottom: 300, top: 200 }; | ||
|
||
describe('when updating the scrollTo prop', () => { | ||
beforeEach(() => { | ||
jest.spyOn(ScrollTo.prototype, 'elementToScrollTo').mockImplementation(() => ({ offsetTop: 450 })); | ||
}); | ||
describe('when setting from true to false', () => { | ||
it('does not scroll to the selected element', () => { | ||
jest.spyOn(ScrollTo.prototype, 'scrollToBoundingRect').mockImplementation(() => ({ | ||
...scrollToElAboveBoundingRect, | ||
})); | ||
|
||
const { rerender } = render(<ScrollTo scrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
|
||
// It is called once when initially rendered w/ true | ||
expect(scrollTo).toHaveBeenCalled(); | ||
scrollTo.mockReset(); | ||
|
||
rerender(<ScrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
|
||
// But it is not called on the re-render w/ false | ||
expect(scrollTo).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('when set from false to true', () => { | ||
it('scrolls to the selected element when it is hidden above the container', () => { | ||
jest.spyOn(ScrollTo.prototype, 'scrollToBoundingRect').mockImplementation(() => ({ | ||
...scrollToElAboveBoundingRect, | ||
})); | ||
const { rerender } = render(<ScrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
|
||
rerender(<ScrollTo scrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
Element.prototype.scrollTo = jest.fn(); | ||
}); | ||
|
||
expect(scrollTo).toHaveBeenCalledWith(0, 230); | ||
}); | ||
afterEach(() => { | ||
Element.prototype.getBoundingClientRect = originalGetBoundingClientRect; | ||
Element.prototype.scrollTo = originalScrollTo; | ||
Element.prototype.offsetTop = originalOffsetTop; | ||
}); | ||
|
||
it('scrolls to the selected element when it is hidden below the container', () => { | ||
jest.spyOn(ScrollTo.prototype, 'scrollToBoundingRect').mockImplementation(() => ({ | ||
...scrollToElBelowBoundingRect, | ||
})); | ||
it('renders the children', () => { | ||
render(<ScrollTo><div data-testid="a" /></ScrollTo>); | ||
|
||
const { rerender } = render(<ScrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
expect(screen.getByTestId('a')).toBeInTheDocument(); | ||
}); | ||
|
||
rerender(<ScrollTo scrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
it('scrolls to the element if it is off-screen ', () => { | ||
const containerRef = createRef(); | ||
|
||
expect(scrollTo).toHaveBeenCalledWith(0, 230); | ||
}); | ||
render( | ||
<div data-testid="container" ref={containerRef} data-mockboundingrect={JSON.stringify({ bottom: 100, height: 100, top: 0 })}> | ||
<div data-testid="scrollableContainer" style={{ height: 100, overflowY: true }} className="mirador-scrollto-scrollable"> | ||
<ScrollTo containerRef={containerRef}><div data-testid="a" style={{ height: 75 }} /></ScrollTo> | ||
<ScrollTo containerRef={containerRef}><div data-testid="b" style={{ height: 75 }} /></ScrollTo> | ||
<ScrollTo containerRef={containerRef} scrollTo><div data-testid="c" data-mockboundingrect={JSON.stringify({ bottom: 225, top: 150 })} style={{ height: 75 }} /></ScrollTo> | ||
</div> | ||
</div>, | ||
); | ||
|
||
it('does not scroll to the selected element when it is visible', () => { | ||
jest.spyOn(ScrollTo.prototype, 'scrollToBoundingRect').mockImplementation(() => ({ | ||
...visibleScrollToElBoundingRect, | ||
})); | ||
expect(Element.prototype.scrollTo).toHaveBeenCalled(); | ||
}); | ||
|
||
const { rerender } = render(<ScrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
it('does nothing if the element is visible', () => { | ||
const containerRef = createRef(); | ||
|
||
rerender(<ScrollTo scrollTo containerRef={containerRef}><div>Child</div></ScrollTo>); | ||
render( | ||
<div data-testid="container" ref={containerRef} data-mockboundingrect={JSON.stringify({ bottom: 100, height: 100, top: 0 })}> | ||
<div data-testid="scrollableContainer" style={{ height: 100, overflowY: true }} className="mirador-scrollto-scrollable"> | ||
<ScrollTo containerRef={containerRef}><div data-testid="a" style={{ height: 75 }} /></ScrollTo> | ||
<ScrollTo containerRef={containerRef}><div data-testid="b" style={{ height: 75 }} /></ScrollTo> | ||
<ScrollTo containerRef={containerRef} scrollTo><div data-testid="c" data-mockboundingrect={JSON.stringify({ bottom: 100, top: 25 })} style={{ height: 75 }} /></ScrollTo> | ||
</div> | ||
</div>, | ||
); | ||
|
||
expect(scrollTo).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
expect(Element.prototype.scrollTo).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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