pages_theme_setting.test.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { JSDOM } from 'jsdom'
  2. import React from 'react'
  3. import {render, fireEvent, within, screen} from '@testing-library/react'
  4. import { ThemeSettingsPage } from "../pages_theme_settings.tsx"
  5. import { ThemeProvider } from "../hooks_sys.tsx"
  6. import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
  7. import {
  8. assertEquals,
  9. assertExists,
  10. assertNotEquals,
  11. assertRejects,
  12. } from "https://deno.land/std@0.217.0/assert/mod.ts";
  13. const queryClient = new QueryClient()
  14. const dom = new JSDOM(`<body></body>`, {
  15. runScripts: "dangerously",
  16. pretendToBeVisual: true,
  17. url: "http://localhost"
  18. });
  19. // 模拟浏览器环境
  20. globalThis.window = dom.window;
  21. globalThis.document = dom.window.document;
  22. // 定义浏览器环境所需的类
  23. globalThis.Element = dom.window.Element;
  24. globalThis.HTMLElement = dom.window.HTMLElement;
  25. globalThis.ShadowRoot = dom.window.ShadowRoot;
  26. globalThis.SVGElement = dom.window.SVGElement;
  27. // 模拟 getComputedStyle
  28. globalThis.getComputedStyle = (elt) => {
  29. const style = new dom.window.CSSStyleDeclaration();
  30. style.getPropertyValue = () => '';
  31. return style;
  32. };
  33. // 模拟matchMedia函数
  34. globalThis.matchMedia = (query) => ({
  35. matches: query.includes('max-width'),
  36. media: query,
  37. onchange: null,
  38. addListener: () => {},
  39. removeListener: () => {},
  40. addEventListener: () => {},
  41. removeEventListener: () => {},
  42. dispatchEvent: () => false,
  43. });
  44. // 模拟动画相关API
  45. globalThis.AnimationEvent = globalThis.AnimationEvent || dom.window.Event;
  46. globalThis.TransitionEvent = globalThis.TransitionEvent || dom.window.Event;
  47. // 模拟requestAnimationFrame
  48. globalThis.requestAnimationFrame = globalThis.requestAnimationFrame || ((cb) => setTimeout(cb, 0));
  49. globalThis.cancelAnimationFrame = globalThis.cancelAnimationFrame || clearTimeout;
  50. // 设置浏览器尺寸相关方法
  51. window.resizeTo = (width, height) => {
  52. window.innerWidth = width || window.innerWidth;
  53. window.innerHeight = height || window.innerHeight;
  54. window.dispatchEvent(new Event('resize'));
  55. };
  56. window.scrollTo = () => {};
  57. const customScreen = within(document.body);
  58. // 使用异步测试处理真实API调用
  59. Deno.test('主题设置页面测试', async (t) => {
  60. // 渲染组件
  61. const {findByRole, debug} = render(
  62. <QueryClientProvider client={queryClient}>
  63. <ThemeProvider>
  64. <ThemeSettingsPage />
  65. </ThemeProvider>
  66. </QueryClientProvider>
  67. )
  68. // debug(await findByRole('radio', { name: /浅色模式/i }))
  69. // 测试1: 渲染基本元素
  70. await t.step('应渲染主题设置标题', async () => {
  71. const title = await customScreen.findByText(/主题设置/i)
  72. assertExists(title, '未找到主题设置标题')
  73. })
  74. // 测试2: 表单初始化状态
  75. await t.step('表单应正确初始化', async () => {
  76. // 检查主题模式选择
  77. const lightRadio = await customScreen.findByRole('radio', { name: /浅色模式/i })
  78. assertExists(lightRadio, '未找到浅色模式单选按钮')
  79. // 检查主题模式标签
  80. const themeModeLabel = await customScreen.findByText(/主题模式/i)
  81. assertExists(themeModeLabel, '未找到主题模式标签')
  82. // // 检查主题模式选择器 - Ant Design 使用 div 包裹 radio 而不是 radiogroup
  83. // const themeModeField = await customScreen.findByTestId('theme-mode-selector')
  84. // assertExists(themeModeField, '未找到主题模式选择器')
  85. })
  86. // 测试3: 配色方案选择
  87. await t.step('应显示配色方案选项', async () => {
  88. // 查找预设配色方案标签
  89. const colorSchemeLabel = await customScreen.findByText('预设配色方案')
  90. assertExists(colorSchemeLabel, '未找到预设配色方案标签')
  91. // 查找配色方案按钮
  92. const colorSchemeButtons = await customScreen.findAllByRole('button')
  93. assertNotEquals(colorSchemeButtons.length, 0, '未找到配色方案按钮')
  94. })
  95. })