#49 feat: CRUD admin de organizaciones, proveedores y geografía
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import SuppliersManagement from '@/components/provenance/admin/SuppliersManagement.vue'
|
||||
import vuetify from '@/plugins/vuetify'
|
||||
import { clickBody, setBodyInput } from './helpers'
|
||||
|
||||
const organizations = [
|
||||
{ id: 3, name: 'Red de Economía Solidaria' },
|
||||
{ id: 4, name: 'Cooperativa El Campo' },
|
||||
]
|
||||
|
||||
const municipalities = [
|
||||
{ id: 7, name: 'La Mesa' },
|
||||
{ id: 8, name: 'San Antonio' },
|
||||
]
|
||||
|
||||
const suppliers = [
|
||||
{
|
||||
id: 5,
|
||||
name: 'Asociación Agropecuaria La Mesa',
|
||||
description: 'Cooperativa de campesinos',
|
||||
organization: 3,
|
||||
organization_detail: { id: 3, name: 'Red de Economía Solidaria' },
|
||||
municipality: 7,
|
||||
municipality_detail: { id: 7, name: 'La Mesa' },
|
||||
contact_email: 'contacto@agro.example.org',
|
||||
contact_phone: '300 123 4567',
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
name: 'Finca El Paraíso',
|
||||
description: null,
|
||||
organization: null,
|
||||
organization_detail: null,
|
||||
municipality: null,
|
||||
municipality_detail: null,
|
||||
contact_email: null,
|
||||
contact_phone: null,
|
||||
},
|
||||
]
|
||||
|
||||
function mockApi () {
|
||||
return {
|
||||
getSuppliers: vi.fn().mockResolvedValue(suppliers),
|
||||
getOrganizations: vi.fn().mockResolvedValue(organizations),
|
||||
getMunicipalities: vi.fn().mockResolvedValue(municipalities),
|
||||
createSupplier: vi.fn().mockResolvedValue({}),
|
||||
updateSupplier: vi.fn().mockResolvedValue({}),
|
||||
deleteSupplier: vi.fn().mockResolvedValue({}),
|
||||
}
|
||||
}
|
||||
|
||||
function mountComponent (api) {
|
||||
return mount(SuppliersManagement, {
|
||||
global: { plugins: [vuetify], provide: { api } },
|
||||
})
|
||||
}
|
||||
|
||||
describe('SuppliersManagement', () => {
|
||||
it('carga y muestra los proveedores con su organización y municipio', async () => {
|
||||
const api = mockApi()
|
||||
const wrapper = mountComponent(api)
|
||||
await flushPromises()
|
||||
|
||||
expect(api.getSuppliers).toHaveBeenCalled()
|
||||
expect(wrapper.text()).toContain('Asociación Agropecuaria La Mesa')
|
||||
expect(wrapper.text()).toContain('Red de Economía Solidaria')
|
||||
expect(wrapper.text()).toContain('La Mesa')
|
||||
})
|
||||
|
||||
it('crea un proveedor con organización y municipio', async () => {
|
||||
const api = mockApi()
|
||||
const wrapper = mountComponent(api)
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-testid="supplier-create"]').trigger('click')
|
||||
|
||||
setBodyInput('[data-testid="supplier-form-name"] input', 'Asociación El Cedro')
|
||||
setBodyInput('[data-testid="supplier-form-description"] textarea', 'Caficultores')
|
||||
const orgSelect = wrapper.findAllComponents({ name: 'VSelect' })
|
||||
.find(select => select.props('items').some(item => item.id === 4))
|
||||
await orgSelect.vm.$emit('update:modelValue', 4)
|
||||
await wrapper.findAllComponents({ name: 'VAutocomplete' })[0].vm.$emit('update:modelValue', 8)
|
||||
clickBody('[data-testid="supplier-save"]')
|
||||
await flushPromises()
|
||||
|
||||
expect(api.createSupplier).toHaveBeenCalledWith(expect.objectContaining({
|
||||
name: 'Asociación El Cedro',
|
||||
description: 'Caficultores',
|
||||
organization: 4,
|
||||
municipality: 8,
|
||||
}))
|
||||
expect(api.getSuppliers).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('edita un proveedor conservando la organización seleccionada', async () => {
|
||||
const api = mockApi()
|
||||
const wrapper = mountComponent(api)
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-testid="supplier-edit-5"]').trigger('click')
|
||||
setBodyInput('[data-testid="supplier-form-name"] input', 'Asociación Agropecuaria La Mesa Renovada')
|
||||
clickBody('[data-testid="supplier-save"]')
|
||||
await flushPromises()
|
||||
|
||||
expect(api.updateSupplier).toHaveBeenCalledWith(5, expect.objectContaining({
|
||||
name: 'Asociación Agropecuaria La Mesa Renovada',
|
||||
organization: 3,
|
||||
municipality: 7,
|
||||
}))
|
||||
})
|
||||
|
||||
it('elimina un proveedor', async () => {
|
||||
const api = mockApi()
|
||||
const wrapper = mountComponent(api)
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-testid="supplier-delete-6"]').trigger('click')
|
||||
clickBody('[data-testid="supplier-confirm-delete"]')
|
||||
await flushPromises()
|
||||
|
||||
expect(api.deleteSupplier).toHaveBeenCalledWith(6)
|
||||
expect(api.getSuppliers).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user