utils.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { render, RenderResult } from '@testing-library/react'
  2. import React from 'react'
  3. export const delay = (ms = 500) => {
  4. return new Promise<void>((resolve) => {
  5. setTimeout(() => {
  6. resolve()
  7. }, ms)
  8. })
  9. }
  10. export function toCamelCase(s: string) {
  11. let camel = ''
  12. let nextCap = false
  13. for (let i = 0; i < s.length; i++) {
  14. if (s[i] !== '-') {
  15. camel += nextCap ? s[i].toUpperCase() : s[i]
  16. nextCap = false
  17. } else {
  18. nextCap = true
  19. }
  20. }
  21. return camel
  22. }
  23. export function capitalize(s: string) {
  24. return s.charAt(0).toUpperCase() + s.slice(1)
  25. }
  26. export function printUnimplementedWarning(node?: Node) {
  27. const name = node?.nodeName.slice(5).replace('-CORE', '').toLowerCase() || 'unknown'
  28. return `H5 暂不支持 ${capitalize(toCamelCase(name))} 组件!`
  29. }
  30. export function parsePx2Number(px: string) {
  31. return Number(px.replace('px', ''))
  32. }
  33. export function parseStyle2String(...styles: Record<string, string | number>[]) {
  34. const style = Object.assign({}, ...styles)
  35. return Object.entries(style).map(([key, value]) => `${key}: ${value};`).join('')
  36. }
  37. // React 测试工具函数
  38. export function renderWithProviders(
  39. ui: React.ReactElement,
  40. options?: any
  41. ): RenderResult {
  42. return render(ui, {
  43. ...options,
  44. })
  45. }
  46. // 模拟事件工具
  47. export const createMockEvent = (type: string, detail?: any) => ({
  48. type,
  49. detail: detail || {},
  50. preventDefault: jest.fn(),
  51. stopPropagation: jest.fn(),
  52. })
  53. // 模拟 Taro 环境
  54. export const mockTaroEnv = () => {
  55. // 模拟 Taro 的 View 组件
  56. jest.doMock('@tarojs/components', () => ({
  57. View: ({ children, ...props }: any) => React.createElement('div', props, children),
  58. Text: ({ children, ...props }: any) => React.createElement('span', props, children),
  59. Image: (props: any) => React.createElement('img', props),
  60. Button: ({ children, ...props }: any) => React.createElement('button', props, children),
  61. }))
  62. }
  63. // 测试数据生成器
  64. export const createTestData = {
  65. // 创建选择器数据
  66. selector: (count = 5) => Array.from({ length: count }, (_, i) => `选项${i + 1}`),
  67. // 创建多选择器数据
  68. multiSelector: () => [
  69. ['早餐', '午餐', '晚餐'],
  70. ['米饭', '面条', '馒头'],
  71. ['青菜', '肉类', '海鲜']
  72. ],
  73. // 创建时间数据
  74. time: () => ({
  75. start: '00:00',
  76. end: '23:59',
  77. value: '12:00'
  78. }),
  79. // 创建日期数据
  80. date: () => ({
  81. start: '2020-01-01',
  82. end: '2030-12-31',
  83. value: '2024-01-01'
  84. }),
  85. // 创建地区数据
  86. region: () => [
  87. {
  88. value: '北京市',
  89. code: '110000',
  90. children: [
  91. {
  92. value: '北京市',
  93. code: '110100',
  94. children: [
  95. { value: '东城区', code: '110101' },
  96. { value: '西城区', code: '110102' }
  97. ]
  98. }
  99. ]
  100. }
  101. ]
  102. }
  103. // 模拟 Taro API
  104. export const mockTaroApis = {
  105. navigateTo: jest.fn(),
  106. redirectTo: jest.fn(),
  107. switchTab: jest.fn(),
  108. navigateBack: jest.fn(),
  109. reLaunch: jest.fn(),
  110. showToast: jest.fn(),
  111. showModal: jest.fn(),
  112. showLoading: jest.fn(),
  113. hideLoading: jest.fn(),
  114. request: jest.fn(),
  115. uploadFile: jest.fn(),
  116. downloadFile: jest.fn(),
  117. getStorage: jest.fn(),
  118. setStorage: jest.fn(),
  119. removeStorage: jest.fn(),
  120. clearStorage: jest.fn(),
  121. getSystemInfo: jest.fn(),
  122. getNetworkType: jest.fn(),
  123. onNetworkStatusChange: jest.fn(),
  124. offNetworkStatusChange: jest.fn(),
  125. }
  126. // 组件测试辅助函数
  127. export const createTestComponent = (Component: React.ComponentType<any>, props: any = {}) => {
  128. return React.createElement(Component, props)
  129. }