pages_know_info.test.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { JSDOM } from 'jsdom'
  2. import React from 'react'
  3. import {render, fireEvent, within, screen, waitFor} from '@testing-library/react'
  4. import { KnowInfoPage } from "./pages_know_info.tsx"
  5. import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
  6. import {
  7. assertEquals,
  8. assertExists,
  9. assertNotEquals,
  10. assertRejects,
  11. } from "https://deno.land/std@0.217.0/assert/mod.ts";
  12. const queryClient = new QueryClient()
  13. const dom = new JSDOM(`<body></body>`, {
  14. runScripts: "dangerously",
  15. pretendToBeVisual: true,
  16. url: "http://localhost"
  17. });
  18. // 模拟浏览器环境
  19. globalThis.window = dom.window;
  20. globalThis.document = dom.window.document;
  21. // 定义浏览器环境所需的类
  22. globalThis.Element = dom.window.Element;
  23. globalThis.HTMLElement = dom.window.HTMLElement;
  24. globalThis.ShadowRoot = dom.window.ShadowRoot;
  25. globalThis.SVGElement = dom.window.SVGElement;
  26. // 模拟 getComputedStyle
  27. globalThis.getComputedStyle = (elt) => {
  28. const style = new dom.window.CSSStyleDeclaration();
  29. style.getPropertyValue = () => '';
  30. return style;
  31. };
  32. // 模拟matchMedia函数
  33. globalThis.matchMedia = (query) => ({
  34. matches: query.includes('max-width'),
  35. media: query,
  36. onchange: null,
  37. addListener: () => {},
  38. removeListener: () => {},
  39. addEventListener: () => {},
  40. removeEventListener: () => {},
  41. dispatchEvent: () => false,
  42. });
  43. // 模拟动画相关API
  44. globalThis.AnimationEvent = globalThis.AnimationEvent || dom.window.Event;
  45. globalThis.TransitionEvent = globalThis.TransitionEvent || dom.window.Event;
  46. // 模拟requestAnimationFrame
  47. globalThis.requestAnimationFrame = globalThis.requestAnimationFrame || ((cb) => setTimeout(cb, 0));
  48. globalThis.cancelAnimationFrame = globalThis.cancelAnimationFrame || clearTimeout;
  49. // 设置浏览器尺寸相关方法
  50. window.resizeTo = (width, height) => {
  51. window.innerWidth = width || window.innerWidth;
  52. window.innerHeight = height || window.innerHeight;
  53. window.dispatchEvent(new Event('resize'));
  54. };
  55. window.scrollTo = () => {};
  56. const customScreen = within(document.body);
  57. // 使用异步测试处理组件渲染
  58. Deno.test('知识库管理页面测试', async (t) => {
  59. // 渲染组件
  60. const {
  61. findByText, findByPlaceholderText, queryByText,
  62. findByRole, findAllByRole, findByLabelText, findAllByText
  63. } = render(
  64. <QueryClientProvider client={queryClient}>
  65. <KnowInfoPage />
  66. </QueryClientProvider>
  67. );
  68. // 测试1: 基本渲染
  69. await t.step('应正确渲染页面元素', async () => {
  70. const title = await findByText(/知识库管理/i);
  71. assertExists(title, '未找到知识库管理标题');
  72. });
  73. // 测试2: 搜索表单功能
  74. await t.step('搜索表单应正常工作', async () => {
  75. const searchInput = await findByPlaceholderText(/请输入文章标题/i);
  76. const searchButton = await findByText(/搜索/i);
  77. // 输入搜索内容
  78. fireEvent.change(searchInput, { target: { value: '测试' } });
  79. assertEquals(searchInput.getAttribute('value'), '测试', '搜索输入框值未更新');
  80. // 提交搜索
  81. fireEvent.click(searchButton);
  82. // 验证是否触发了搜索
  83. await waitFor(() => {
  84. const loading = queryByText(/加载中/i);
  85. assertNotEquals(loading, null, '搜索未触发加载状态');
  86. });
  87. });
  88. // 测试3: 表格数据加载
  89. await t.step('表格应加载并显示数据', async () => {
  90. // 等待数据加载
  91. await waitFor(() => {
  92. const loading = queryByText(/加载中/i);
  93. assertEquals(loading, null, '数据加载未完成');
  94. });
  95. // 验证表格存在
  96. const table = await findByRole('table');
  97. assertExists(table, '未找到数据表格');
  98. });
  99. // 测试4: 添加文章功能
  100. await t.step('应能打开添加文章模态框', async () => {
  101. const addButton = await findByText(/添加文章/i);
  102. fireEvent.click(addButton);
  103. const modalTitle = await findByText(/添加知识库文章/i);
  104. assertExists(modalTitle, '未找到添加文章模态框');
  105. // 验证表单字段
  106. const titleInput = await findByLabelText(/文章标题/i);
  107. assertExists(titleInput, '未找到标题输入框');
  108. });
  109. // 测试5: 分页功能
  110. await t.step('应显示分页控件', async () => {
  111. const pagination = await findByRole('navigation');
  112. assertExists(pagination, '未找到分页控件');
  113. const pageItems = await findAllByRole('button', { name: /1|2|3|下一页|上一页/i });
  114. assertNotEquals(pageItems.length, 0, '未找到分页按钮');
  115. });
  116. // 测试6: 操作按钮
  117. await t.step('应显示操作按钮', async () => {
  118. const editButtons = await findAllByText(/编辑/i);
  119. assertNotEquals(editButtons.length, 0, '未找到编辑按钮');
  120. const deleteButtons = await findAllByText(/删除/i);
  121. assertNotEquals(deleteButtons.length, 0, '未找到删除按钮');
  122. });
  123. });