feat(transport) : onglet prix transporteur (ERP-169)
This commit is contained in:
@@ -38,7 +38,8 @@ vi.stubGlobal('useToast', () => ({
|
||||
|
||||
const { useCarrierForm, CARRIER_TAB_KEYS } = await import('../useCarrierForm')
|
||||
const { buildCarrierContactPayload, isCarrierContactBlank, isCarrierContactNamed } = await import('../../utils/forms/carrierContact')
|
||||
const { emptyCarrierContact } = await import('../../types/carrierForm')
|
||||
const { buildCarrierPricePayload, isCarrierPriceValid } = await import('../../utils/forms/carrierPrice')
|
||||
const { emptyCarrierContact, emptyCarrierPrice } = await import('../../types/carrierForm')
|
||||
|
||||
describe('useCarrierForm', () => {
|
||||
beforeEach(() => {
|
||||
@@ -682,3 +683,172 @@ describe('useCarrierForm — onglet Contacts (ERP-168)', () => {
|
||||
expect(form.contacts.value).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('carrierPrice (util) — bascule CLIENT/FOURNISSEUR + champs requis par branche', () => {
|
||||
const CLIENT = '/api/clients/3'
|
||||
const CLIENT_ADDR = '/api/client_addresses/8'
|
||||
const SUPPLIER = '/api/suppliers/5'
|
||||
const SUPPLIER_ADDR = '/api/supplier_addresses/9'
|
||||
const SITE = '/api/sites/1'
|
||||
|
||||
it('buildCarrierPricePayload CLIENT : branche client envoyée, branche fournisseur à null', () => {
|
||||
const body = buildCarrierPricePayload({
|
||||
...emptyCarrierPrice(),
|
||||
direction: 'CLIENT',
|
||||
clientIri: CLIENT,
|
||||
clientDeliveryAddressIri: CLIENT_ADDR,
|
||||
departureSiteIri: SITE,
|
||||
containerType: 'BENNE',
|
||||
pricingUnit: 'FORFAIT',
|
||||
price: '120.00',
|
||||
priceState: 'EN_COURS',
|
||||
})
|
||||
expect(body).toMatchObject({
|
||||
direction: 'CLIENT',
|
||||
client: CLIENT,
|
||||
clientDeliveryAddress: CLIENT_ADDR,
|
||||
departureSite: SITE,
|
||||
supplier: null,
|
||||
supplierSupplyAddress: null,
|
||||
deliverySite: null,
|
||||
containerType: 'BENNE',
|
||||
pricingUnit: 'FORFAIT',
|
||||
price: '120.00',
|
||||
priceState: 'EN_COURS',
|
||||
})
|
||||
})
|
||||
|
||||
it('buildCarrierPricePayload FOURNISSEUR : branche fournisseur envoyée, branche client à null', () => {
|
||||
const body = buildCarrierPricePayload({
|
||||
...emptyCarrierPrice(),
|
||||
direction: 'FOURNISSEUR',
|
||||
supplierIri: SUPPLIER,
|
||||
supplierSupplyAddressIri: SUPPLIER_ADDR,
|
||||
deliverySiteIri: SITE,
|
||||
containerType: 'FOND_MOUVANT',
|
||||
pricingUnit: 'TONNE',
|
||||
price: '45',
|
||||
priceState: 'VALIDE',
|
||||
})
|
||||
expect(body).toMatchObject({
|
||||
direction: 'FOURNISSEUR',
|
||||
supplier: SUPPLIER,
|
||||
supplierSupplyAddress: SUPPLIER_ADDR,
|
||||
deliverySite: SITE,
|
||||
client: null,
|
||||
clientDeliveryAddress: null,
|
||||
departureSite: null,
|
||||
})
|
||||
})
|
||||
|
||||
it('isCarrierPriceValid : faux si branche incomplète, vrai si branche complète + communs', () => {
|
||||
const base = { ...emptyCarrierPrice(), containerType: 'BENNE', pricingUnit: 'FORFAIT', price: '10', priceState: 'EN_COURS' }
|
||||
// Direction non choisie → invalide.
|
||||
expect(isCarrierPriceValid(base)).toBe(false)
|
||||
// CLIENT sans adresse/site → invalide.
|
||||
expect(isCarrierPriceValid({ ...base, direction: 'CLIENT', clientIri: CLIENT })).toBe(false)
|
||||
// CLIENT complet → valide.
|
||||
expect(isCarrierPriceValid({ ...base, direction: 'CLIENT', clientIri: CLIENT, clientDeliveryAddressIri: CLIENT_ADDR, departureSiteIri: SITE })).toBe(true)
|
||||
// FOURNISSEUR complet → valide.
|
||||
expect(isCarrierPriceValid({ ...base, direction: 'FOURNISSEUR', supplierIri: SUPPLIER, supplierSupplyAddressIri: SUPPLIER_ADDR, deliverySiteIri: SITE })).toBe(true)
|
||||
// Prix manquant → invalide même branche complète.
|
||||
expect(isCarrierPriceValid({ ...emptyCarrierPrice(), direction: 'CLIENT', clientIri: CLIENT, clientDeliveryAddressIri: CLIENT_ADDR, departureSiteIri: SITE, containerType: 'BENNE', pricingUnit: 'FORFAIT', priceState: 'EN_COURS' })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('useCarrierForm — onglet Prix (ERP-169)', () => {
|
||||
beforeEach(() => {
|
||||
mockPost.mockReset()
|
||||
mockPatch.mockReset()
|
||||
mockDelete.mockReset()
|
||||
})
|
||||
|
||||
function createdForm() {
|
||||
const form = useCarrierForm()
|
||||
form.carrierId.value = 7
|
||||
return form
|
||||
}
|
||||
|
||||
it('démarre vide ; « + Nouveau prix » autorisé sur liste vide, bloqué si dernier bloc incomplet', () => {
|
||||
const form = createdForm()
|
||||
expect(form.prices.value).toHaveLength(0)
|
||||
expect(form.canAddPrice.value).toBe(true)
|
||||
|
||||
form.addPrice()
|
||||
expect(form.prices.value).toHaveLength(1)
|
||||
// Bloc vide → on ne peut pas en ajouter un autre.
|
||||
expect(form.canAddPrice.value).toBe(false)
|
||||
form.addPrice()
|
||||
expect(form.prices.value).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('submitPrices : POST des nouveaux prix (branche CLIENT), capture l\'id, finalise', async () => {
|
||||
mockPost.mockResolvedValueOnce({ id: 50 })
|
||||
const form = createdForm()
|
||||
form.addPrice()
|
||||
const p = form.prices.value[0]
|
||||
if (p) {
|
||||
p.direction = 'CLIENT'
|
||||
p.clientIri = '/api/clients/3'
|
||||
p.clientDeliveryAddressIri = '/api/client_addresses/8'
|
||||
p.departureSiteIri = '/api/sites/1'
|
||||
p.containerType = 'BENNE'
|
||||
p.pricingUnit = 'FORFAIT'
|
||||
p.price = '120'
|
||||
p.priceState = 'EN_COURS'
|
||||
}
|
||||
|
||||
const ok = await form.submitPrices(vi.fn())
|
||||
|
||||
expect(ok).toBe(true)
|
||||
const [url, body, opts] = mockPost.mock.calls[0] ?? []
|
||||
expect(url).toBe('/carriers/7/prices')
|
||||
expect(body).toMatchObject({ direction: 'CLIENT', client: '/api/clients/3', supplier: null })
|
||||
expect(opts).toMatchObject({ toast: false, headers: { Accept: 'application/ld+json' } })
|
||||
expect(form.prices.value[0]?.id).toBe(50)
|
||||
expect(form.isValidated('prices')).toBe(true)
|
||||
})
|
||||
|
||||
it('submitPrices : PATCH des prix existants sur /carrier_prices/{id}', async () => {
|
||||
mockPatch.mockResolvedValueOnce({})
|
||||
const form = createdForm()
|
||||
form.addPrice()
|
||||
const p = form.prices.value[0]
|
||||
if (p) { p.id = 50; p.direction = 'FOURNISSEUR'; p.supplierIri = '/api/suppliers/5'; p.price = '10' }
|
||||
|
||||
await form.submitPrices(vi.fn())
|
||||
|
||||
expect(mockPost).not.toHaveBeenCalled()
|
||||
expect(mockPatch).toHaveBeenCalledWith('/carrier_prices/50', expect.objectContaining({ direction: 'FOURNISSEUR' }), { toast: false })
|
||||
})
|
||||
|
||||
it('submitPrices : mappe les 422 par ligne et ne finalise pas', async () => {
|
||||
mockPost.mockRejectedValueOnce({
|
||||
response: { status: 422, _data: { violations: [{ propertyPath: 'departureSite', message: 'Le site de depart est obligatoire pour un prix client.' }] } },
|
||||
})
|
||||
const form = createdForm()
|
||||
form.addPrice()
|
||||
const p = form.prices.value[0]
|
||||
if (p) { p.direction = 'CLIENT'; p.clientIri = '/api/clients/3'; p.price = '10' }
|
||||
|
||||
const ok = await form.submitPrices(vi.fn())
|
||||
|
||||
expect(ok).toBe(false)
|
||||
expect(form.priceErrors.value[0]?.departureSite).toBe('Le site de depart est obligatoire pour un prix client.')
|
||||
expect(form.isValidated('prices')).toBe(false)
|
||||
})
|
||||
|
||||
it('removePrice : DELETE /carrier_prices/{id} puis retrait du bloc', async () => {
|
||||
mockDelete.mockResolvedValueOnce({})
|
||||
const form = createdForm()
|
||||
form.addPrice()
|
||||
const p = form.prices.value[0]
|
||||
if (p) { p.id = 77; p.direction = 'CLIENT'; p.clientIri = '/api/clients/3'; p.clientDeliveryAddressIri = '/api/client_addresses/8'; p.departureSiteIri = '/api/sites/1'; p.containerType = 'BENNE'; p.pricingUnit = 'FORFAIT'; p.price = '10'; p.priceState = 'EN_COURS' }
|
||||
form.addPrice()
|
||||
|
||||
await form.removePrice(0)
|
||||
|
||||
expect(mockDelete).toHaveBeenCalledWith('/carrier_prices/77', {}, { toast: false })
|
||||
expect(form.prices.value).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user