返回 oh-my-ppt
style-select.test.ts
根目录 / tests / unit / styles / style-select.test.ts
1 /**
2 * @vitest-environment happy-dom
3 */
4 import React, { act } from 'react'
5 import { afterEach, describe, expect, it, vi } from 'vitest'
6 import { createRoot, type Root } from 'react-dom/client'
7 import { StyleSelect } from '../../../src/renderer/src/components/style/StyleSelect'
8
9 vi.mock('@renderer/i18n', () => ({ useT: () => (key: string) => key }))
10 vi.mock('@renderer/lib/ipc', () => ({
11 ipc: {
12 onHtmlThumbnailChanged: vi.fn(() => () => undefined)
13 }
14 }))
15
16 async function renderStyleSelect(): Promise<{ container: HTMLDivElement; root: Root }> {
17 const container = document.createElement('div')
18 document.body.appendChild(container)
19 const root = createRoot(container)
20 await act(async () => {
21 root.render(
22 React.createElement(StyleSelect, {
23 value: 'normal',
24 onChange: vi.fn(),
25 options: [
26 {
27 id: 'normal',
28 label: 'Normal Style',
29 description: 'Regular option'
30 },
31 {
32 id: 'favorite-old',
33 label: 'Favorite Old',
34 description: 'Older favorite',
35 favoriteAt: 10
36 },
37 {
38 id: 'favorite-new',
39 label: 'Favorite New',
40 description: 'Newer favorite',
41 favoriteAt: 20
42 }
43 ]
44 })
45 )
46 })
47 return { container, root }
48 }
49
50 describe('StyleSelect', () => {
51 afterEach(() => {
52 vi.restoreAllMocks()
53 document.body.innerHTML = ''
54 })
55
56 it('shows favorite styles first with a filled star', async () => {
57 const { container, root } = await renderStyleSelect()
58 try {
59 await act(async () => {
60 container.querySelector('button')?.click()
61 await new Promise((resolve) => window.setTimeout(resolve, 5))
62 })
63
64 const optionButtons = Array.from(document.body.querySelectorAll('button')).filter((button) =>
65 /Favorite|Normal/.test(button.textContent || '')
66 )
67 expect(optionButtons.map((button) => button.textContent)).toEqual([
68 'Normal Style',
69 'Favorite NewNewer favorite',
70 'Favorite OldOlder favorite',
71 'Normal StyleRegular option'
72 ])
73
74 const newestFavorite = optionButtons[1]
75 expect(newestFavorite?.querySelector('svg')?.getAttribute('class')).toContain('fill-[#d6a942]')
76 } finally {
77 await act(async () => root.unmount())
78 container.remove()
79 }
80 })
81 })
82
82 lines TYPESCRIPT