| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import React from 'react'
- import { render, screen, fireEvent, waitFor } from '@testing-library/react'
- import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
- import { AreaPicker } from '../../src/components/AreaPicker'
- // Mock API 客户端
- const mockAreaClient = {
- provinces: {
- $get: jest.fn()
- },
- cities: {
- $get: jest.fn()
- },
- districts: {
- $get: jest.fn()
- }
- }
- jest.mock('../../src/api', () => ({
- areaClient: mockAreaClient
- }))
- // 创建测试用的 QueryClient
- const createTestQueryClient = () => new QueryClient({
- defaultOptions: {
- queries: {
- retry: false,
- },
- },
- })
- // 包装组件
- const Wrapper = ({ children }: { children: React.ReactNode }) => {
- const queryClient = createTestQueryClient()
- return (
- <QueryClientProvider client={queryClient}>
- {children}
- </QueryClientProvider>
- )
- }
- // 模拟数据
- const mockProvinces = {
- success: true,
- data: {
- provinces: [
- { id: 1, name: '北京市' },
- { id: 2, name: '上海市' },
- { id: 3, name: '广东省' }
- ]
- },
- message: ''
- }
- const mockCities = {
- success: true,
- data: {
- cities: [
- { id: 11, name: '北京市' },
- { id: 12, name: '朝阳区' },
- { id: 13, name: '海淀区' }
- ]
- },
- message: ''
- }
- const mockDistricts = {
- success: true,
- data: {
- districts: [
- { id: 101, name: '朝阳区' },
- { id: 102, name: '海淀区' },
- { id: 103, name: '西城区' }
- ]
- },
- message: ''
- }
- describe('AreaPicker 组件', () => {
- beforeEach(() => {
- // 重置所有 mock
- jest.clearAllMocks()
- // 设置默认的 mock 返回值
- mockAreaClient.provinces.$get.mockResolvedValue({
- status: 200,
- json: async () => mockProvinces
- })
- mockAreaClient.cities.$get.mockResolvedValue({
- status: 200,
- json: async () => mockCities
- })
- mockAreaClient.districts.$get.mockResolvedValue({
- status: 200,
- json: async () => mockDistricts
- })
- })
- test('应该正确渲染组件', async () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- />
- </Wrapper>
- )
- // 检查标题
- expect(screen.getByText('选择地区')).toBeInTheDocument()
- // 检查选择器标签
- expect(screen.getByText('省份')).toBeInTheDocument()
- expect(screen.getByText('城市')).toBeInTheDocument()
- expect(screen.getByText('区县')).toBeInTheDocument()
- // 检查按钮
- expect(screen.getByText('取消')).toBeInTheDocument()
- expect(screen.getByText('确定')).toBeInTheDocument()
- // 等待数据加载
- await waitFor(() => {
- expect(mockAreaClient.provinces.$get).toHaveBeenCalled()
- })
- })
- test('应该显示自定义标题', () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- title="选择出发地"
- />
- </Wrapper>
- )
- expect(screen.getByText('选择出发地')).toBeInTheDocument()
- })
- test('应该初始化选择值', async () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- value={[1, 11, 101]}
- />
- </Wrapper>
- )
- // 等待数据加载
- await waitFor(() => {
- expect(mockAreaClient.provinces.$get).toHaveBeenCalled()
- })
- // 检查是否调用了城市和区县查询
- await waitFor(() => {
- expect(mockAreaClient.cities.$get).toHaveBeenCalledWith({
- query: { provinceId: 1, page: 1, pageSize: 50 }
- })
- })
- await waitFor(() => {
- expect(mockAreaClient.districts.$get).toHaveBeenCalledWith({
- query: { cityId: 11, page: 1, pageSize: 50 }
- })
- })
- })
- test('应该处理取消操作', () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- />
- </Wrapper>
- )
- const cancelButton = screen.getByText('取消')
- fireEvent.click(cancelButton)
- expect(onClose).toHaveBeenCalledTimes(1)
- expect(onConfirm).not.toHaveBeenCalled()
- })
- test('应该处理确认操作', async () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- />
- </Wrapper>
- )
- // 等待数据加载
- await waitFor(() => {
- expect(mockAreaClient.provinces.$get).toHaveBeenCalled()
- })
- const confirmButton = screen.getByText('确定')
- fireEvent.click(confirmButton)
- // 由于没有选择任何值,应该传递空数组
- expect(onConfirm).toHaveBeenCalledWith([], [])
- expect(onClose).toHaveBeenCalledTimes(1)
- })
- test('当不可见时应该返回 null', () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- const { container } = render(
- <Wrapper>
- <AreaPicker
- visible={false}
- onClose={onClose}
- onConfirm={onConfirm}
- />
- </Wrapper>
- )
- // 检查容器是否为空
- expect(container.firstChild).toBeNull()
- })
- test('应该处理 API 错误', async () => {
- // 模拟 API 错误
- mockAreaClient.provinces.$get.mockResolvedValue({
- status: 500,
- json: async () => ({ success: false, message: '服务器错误' })
- })
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- />
- </Wrapper>
- )
- // 组件应该正常渲染,即使 API 调用失败
- expect(screen.getByText('选择地区')).toBeInTheDocument()
- // 等待 API 调用
- await waitFor(() => {
- expect(mockAreaClient.provinces.$get).toHaveBeenCalled()
- })
- })
- test('应该正确显示已选择的省市区文本', async () => {
- const onClose = jest.fn()
- const onConfirm = jest.fn()
- render(
- <Wrapper>
- <AreaPicker
- visible={true}
- onClose={onClose}
- onConfirm={onConfirm}
- value={[1, 11, 101]}
- />
- </Wrapper>
- )
- // 等待数据加载
- await waitFor(() => {
- expect(mockAreaClient.provinces.$get).toHaveBeenCalled()
- })
- await waitFor(() => {
- expect(mockAreaClient.cities.$get).toHaveBeenCalled()
- })
- await waitFor(() => {
- expect(mockAreaClient.districts.$get).toHaveBeenCalled()
- })
- // 检查显示文本
- await waitFor(() => {
- expect(screen.getByText('北京市 北京市 朝阳区')).toBeInTheDocument()
- })
- })
- })
|