theme_setting_page.test.tsx 3.8 KB

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