example.test.tsx 1.2 KB

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