import { describe, it, expect, vi, beforeEach } from 'vitest' import { useBettingHistory } from '../use-betting-history' import { BETTING_STATUS_INFO, CURRENT_PAGE_DEFAULT, TOTAL_PAGE_DEFAULT, } from '#lib/constants' const mockNadal = { transaction: { betHistories: vi.fn(), } } vi.mock('#imports', () => ({ persistedState: vi.fn().mockReturnValue({ localStorage: {}, }), })) vi.mock('#lib/composables', () => ({ useNadal: () => mockNadal, })) describe('use-betting-history', () => { it('should initialize with default values', () => { const { isLoading, currentPage, isNextPage, limit, totalPost, bettingHistories, } = useBettingHistory() expect(isLoading.value).toBe(false) expect(currentPage.value).toBe(CURRENT_PAGE_DEFAULT) expect(isNextPage.value).toBe(false) expect(limit.value).toBe(0) expect(totalPost.value).toBe(TOTAL_PAGE_DEFAULT) expect(bettingHistories.value).toEqual([]) }) it('should fetch betting history and update state', async () => { const { fetchBetHistory, isLoading, totalPost, bettingHistories } = useBettingHistory() const mockResponse = { data: [{ id: 1 }], total: 10, } mockNadal.transaction.betHistories.mockResolvedValue(mockResponse) const payload = { limit: 5 } const result = await fetchBetHistory(payload) expect(isLoading.value).toBe(false) expect(totalPost.value).toBe(10) expect(bettingHistories.value).toEqual(mockResponse.data) expect(result).toEqual(mockResponse.data) }) it('should handle pagination change', async () => { let { onChangePagination, currentPage, bettingHistories, isLoading, totalPost, } = useBettingHistory() const payload = { limit: 5 } const mockResponse = { data: [{ id: 2 }], total: 10, } mockNadal.transaction.betHistories.mockResolvedValue(mockResponse) await onChangePagination(3, payload) expect(currentPage.value).toBe(3) expect(isLoading.value).toBe(false) expect(totalPost.value).toBe(10) expect(bettingHistories.value).toEqual(mockResponse.data) expect(mockNadal.transaction.betHistories).toHaveBeenCalledWith(payload) }) it('should return correct status for a betting item', () => { const { getStatus } = useBettingHistory() const bettingItem = { ticket_status: 'WIN' } const status = getStatus(bettingItem as any) expect(status).toEqual(BETTING_STATUS_INFO.WIN) }) })