basic.test.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react'
  2. import { render } from '@testing-library/react'
  3. import CartPage from '@/pages/cart/index'
  4. // Mock Taro相关API
  5. jest.mock('@tarojs/taro', () => ({
  6. default: {
  7. navigateBack: jest.fn(),
  8. navigateTo: jest.fn(),
  9. showToast: jest.fn(),
  10. showModal: jest.fn(),
  11. getStorageSync: jest.fn(),
  12. setStorageSync: jest.fn(),
  13. },
  14. }))
  15. // Mock购物车hook
  16. jest.mock('@/utils/cart', () => ({
  17. useCart: () => ({
  18. cart: {
  19. items: [
  20. {
  21. id: 1,
  22. name: '测试商品',
  23. price: 29.9,
  24. image: 'test-image.jpg',
  25. stock: 10,
  26. quantity: 2,
  27. spec: '红色/M',
  28. },
  29. ],
  30. totalAmount: 59.8,
  31. totalCount: 2,
  32. },
  33. updateQuantity: jest.fn(),
  34. removeFromCart: jest.fn(),
  35. clearCart: jest.fn(),
  36. isLoading: false,
  37. }),
  38. }))
  39. // Mock布局组件
  40. jest.mock('@/layouts/tab-bar-layout', () => ({
  41. TabBarLayout: ({ children }: any) => <div data-testid="tabbar-layout">{children}</div>,
  42. }))
  43. // Mock导航栏组件
  44. jest.mock('@/components/ui/navbar', () => ({
  45. Navbar: ({ title }: any) => <div data-testid="navbar">{title}</div>,
  46. }))
  47. // Mock按钮组件
  48. jest.mock('@/components/ui/button', () => ({
  49. Button: ({ children }: any) => <button data-testid="button">{children}</button>,
  50. }))
  51. // Mock图片组件
  52. jest.mock('@/components/ui/image', () => ({
  53. Image: ({ src }: any) => <img src={src} alt="商品图片" data-testid="image" />,
  54. }))
  55. describe('购物车页面基础测试', () => {
  56. it('应该正确渲染购物车页面', () => {
  57. const { getByTestId } = render(<CartPage />)
  58. expect(getByTestId('tabbar-layout')).toBeDefined()
  59. expect(getByTestId('navbar')).toBeDefined()
  60. })
  61. it('应该显示购物车标题', () => {
  62. const { getByTestId } = render(<CartPage />)
  63. const navbar = getByTestId('navbar')
  64. expect(navbar.textContent).toBe('购物车')
  65. })
  66. })