-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathScrollTo.test.jsx
More file actions
65 lines (52 loc) · 2.74 KB
/
ScrollTo.test.jsx
File metadata and controls
65 lines (52 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { render, screen } from '@tests/utils/test-utils';
import { createRef } from 'react';
import { ScrollTo } from '../../../src/components/ScrollTo';
describe('ScrollTo', () => {
const originalGetBoundingClientRect = Element.prototype.getBoundingClientRect;
const originalScrollTo = Element.prototype.scrollTo;
const originalOffsetTop = Element.prototype.offsetTop;
beforeEach(() => {
Element.prototype.getBoundingClientRect = function mockBoundingClientRect() {
if (this.dataset.mockboundingrect) {
return JSON.parse(this.dataset.mockboundingrect);
}
return originalGetBoundingClientRect.call(this);
};
Element.prototype.scrollTo = vi.fn();
});
afterEach(() => {
Element.prototype.getBoundingClientRect = originalGetBoundingClientRect;
Element.prototype.scrollTo = originalScrollTo;
Element.prototype.offsetTop = originalOffsetTop;
});
it('renders the children', () => {
render(<ScrollTo><div data-testid="a" /></ScrollTo>);
expect(screen.getByTestId('a')).toBeInTheDocument();
});
it('scrolls to the element if it is off-screen ', () => {
const containerRef = createRef();
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>,
);
expect(Element.prototype.scrollTo).toHaveBeenCalled();
});
it('does nothing if the element is visible', () => {
const containerRef = createRef();
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(Element.prototype.scrollTo).not.toHaveBeenCalled();
});
});