-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitest.setup.tsx
More file actions
123 lines (114 loc) · 2.73 KB
/
Copy pathvitest.setup.tsx
File metadata and controls
123 lines (114 loc) · 2.73 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-FileCopyrightText: 2025-2026 Temple University <kleinweb@temple.edu>
//
// SPDX-License-Identifier: GPL-2.0-or-later
import '@testing-library/jest-dom/vitest'
import {cleanup} from '@testing-library/react'
import {afterEach, vi} from 'vitest'
// Cleanup after each test
afterEach(() => {
cleanup()
})
// Mock WordPress packages
vi.mock('@wordpress/i18n', () => ({
__: (text: string) => text,
_x: (text: string) => text,
_n: (single: string, plural: string, number: number) =>
number === 1 ? single : plural,
sprintf: (format: string, ...args: unknown[]) => {
let i = 0
return format.replace(/%[sd]/g, () => String(args[i++] ?? ''))
},
}))
vi.mock('@wordpress/element', async () => {
const actual = await vi.importActual<typeof import('react')>('react')
return {
...actual,
createInterpolateElement: (text: string) => text,
}
})
vi.mock('@wordpress/data', () => ({
useSelect: vi.fn(() => ({})),
useDispatch: vi.fn(() => ({})),
select: vi.fn(() => ({})),
dispatch: vi.fn(() => ({})),
}))
vi.mock('@wordpress/core-data', () => ({
useEntityProp: vi.fn(() => [{}, vi.fn()]),
}))
vi.mock('@wordpress/block-editor', () => ({
useBlockProps: vi.fn((props = {}) => ({
...props,
className: `wp-block ${props.className || ''}`.trim(),
})),
InspectorControls: ({children}: {children: React.ReactNode}) => children,
RichText: vi.fn(),
}))
vi.mock('@wordpress/components', () => ({
Button: ({
children,
onClick,
}: {
children: React.ReactNode
onClick?: () => void
}) => (
<button onClick={onClick} type="button">
{children}
</button>
),
PanelBody: ({
children,
title,
}: {
children: React.ReactNode
title: string
}) => (
<div data-testid="panel-body" data-title={title}>
{children}
</div>
),
ToggleControl: ({
label,
checked,
onChange,
}: {
label: string
checked: boolean
onChange: (val: boolean) => void
}) => (
<label>
<input
checked={checked}
onChange={e => onChange(e.target.checked)}
type="checkbox"
/>
{label}
</label>
),
SelectControl: ({
label,
value,
options,
onChange,
}: {
label: string
value: string
options: {value: string; label: string}[]
onChange: (val: string) => void
}) => (
<label>
{label}
<select onChange={e => onChange(e.target.value)} value={value}>
{options.map(opt => (
<option key={opt.value} value={opt.value}>
{opt.label}
</option>
))}
</select>
</label>
),
Spinner: () => <div data-testid="spinner">Loading...</div>,
FormTokenField: vi.fn(() => null),
}))
vi.mock('@wordpress/blocks', () => ({
registerBlockType: vi.fn(),
}))