import { describe, expect, it, vi } from 'vitest' import { flushPromises, mount } from '@vue/test-utils' import GeographyManagement from '@/components/provenance/admin/GeographyManagement.vue' import vuetify from '@/plugins/vuetify' import { clickBody, setBodyInput } from './helpers' const countries = [ { id: 1, name: 'Colombia', code: 'CO' }, { id: 2, name: 'Venezuela', code: 'VE' }, ] const departments = [ { id: 2, name: 'Cundinamarca', country: 1, country_detail: { id: 1, name: 'Colombia', code: 'CO' } }, { id: 3, name: 'Antioquia', country: 1, country_detail: { id: 1, name: 'Colombia', code: 'CO' } }, ] const municipalities = [ { id: 7, name: 'La Mesa', department: 2, country: 1, department_detail: { id: 2, name: 'Cundinamarca' } }, { id: 8, name: 'San Antonio', department: 3, country: 1, department_detail: { id: 3, name: 'Antioquia' } }, ] function mockApi () { return { getCountries: vi.fn().mockResolvedValue(countries), createCountry: vi.fn().mockResolvedValue({}), updateCountry: vi.fn().mockResolvedValue({}), deleteCountry: vi.fn().mockResolvedValue({}), getDepartments: vi.fn().mockResolvedValue(departments), createDepartment: vi.fn().mockResolvedValue({}), updateDepartment: vi.fn().mockResolvedValue({}), deleteDepartment: vi.fn().mockResolvedValue({}), getMunicipalities: vi.fn().mockResolvedValue(municipalities), createMunicipality: vi.fn().mockResolvedValue({}), updateMunicipality: vi.fn().mockResolvedValue({}), deleteMunicipality: vi.fn().mockResolvedValue({}), } } function mountComponent (api) { return mount(GeographyManagement, { global: { plugins: [vuetify], provide: { api } }, }) } describe('GeographyManagement', () => { it('muestra los países por defecto y permite crear uno', async () => { const api = mockApi() const wrapper = mountComponent(api) await flushPromises() expect(api.getCountries).toHaveBeenCalled() expect(wrapper.text()).toContain('Colombia') await wrapper.find('[data-testid="geo-country-create"]').trigger('click') setBodyInput('[data-testid="geo-country-form-name"] input', 'Ecuador') setBodyInput('[data-testid="geo-country-form-code"] input', 'EC') clickBody('[data-testid="geo-country-save"]') await flushPromises() expect(api.createCountry).toHaveBeenCalledWith({ name: 'Ecuador', code: 'EC' }) expect(api.getCountries).toHaveBeenCalledTimes(2) }) it('cambia a la pestaña de departamentos y crea uno', async () => { const api = mockApi() const wrapper = mountComponent(api) await flushPromises() await wrapper.find('[data-testid="geo-tab-departments"]').trigger('click') await flushPromises() expect(api.getDepartments).toHaveBeenCalled() expect(wrapper.text()).toContain('Cundinamarca') await wrapper.find('[data-testid="geo-department-create"]').trigger('click') setBodyInput('[data-testid="geo-department-form-name"] input', 'Risaralda') const countrySelect = wrapper.findAllComponents({ name: 'VSelect' }) .find(select => select.props('items').some(item => item.id === 2)) await countrySelect.vm.$emit('update:modelValue', 2) clickBody('[data-testid="geo-department-save"]') await flushPromises() expect(api.createDepartment).toHaveBeenCalledWith(expect.objectContaining({ name: 'Risaralda', country: 2, })) expect(api.getDepartments).toHaveBeenCalledTimes(2) }) it('cambia a la pestaña de municipios y crea uno con departamento y país', async () => { const api = mockApi() const wrapper = mountComponent(api) await flushPromises() await wrapper.find('[data-testid="geo-tab-municipalities"]').trigger('click') await flushPromises() expect(api.getMunicipalities).toHaveBeenCalled() expect(wrapper.text()).toContain('San Antonio') await wrapper.find('[data-testid="geo-municipality-create"]').trigger('click') setBodyInput('[data-testid="geo-municipality-form-name"] input', 'Pereira') await wrapper.findAllComponents({ name: 'VAutocomplete' })[0].vm.$emit('update:modelValue', 3) clickBody('[data-testid="geo-municipality-save"]') await flushPromises() expect(api.createMunicipality).toHaveBeenCalledWith(expect.objectContaining({ name: 'Pereira', department: 3, country: 1, })) expect(api.getMunicipalities).toHaveBeenCalledTimes(2) }) })