106 lines
4.7 KiB
TypeScript
106 lines
4.7 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest'
|
|
|
|
// `todayIso` est importé par le composable : on le stubbe pour une date déterministe.
|
|
vi.mock('~/shared/utils/date', () => ({ todayIso: () => '2026-06-22' }))
|
|
|
|
const { useWeighingTicketForm } = await import('../useWeighingTicketForm')
|
|
|
|
describe('useWeighingTicketForm', () => {
|
|
it('initialise les 2 blocs à la date du jour (RG-5.07), sans poids ni DSD', () => {
|
|
const form = useWeighingTicketForm()
|
|
expect(form.empty.date).toBe('2026-06-22')
|
|
expect(form.full.date).toBe('2026-06-22')
|
|
expect(form.empty.weight).toBeNull()
|
|
expect(form.empty.dsd).toBeNull()
|
|
expect(form.counterpartyType.value).toBeNull()
|
|
})
|
|
|
|
// ── Contrepartie conditionnelle (RG-5.03) ────────────────────────────────
|
|
it('CLIENT : ne conserve que le client, purge supplier et otherLabel', () => {
|
|
const form = useWeighingTicketForm()
|
|
form.supplierIri.value = '/api/suppliers/3'
|
|
form.otherLabel.value = 'Particulier'
|
|
|
|
form.setCounterpartyType('CLIENT')
|
|
form.clientIri.value = '/api/clients/629'
|
|
|
|
expect(form.counterpartyField.value).toBe('client')
|
|
expect(form.supplierIri.value).toBeNull()
|
|
expect(form.otherLabel.value).toBeNull()
|
|
|
|
const payload = form.buildCreatePayload()
|
|
expect(payload.counterpartyType).toBe('CLIENT')
|
|
expect(payload.client).toBe('/api/clients/629')
|
|
expect(payload).not.toHaveProperty('supplier')
|
|
expect(payload).not.toHaveProperty('otherLabel')
|
|
})
|
|
|
|
it('FOURNISSEUR : ne conserve que le supplier', () => {
|
|
const form = useWeighingTicketForm()
|
|
form.clientIri.value = '/api/clients/1'
|
|
form.setCounterpartyType('FOURNISSEUR')
|
|
form.supplierIri.value = '/api/suppliers/7'
|
|
|
|
expect(form.counterpartyField.value).toBe('supplier')
|
|
expect(form.clientIri.value).toBeNull()
|
|
expect(form.buildCreatePayload().supplier).toBe('/api/suppliers/7')
|
|
})
|
|
|
|
it('AUTRE : ne conserve que le libellé libre', () => {
|
|
const form = useWeighingTicketForm()
|
|
form.clientIri.value = '/api/clients/1'
|
|
form.setCounterpartyType('AUTRE')
|
|
form.otherLabel.value = 'Reprise interne'
|
|
|
|
expect(form.counterpartyField.value).toBe('other')
|
|
expect(form.clientIri.value).toBeNull()
|
|
expect(form.buildCreatePayload().otherLabel).toBe('Reprise interne')
|
|
})
|
|
|
|
// ── Immatriculation / « Tout format » partagés entre blocs (RG-5.01) ──────
|
|
it('immatriculation et plateFreeFormat sont partagés (une seule valeur)', () => {
|
|
const form = useWeighingTicketForm()
|
|
form.immatriculation.value = 'AB-123-CD'
|
|
form.plateFreeFormat.value = true
|
|
|
|
// Les 2 payloads (création + finalisation) reflètent la même valeur.
|
|
expect(form.buildCreatePayload().immatriculation).toBe('AB-123-CD')
|
|
expect(form.buildCreatePayload().plateFreeFormat).toBe(true)
|
|
expect(form.buildFullPayload().immatriculation).toBe('AB-123-CD')
|
|
expect(form.buildFullPayload().plateFreeFormat).toBe(true)
|
|
})
|
|
|
|
// ── Application d'une lecture de pesée ────────────────────────────────────
|
|
it('applyReading remplit poids / DSD / mode du bloc visé', () => {
|
|
const form = useWeighingTicketForm()
|
|
form.applyReading(form.empty, { weight: 7150, dsd: 1, mode: 'AUTO' })
|
|
expect(form.empty.weight).toBe(7150)
|
|
expect(form.empty.dsd).toBe(1)
|
|
expect(form.empty.mode).toBe('AUTO')
|
|
expect(form.empty.manualNumber).toBeNull()
|
|
|
|
form.applyReading(form.full, { weight: 14300, dsd: 2, mode: 'MANUAL', manualNumber: 'PAP-555' })
|
|
expect(form.full.weight).toBe(14300)
|
|
expect(form.full.manualNumber).toBe('PAP-555')
|
|
})
|
|
|
|
it('buildCreatePayload porte la pesée à vide, buildFullPayload la pesée à plein', () => {
|
|
const form = useWeighingTicketForm()
|
|
form.setCounterpartyType('CLIENT')
|
|
form.clientIri.value = '/api/clients/1'
|
|
form.applyReading(form.empty, { weight: 7150, dsd: 1, mode: 'AUTO' })
|
|
form.applyReading(form.full, { weight: 14300, dsd: 2, mode: 'AUTO' })
|
|
|
|
const create = form.buildCreatePayload()
|
|
expect(create.emptyWeight).toBe(7150)
|
|
expect(create.emptyDsd).toBe(1)
|
|
expect(create.emptyMode).toBe('AUTO')
|
|
expect(create).not.toHaveProperty('fullWeight')
|
|
|
|
const full = form.buildFullPayload()
|
|
expect(full.fullWeight).toBe(14300)
|
|
expect(full.fullDsd).toBe(2)
|
|
expect(full.fullMode).toBe('AUTO')
|
|
})
|
|
})
|