Open History to see the last commit
1import '@testing-library/jest-dom/vitest'
2import { cleanup } from '@solidjs/testing-library'
3import { afterEach, beforeEach, vi } from 'vitest'
4β
5const scrollToDescriptor = Object.getOwnPropertyDescriptor(
6 HTMLElement.prototype,
7 'scrollTo',
8)
9const isList = (element: HTMLElement) =>
10 element.matches(
11 '.tsqd-queries-overflow-container, .tsqd-mutations-overflow-container, .test-virtual-list',
12 )
13β
14beforeEach(() => {
15 vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(
16 function (this: HTMLElement) {
17 return isList(this) ? 410 : 0
18 },
19 )
20 vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(
21 function (this: HTMLElement) {
22 return isList(this) ? 1000 : 0
23 },
24 )
25 vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(
26 function (this: HTMLElement) {
27 return this.hasAttribute('data-index')
28 ? Number(this.firstElementChild?.getAttribute('data-height') ?? 28)
29 : 0
30 },
31 )
32 vi.spyOn(HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(
33 function (this: HTMLElement) {
34 if (!isList(this)) return 0
35 const content = this.firstElementChild
36 return content instanceof HTMLElement
37 ? Number.parseFloat(content.style.height) || 0
38 : 0
39 },
40 )
41 Object.defineProperty(HTMLElement.prototype, 'scrollTo', {
42 configurable: true,
43 value(this: HTMLElement, options: ScrollToOptions) {
44 const next = Math.max(
45 0,
46 Math.min(options.top ?? 0, this.scrollHeight - this.clientHeight),
47 )
48 if (next !== this.scrollTop) {
49 this.scrollTop = next
50 this.dispatchEvent(new Event('scroll'))
51 }
52 },
53 })
54})
55β
56afterEach(() => {
57 cleanup()
58 if (scrollToDescriptor) {
59 Object.defineProperty(HTMLElement.prototype, 'scrollTo', scrollToDescriptor)
60 } else {
61 Reflect.deleteProperty(HTMLElement.prototype, 'scrollTo')
62 }
63})