import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { useNews } from '../use-news' import { formatUnitTime } from '#lib/utils' import { DATE_TIME_FORMAT, SLUG_CATEGORY_NEWS } from '#lib/constants' const mockNadal = { news: { posts: vi.fn(), }, } const mockRoute = { query: {} as any, params: {} as any, path: '', } // Mock dependencies vi.mock('nuxt/app', () => ({ useRoute: () => mockRoute, })) vi.mock('#lib/composables', () => ({ useNadal: () => mockNadal, })) vi.mock('#imports', () => ({ useDevice: vi.fn().mockReturnValue({ isMobile: false, }), })) describe('useNews composable', () => { beforeEach(() => { mockRoute.query = {} }) afterEach(() => { // Reset all mocks after each test vi.clearAllMocks() }) it('should initialize categories and the current category index', () => { mockRoute.query['cat'] = 'news' const { categories, currentCategoryIndex, categorySlugCurrent } = useNews([]) expect(categories).toEqual([]) // Default value or whatever you expect it to be expect(currentCategoryIndex.value).toBe(0) expect(categorySlugCurrent.value).toEqual('news') }) it('should handle pagination correctly', async () => { const mockCategory = { slug: SLUG_CATEGORY_NEWS.ALL, name: 'Tất cả', subCategory: ['1'], } const mockResponse = { total: '10', data: [ { id: 1, title: 'Test Post', updated_date: 1620000000, slug: 'test-post', }, ], } mockNadal.news.posts.mockResolvedValue(mockResponse) const { handlePage, currentPage, totalPost, isLoading } = useNews([ mockCategory, ]) const dataNews: any[] = [] await handlePage(2, dataNews, 4, SLUG_CATEGORY_NEWS.ALL) expect(isLoading.value).toBe(false) expect(currentPage.value).toBe(2) expect(totalPost.value).toBe(10) expect(dataNews).toEqual([ { category: mockCategory, data: [ { id: 1, slug: 'test-post.html', title: 'Test Post', date: formatUnitTime( 1620000000, DATE_TIME_FORMAT.SHORT_DATE_FORMAT, ), updated_date: 1620000000, time: formatUnitTime(1620000000, DATE_TIME_FORMAT.HOUR_MINUTE), image: '', category: '', desc: '', }, ], }, ]) }) it('should fetch all category data correctly', async () => { const mockCategory = { slug: SLUG_CATEGORY_NEWS.ALL, name: 'Tất cả', subCategory: ['1'], } const mockResponse = { total: '10', data: [ { id: 1, title: 'Test Post', updated_date: 1620000000, slug: 'test-post', }, ], } mockNadal.news.posts.mockResolvedValue(mockResponse) const { fetchAllCategoryData, isLoading } = useNews([mockCategory]) const dataNews: any[] = [] await fetchAllCategoryData(dataNews, 0, 4, SLUG_CATEGORY_NEWS.ALL, false) expect(isLoading.value).toBe(false) expect(dataNews).toEqual([ { category: mockCategory, data: [ { id: 1, slug: 'test-post.html', title: 'Test Post', date: formatUnitTime( 1620000000, DATE_TIME_FORMAT.SHORT_DATE_FORMAT, ), updated_date: 1620000000, time: formatUnitTime(1620000000, DATE_TIME_FORMAT.HOUR_MINUTE), image: '', category: '', desc: '', }, ], }, ]) }) it('should compute the current category slug from the route', () => { mockRoute.query.cat = 'news' const { categorySlugCurrent } = useNews() expect(categorySlugCurrent.value).toBe('news') }) })