118 lines
3.7 KiB
JavaScript
118 lines
3.7 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { flushPromises, mount } from '@vue/test-utils'
|
|
import OrganizationsManagement from '@/components/provenance/admin/OrganizationsManagement.vue'
|
|
import vuetify from '@/plugins/vuetify'
|
|
import { clickBody, setBodyInput } from './helpers'
|
|
|
|
const organizations = [
|
|
{
|
|
id: 1,
|
|
name: 'Red de Economía Solidaria',
|
|
description: 'Red de organizaciones',
|
|
website: 'https://red.example.org',
|
|
contact_email: 'contacto@red.example.org',
|
|
contact_phone: '3001112233',
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'Cooperativa El Campo',
|
|
description: null,
|
|
website: null,
|
|
contact_email: null,
|
|
contact_phone: null,
|
|
},
|
|
]
|
|
|
|
function mockApi () {
|
|
return {
|
|
getOrganizations: vi.fn().mockResolvedValue(organizations),
|
|
createOrganization: vi.fn().mockResolvedValue({}),
|
|
updateOrganization: vi.fn().mockResolvedValue({}),
|
|
deleteOrganization: vi.fn().mockResolvedValue({}),
|
|
}
|
|
}
|
|
|
|
function mountComponent (api) {
|
|
return mount(OrganizationsManagement, {
|
|
global: { plugins: [vuetify], provide: { api } },
|
|
})
|
|
}
|
|
|
|
function fillForm () {
|
|
setBodyInput('[data-testid="org-form-name"] input', 'Fundación Montaña')
|
|
setBodyInput('[data-testid="org-form-description"] textarea', 'Fundación')
|
|
setBodyInput('[data-testid="org-form-website"] input', 'https://montana.example.org')
|
|
setBodyInput('[data-testid="org-form-email"] input', 'info@montana.example.org')
|
|
setBodyInput('[data-testid="org-form-phone"] input', '3109876543')
|
|
}
|
|
|
|
describe('OrganizationsManagement', () => {
|
|
it('carga y muestra las organizaciones', async () => {
|
|
const api = mockApi()
|
|
const wrapper = mountComponent(api)
|
|
await flushPromises()
|
|
|
|
expect(api.getOrganizations).toHaveBeenCalled()
|
|
expect(wrapper.text()).toContain('Red de Economía Solidaria')
|
|
expect(wrapper.text()).toContain('Cooperativa El Campo')
|
|
})
|
|
|
|
it('filtra las organizaciones por búsqueda', async () => {
|
|
const wrapper = mountComponent(mockApi())
|
|
await flushPromises()
|
|
|
|
await wrapper.find('[data-testid="org-search"] input').setValue('Cooperativa')
|
|
|
|
expect(wrapper.text()).toContain('Cooperativa El Campo')
|
|
expect(wrapper.text()).not.toContain('Red de Economía Solidaria')
|
|
})
|
|
|
|
it('crea una organización', async () => {
|
|
const api = mockApi()
|
|
const wrapper = mountComponent(api)
|
|
await flushPromises()
|
|
|
|
await wrapper.find('[data-testid="org-create"]').trigger('click')
|
|
fillForm()
|
|
clickBody('[data-testid="org-save"]')
|
|
await flushPromises()
|
|
|
|
expect(api.createOrganization).toHaveBeenCalledWith({
|
|
name: 'Fundación Montaña',
|
|
description: 'Fundación',
|
|
website: 'https://montana.example.org',
|
|
contact_email: 'info@montana.example.org',
|
|
contact_phone: '3109876543',
|
|
})
|
|
expect(api.getOrganizations).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('edita una organización', async () => {
|
|
const api = mockApi()
|
|
const wrapper = mountComponent(api)
|
|
await flushPromises()
|
|
|
|
await wrapper.find('[data-testid="org-edit-1"]').trigger('click')
|
|
setBodyInput('[data-testid="org-form-name"] input', 'Red de Economía Solidaria Colombia')
|
|
clickBody('[data-testid="org-save"]')
|
|
await flushPromises()
|
|
|
|
expect(api.updateOrganization).toHaveBeenCalledWith(1, expect.objectContaining({
|
|
name: 'Red de Economía Solidaria Colombia',
|
|
}))
|
|
})
|
|
|
|
it('elimina una organización', async () => {
|
|
const api = mockApi()
|
|
const wrapper = mountComponent(api)
|
|
await flushPromises()
|
|
|
|
await wrapper.find('[data-testid="org-delete-2"]').trigger('click')
|
|
clickBody('[data-testid="org-confirm-delete"]')
|
|
await flushPromises()
|
|
|
|
expect(api.deleteOrganization).toHaveBeenCalledWith(2)
|
|
expect(api.getOrganizations).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|