Files
expo-popcore-app/hooks/use-message-unread-count.test.ts
imeepos 83c3183be8 feat: add message action hooks with TDD
Implement useMessageActions and useMessageUnreadCount hooks following strict TDD principles (RED → GREEN → REFACTOR).

useMessageActions provides:
- markRead(id) - mark single message as read
- batchMarkRead(ids) - batch mark messages as read
- deleteMessage(id) - delete message
- Independent loading/error states for each operation

useMessageUnreadCount provides:
- Fetch total unread count and counts by message type
- refetch() method for manual refresh
- loading and error state management

All operations use MessageController from SDK with proper error handling.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 14:48:27 +08:00

101 lines
2.7 KiB
TypeScript

import { renderHook, act } from '@testing-library/react-native'
import { useMessageUnreadCount } from './use-message-unread-count'
import { root } from '@repo/core'
import { MessageController } from '@repo/sdk'
jest.mock('@repo/core', () => ({
root: {
get: jest.fn(),
},
}))
describe('useMessageUnreadCount', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should return initial state', () => {
const mockMessageController = {
getUnreadCount: jest.fn(),
}
;(root.get as jest.Mock).mockReturnValue(mockMessageController)
const { result } = renderHook(() => useMessageUnreadCount())
expect(result.current.data).toBeUndefined()
expect(result.current.loading).toBe(false)
expect(result.current.error).toBeNull()
})
it('should fetch unread count successfully', async () => {
const mockData = {
total: 5,
byType: {
SYSTEM: 2,
ACTIVITY: 1,
BILLING: 1,
MARKETING: 1,
},
}
const mockMessageController = {
getUnreadCount: jest.fn().mockResolvedValue(mockData),
}
;(root.get as jest.Mock).mockReturnValue(mockMessageController)
const { result } = renderHook(() => useMessageUnreadCount())
await act(async () => {
await result.current.refetch()
})
expect(result.current.data).toEqual(mockData)
expect(result.current.loading).toBe(false)
expect(result.current.error).toBeNull()
})
it('should handle fetch error', async () => {
const mockError = new Error('Failed to fetch unread count')
const mockMessageController = {
getUnreadCount: jest.fn().mockRejectedValue(mockError),
}
;(root.get as jest.Mock).mockReturnValue(mockMessageController)
const { result } = renderHook(() => useMessageUnreadCount())
await act(async () => {
await result.current.refetch()
})
expect(result.current.error).toEqual(mockError)
expect(result.current.data).toBeUndefined()
})
it('should set loading state during fetch', async () => {
let resolveFetch: (value: any) => void
const fetchPromise = new Promise((resolve) => {
resolveFetch = resolve
})
const mockMessageController = {
getUnreadCount: jest.fn().mockReturnValue(fetchPromise),
}
;(root.get as jest.Mock).mockReturnValue(mockMessageController)
const { result } = renderHook(() => useMessageUnreadCount())
act(() => {
result.current.refetch()
})
expect(result.current.loading).toBe(true)
await act(async () => {
resolveFetch!({ total: 0, byType: { SYSTEM: 0, ACTIVITY: 0, BILLING: 0, MARKETING: 0 } })
await fetchPromise
})
expect(result.current.loading).toBe(false)
})
})