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β
14// jsdom has no `adoptedStyleSheets`, which the devtools use to add their styles.
15for (const prototype of [Document.prototype, ShadowRoot.prototype]) {
16 const adopted = new WeakMap<object, Array<CSSStyleSheet>>()
17 Object.defineProperty(prototype, 'adoptedStyleSheets', {
18 configurable: true,
19 get() {
20 return adopted.get(this) ?? []
21 },
22 set(sheets: Array<CSSStyleSheet>) {
23 adopted.set(this, sheets)
24 },
25 })
26}
27β
28beforeEach(() => {
29 vi.stubGlobal('CSS', { ...globalThis.CSS, highlights: new Map() })
30 vi.stubGlobal('Highlight', Set)
31 vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(
32 function (this: HTMLElement) {
33 return isList(this) ? 410 : 0
34 },
35 )
36 vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(
37 function (this: HTMLElement) {
38 return isList(this) ? 1000 : 0
39 },
40 )
41 vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockImplementation(
42 function (this: HTMLElement) {
43 return this.hasAttribute('data-index')
44 ? Number(this.firstElementChild?.getAttribute('data-height') ?? 28)
45 : 0
46 },
47 )
48 vi.spyOn(HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(
49 function (this: HTMLElement) {
50 if (!isList(this)) return 0
51 const content = this.firstElementChild
52 return content instanceof HTMLElement
53 ? Number.parseFloat(content.style.height) || 0
54 : 0
55 },
56 )
57 Object.defineProperty(HTMLElement.prototype, 'scrollTo', {
58 configurable: true,
59 value(this: HTMLElement, options: ScrollToOptions) {
60 const next = Math.max(
61 0,
62 Math.min(options.top ?? 0, this.scrollHeight - this.clientHeight),
63 )
64 if (next !== this.scrollTop) {
65 this.scrollTop = next
66 this.dispatchEvent(new Event('scroll'))
67 }
68 },
69 })
70})
71β
72afterEach(() => {
73 cleanup()
74 vi.unstubAllGlobals()
75 if (scrollToDescriptor) {
76 Object.defineProperty(HTMLElement.prototype, 'scrollTo', scrollToDescriptor)
77 } else {
78 Reflect.deleteProperty(HTMLElement.prototype, 'scrollTo')
79 }
80})