-
Notifications
You must be signed in to change notification settings - Fork 1
/
happydom.tsx
53 lines (46 loc) · 1.19 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
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,
}
})