forked from OpenUp-LabTakizawa/caravan-kidstec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhappydom.tsx
71 lines (61 loc) · 1.54 KB
/
happydom.tsx
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
66
67
68
69
70
71
import { mock } from "bun:test"
import { GlobalRegistrator } from "@happy-dom/global-registrator"
import type { ImageProps, StaticImageData } from "next/image"
import type { ComponentType, ReactElement } from "react"
GlobalRegistrator.register()
interface EsModuleDefault<T> {
readonly __esModule: true
readonly default: T
}
type StaticRequire = ImageProps["src"] extends
| infer T
| StaticImageData
| string
? T
: never
const mapStaticImportToSrc = (
staticImport: StaticImageData | StaticRequire,
): string => {
if ("default" in staticImport) {
return staticImport.default.src
}
return staticImport.src
}
const mapNextImageSrcToString = (
src: StaticImageData | StaticRequire | string,
): string => {
if (typeof src === "string") {
return src
}
return mapStaticImportToSrc(src)
}
function MockNextImage({
alt,
height,
src: nextImageSrc,
width,
}: Readonly<ImageProps>): ReactElement {
const imgSrc: string = mapNextImageSrcToString(nextImageSrc)
return <img alt={alt} height={height} src={imgSrc} width={width} />
}
mock.module("next/image", (): EsModuleDefault<ComponentType<ImageProps>> => {
return {
__esModule: true,
default: MockNextImage,
}
})
class IntersectionObserver {
observe = mock()
disconnect = mock()
unobserve = mock()
}
Object.defineProperty(window, "IntersectionObserver", {
writable: true,
configurable: true,
value: IntersectionObserver,
})
Object.defineProperty(global, "IntersectionObserver", {
writable: true,
configurable: true,
value: IntersectionObserver,
})