Files
don_confiao_frontend/tests/unit/services/django-api.spec.js

218 lines
6.2 KiB
JavaScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import http from '@/services/http'
import DjangoApi from '@/services/django-api'
vi.mock('@/services/http', () => ({
default: {
get: vi.fn(),
post: vi.fn(),
patch: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
},
}))
describe('DjangoApi.getPublicOrderSummary', () => {
beforeEach(() => {
vi.stubEnv('VITE_DJANGO_BASE_URL', 'http://backend.test')
http.get.mockReset()
http.get.mockResolvedValue({ data: { code: 'abc123', type: 'sale' } })
})
it('consulta el resumen público por código en la ruta resumen_publico', async () => {
const api = new DjangoApi()
const result = await api.getPublicOrderSummary('abc123')
expect(http.get).toHaveBeenCalledTimes(1)
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/resumen_publico/abc123'
)
expect(result).toEqual({ code: 'abc123', type: 'sale' })
})
})
describe('DjangoApi provenance CRUD', () => {
beforeEach(() => {
vi.stubEnv('VITE_DJANGO_BASE_URL', 'http://backend.test')
http.get.mockReset()
http.post.mockReset()
http.patch.mockReset()
http.delete.mockReset()
http.get.mockResolvedValue({ data: [] })
http.post.mockResolvedValue({ data: { id: 1 } })
http.patch.mockResolvedValue({ data: { id: 1 } })
http.delete.mockResolvedValue({ data: {} })
})
it('getProduct consulta el detalle autenticado del producto', async () => {
const api = new DjangoApi()
await api.getProduct(3)
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/products/3/'
)
})
it('getOrganizations consulta la lista de organizaciones', async () => {
const api = new DjangoApi()
await api.getOrganizations()
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/organizations/'
)
})
it('createOrganization hace POST con los datos', async () => {
const api = new DjangoApi()
const payload = { name: 'Red de Economía Solidaria' }
await api.createOrganization(payload)
expect(http.post).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/organizations/',
payload
)
})
it('updateOrganization hace PATCH con los datos', async () => {
const api = new DjangoApi()
const payload = { description: 'Nueva descripción' }
await api.updateOrganization(3, payload)
expect(http.patch).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/organizations/3/',
payload
)
})
it('deleteOrganization hace DELETE', async () => {
const api = new DjangoApi()
await api.deleteOrganization(3)
expect(http.delete).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/organizations/3/'
)
})
it('getSuppliers consulta la lista de proveedores', async () => {
const api = new DjangoApi()
await api.getSuppliers()
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/suppliers/'
)
})
it('createSupplier hace POST con datos de proveedor', async () => {
const api = new DjangoApi()
const payload = { name: 'Asociación La Mesa', organization: 3, municipality: 7 }
await api.createSupplier(payload)
expect(http.post).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/suppliers/',
payload
)
})
it('updateSupplier hace PATCH para desvincular organización con null', async () => {
const api = new DjangoApi()
await api.updateSupplier(5, { organization: null })
expect(http.patch).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/suppliers/5/',
{ organization: null }
)
})
it('deleteSupplier hace DELETE', async () => {
const api = new DjangoApi()
await api.deleteSupplier(5)
expect(http.delete).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/suppliers/5/'
)
})
it('CRUD de países usa el endpoint countries', async () => {
const api = new DjangoApi()
await api.getCountries()
await api.createCountry({ name: 'Colombia', code: 'CO' })
await api.updateCountry(1, { code: 'CO' })
await api.deleteCountry(1)
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/countries/'
)
expect(http.post).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/countries/',
{ name: 'Colombia', code: 'CO' }
)
expect(http.patch).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/countries/1/',
{ code: 'CO' }
)
expect(http.delete).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/countries/1/'
)
})
it('CRUD de departamentos usa el endpoint departments', async () => {
const api = new DjangoApi()
await api.getDepartments()
await api.createDepartment({ name: 'Cundinamarca', country: 1 })
await api.updateDepartment(2, { name: 'Antioquia' })
await api.deleteDepartment(2)
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/departments/'
)
expect(http.post).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/departments/',
{ name: 'Cundinamarca', country: 1 }
)
expect(http.patch).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/departments/2/',
{ name: 'Antioquia' }
)
expect(http.delete).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/departments/2/'
)
})
it('CRUD de municipios usa el endpoint municipalities', async () => {
const api = new DjangoApi()
await api.getMunicipalities()
await api.createMunicipality({ name: 'La Mesa', department: 2, country: 1 })
await api.updateMunicipality(7, { name: 'LA MESA' })
await api.deleteMunicipality(7)
expect(http.get).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/municipalities/'
)
expect(http.post).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/municipalities/',
{ name: 'La Mesa', department: 2, country: 1 }
)
expect(http.patch).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/municipalities/7/',
{ name: 'LA MESA' }
)
expect(http.delete).toHaveBeenCalledWith(
'http://backend.test/don_confiao/api/municipalities/7/'
)
})
})