fix: bug
This commit is contained in:
@@ -1,7 +1,24 @@
|
||||
import React from 'react'
|
||||
import { render, waitFor } from '@testing-library/react-native'
|
||||
import { render, waitFor, fireEvent } from '@testing-library/react-native'
|
||||
import My from '../my'
|
||||
|
||||
// Mock react-native-safe-area-context FIRST
|
||||
jest.mock('react-native-safe-area-context', () => ({
|
||||
SafeAreaProvider: ({ children }: any) => children,
|
||||
SafeAreaView: ({ children }: any) => children,
|
||||
useSafeAreaInsets: () => ({ top: 0, right: 0, bottom: 0, left: 0 }),
|
||||
}))
|
||||
|
||||
// Mock expo-status-bar
|
||||
jest.mock('expo-status-bar', () => ({
|
||||
StatusBar: 'StatusBar',
|
||||
}))
|
||||
|
||||
// Mock expo-image
|
||||
jest.mock('expo-image', () => ({
|
||||
Image: 'Image',
|
||||
}))
|
||||
|
||||
jest.mock('expo-router', () => ({
|
||||
useRouter: () => ({
|
||||
push: jest.fn(),
|
||||
@@ -36,6 +53,40 @@ jest.mock('@/hooks/use-user-balance', () => ({
|
||||
}),
|
||||
}))
|
||||
|
||||
jest.mock('@/components/skeleton/MySkeleton', () => ({
|
||||
MySkeleton: () => 'MySkeleton',
|
||||
}))
|
||||
|
||||
jest.mock('@/components/icon', () => ({
|
||||
PointsIcon: () => 'PointsIcon',
|
||||
SearchIcon: () => 'SearchIcon',
|
||||
SettingsIcon: () => 'SettingsIcon',
|
||||
}))
|
||||
|
||||
jest.mock('@/components/drawer/EditProfileDrawer', () => ({
|
||||
__esModule: true,
|
||||
default: () => 'EditProfileDrawer',
|
||||
}))
|
||||
|
||||
jest.mock('@/components/ui/dropdown', () => ({
|
||||
__esModule: true,
|
||||
default: () => 'Dropdown',
|
||||
}))
|
||||
|
||||
jest.mock('@/components/ui/Toast', () => ({
|
||||
__esModule: true,
|
||||
default: {
|
||||
show: jest.fn(),
|
||||
showLoading: jest.fn(),
|
||||
hideLoading: jest.fn(),
|
||||
showActionSheet: jest.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
jest.mock('expo-image', () => ({
|
||||
Image: 'Image',
|
||||
}))
|
||||
|
||||
const mockRefetch = jest.fn()
|
||||
const mockLoadMore = jest.fn()
|
||||
|
||||
@@ -65,21 +116,66 @@ describe('My Page - Pagination', () => {
|
||||
|
||||
it('should refresh with limit 20 on pull to refresh', async () => {
|
||||
const { useTemplateGenerations } = require('@/hooks')
|
||||
const mockOnRefresh = jest.fn()
|
||||
const mockRefetchAsync = jest.fn().mockResolvedValue({ data: [], error: null })
|
||||
|
||||
useTemplateGenerations.mockReturnValue({
|
||||
generations: [{ id: '1', status: 'completed' }],
|
||||
loading: false,
|
||||
loadingMore: false,
|
||||
refetch: mockRefetch,
|
||||
refetch: mockRefetchAsync,
|
||||
loadMore: mockLoadMore,
|
||||
hasMore: true,
|
||||
})
|
||||
|
||||
render(<My />)
|
||||
const { getByTestId } = render(<My />)
|
||||
|
||||
// 模拟下拉刷新
|
||||
const scrollView = getByTestId('my-scroll-view')
|
||||
const refreshControl = scrollView.props.refreshControl
|
||||
|
||||
// 触发刷新
|
||||
await refreshControl.props.onRefresh()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockRefetch).toHaveBeenCalledWith({ page: 1, limit: 20 })
|
||||
expect(mockRefetchAsync).toHaveBeenCalledWith({ page: 1, limit: 20 })
|
||||
})
|
||||
})
|
||||
|
||||
it('should stop showing refreshing indicator after refresh completes', async () => {
|
||||
const { useTemplateGenerations } = require('@/hooks')
|
||||
let resolveRefetch: any
|
||||
const mockRefetchAsync = jest.fn(() => new Promise((resolve) => {
|
||||
resolveRefetch = resolve
|
||||
}))
|
||||
|
||||
useTemplateGenerations.mockReturnValue({
|
||||
generations: [{ id: '1', status: 'completed' }],
|
||||
loading: false,
|
||||
loadingMore: false,
|
||||
refetch: mockRefetchAsync,
|
||||
loadMore: mockLoadMore,
|
||||
hasMore: true,
|
||||
})
|
||||
|
||||
const { getByTestId } = render(<My />)
|
||||
const scrollView = getByTestId('my-scroll-view')
|
||||
const refreshControl = scrollView.props.refreshControl
|
||||
|
||||
// 开始刷新
|
||||
const refreshPromise = refreshControl.props.onRefresh()
|
||||
|
||||
// 刷新中应该显示 refreshing
|
||||
await waitFor(() => {
|
||||
expect(refreshControl.props.refreshing).toBe(true)
|
||||
})
|
||||
|
||||
// 完成刷新
|
||||
resolveRefetch({ data: [], error: null })
|
||||
await refreshPromise
|
||||
|
||||
// 刷新完成后应该隐藏 refreshing
|
||||
await waitFor(() => {
|
||||
expect(refreshControl.props.refreshing).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -99,10 +195,22 @@ describe('My Page - Pagination', () => {
|
||||
hasMore: true,
|
||||
})
|
||||
|
||||
render(<My />)
|
||||
const { getByTestId } = render(<My />)
|
||||
const scrollView = getByTestId('my-scroll-view')
|
||||
|
||||
// 模拟滚动到底部
|
||||
const scrollEvent = {
|
||||
nativeEvent: {
|
||||
layoutMeasurement: { height: 800 },
|
||||
contentOffset: { y: 1000 },
|
||||
contentSize: { height: 1800 }, // 距离底部 < 100px
|
||||
},
|
||||
}
|
||||
|
||||
fireEvent.scroll(scrollView, scrollEvent)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockRefetch).toHaveBeenCalledWith({ page: 1, limit: 20 })
|
||||
expect(mockLoadMore).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user