import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' import { setActivePinia, createPinia } from 'pinia' import axios from 'axios' import { useProcesoAdmisionStore } from '../../src/store/procesosAdmisionStore' // Mock axios vi.mock('axios') describe('procesosAdmisionStore', () => { let store beforeEach(() => { setActivePinia(createPinia()) store = useProcesoAdmisionStore() vi.stubEnv('VITE_API_URL', 'http://localhost:8000/api') }) afterEach(() => { vi.clearAllMocks() vi.unstubAllEnvs() }) describe('fetchProcesosPublicados', () => { it('should fetch and update procesosPublicados state with returned data', async () => { const mockProcesos = [ { id: 1, titulo: 'Proceso Ordinario 2026', slug: 'proceso-ordinario-2026', publicado: true, estado: 'publicado', detalles: [ { tipo: 'requisitos', titulo_detalle: 'Requisitos', descripcion: 'Lista de requisitos' } ] }, { id: 2, titulo: 'Proceso Extraordinario 2026', slug: 'proceso-extraordinario-2026', publicado: true, estado: 'en_proceso', detalles: [] } ] axios.get.mockResolvedValueOnce({ data: mockProcesos }) // Initial state should be empty expect(store.procesosPublicados).toEqual([]) expect(store.loading).toBe(false) // Call the action const result = await store.fetchProcesosPublicados() // Verify the result expect(result).toBe(true) expect(store.procesosPublicados).toEqual(mockProcesos) expect(store.procesosPublicados).toHaveLength(2) expect(store.loading).toBe(false) expect(store.error).toBeNull() }) it('should set loading to true while fetching', async () => { let loadingDuringFetch = false axios.get.mockImplementation(() => { loadingDuringFetch = store.loading return Promise.resolve({ data: [] }) }) await store.fetchProcesosPublicados() expect(loadingDuringFetch).toBe(true) }) it('should handle empty response array', async () => { axios.get.mockResolvedValueOnce({ data: [] }) const result = await store.fetchProcesosPublicados() expect(result).toBe(true) expect(store.procesosPublicados).toEqual([]) expect(store.error).toBeNull() }) it('should handle non-array response by setting empty array', async () => { axios.get.mockResolvedValueOnce({ data: null }) const result = await store.fetchProcesosPublicados() expect(result).toBe(true) expect(store.procesosPublicados).toEqual([]) }) it('should set error state when fetch fails', async () => { const errorMessage = 'Network Error' axios.get.mockRejectedValueOnce(new Error(errorMessage)) const result = await store.fetchProcesosPublicados() expect(result).toBe(false) expect(store.error).toBe(errorMessage) expect(store.loading).toBe(false) }) it('should handle API error response with message', async () => { axios.get.mockRejectedValueOnce({ response: { data: { message: 'Server error occurred' }, status: 500 } }) const result = await store.fetchProcesosPublicados() expect(result).toBe(false) expect(store.error).toBe('Server error occurred') }) it('should call the correct API endpoint', async () => { axios.get.mockResolvedValueOnce({ data: [] }) await store.fetchProcesosPublicados() expect(axios.get).toHaveBeenCalledTimes(1) expect(axios.get).toHaveBeenCalledWith( expect.stringContaining('/procesos-admision/publicados') ) }) it('should clear previous error before fetching', async () => { // Set an error first store.error = 'Previous error' axios.get.mockResolvedValueOnce({ data: [] }) await store.fetchProcesosPublicados() expect(store.error).toBeNull() }) it('should update state with processes containing detalles', async () => { const mockProcesos = [ { id: 1, titulo: 'Proceso con Detalles', slug: 'proceso-con-detalles', publicado: true, detalles: [ { tipo: 'requisitos', titulo_detalle: 'Requisitos', descripcion: 'Requisito 1' }, { tipo: 'pagos', titulo_detalle: 'Pagos', descripcion: 'Pago info' }, { tipo: 'vacantes', titulo_detalle: 'Vacantes', descripcion: '100 vacantes' }, { tipo: 'cronograma', titulo_detalle: 'Cronograma', descripcion: 'Fechas importantes' } ] } ] axios.get.mockResolvedValueOnce({ data: mockProcesos }) await store.fetchProcesosPublicados() expect(store.procesosPublicados[0].detalles).toHaveLength(4) expect(store.procesosPublicados[0].detalles[0].tipo).toBe('requisitos') }) }) })