2
0

example.test.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { render, screen, fireEvent } from '@testing-library/react'
  2. import { Text, View } from '@tarojs/components'
  3. // 简单的测试组件
  4. const TestComponent = () => {
  5. return (
  6. <View className="test-component">
  7. <Text className="btn">点击我</Text>
  8. </View>
  9. )
  10. }
  11. describe('Taro 组件测试示例', () => {
  12. test('应该正确渲染组件', () => {
  13. render(<TestComponent />)
  14. const button = screen.getByText('点击我')
  15. expect(button).toBeInTheDocument()
  16. expect(button).toHaveClass('btn')
  17. })
  18. test('应该响应点击事件', () => {
  19. const handleClick = jest.fn()
  20. const InteractiveComponent = () => (
  21. <View className="test-component">
  22. <Text className="btn" onClick={handleClick}>点击我</Text>
  23. </View>
  24. )
  25. render(<InteractiveComponent />)
  26. const button = screen.getByText('点击我')
  27. fireEvent.click(button)
  28. expect(handleClick).toHaveBeenCalledTimes(1)
  29. })
  30. test('应该匹配快照', () => {
  31. const { container } = render(<TestComponent />)
  32. expect(container.firstChild).toMatchSnapshot()
  33. })
  34. })