feat(transport) : onglet contacts transporteur (ERP-168)
This commit is contained in:
@@ -37,6 +37,8 @@ vi.stubGlobal('useToast', () => ({
|
||||
}))
|
||||
|
||||
const { useCarrierForm, CARRIER_TAB_KEYS } = await import('../useCarrierForm')
|
||||
const { buildCarrierContactPayload, isCarrierContactBlank } = await import('../../utils/forms/carrierContact')
|
||||
const { emptyCarrierContact } = await import('../../types/carrierForm')
|
||||
|
||||
describe('useCarrierForm', () => {
|
||||
beforeEach(() => {
|
||||
@@ -538,3 +540,127 @@ describe('useCarrierForm — onglet Adresses (ERP-167)', () => {
|
||||
expect(form.addresses.value).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('carrierContact (util) — RG-4.08 + max 2 téléphones', () => {
|
||||
it('isCarrierContactBlank : vrai si aucun champ, faux dès un champ rempli', () => {
|
||||
expect(isCarrierContactBlank(emptyCarrierContact())).toBe(true)
|
||||
expect(isCarrierContactBlank({ ...emptyCarrierContact(), jobTitle: 'Acheteur' })).toBe(false)
|
||||
expect(isCarrierContactBlank({ ...emptyCarrierContact(), phonePrimary: '0102030405' })).toBe(false)
|
||||
})
|
||||
|
||||
it('buildCarrierContactPayload : phones = 1 numéro sans secondaire', () => {
|
||||
const body = buildCarrierContactPayload({ ...emptyCarrierContact(), phonePrimary: '0102030405' })
|
||||
expect(body.phones).toEqual(['0102030405'])
|
||||
})
|
||||
|
||||
it('buildCarrierContactPayload : phones = 2 numéros si secondaire révélé', () => {
|
||||
const body = buildCarrierContactPayload({
|
||||
...emptyCarrierContact(),
|
||||
phonePrimary: '0102030405',
|
||||
phoneSecondary: '0605040302',
|
||||
hasSecondaryPhone: true,
|
||||
})
|
||||
expect(body.phones).toEqual(['0102030405', '0605040302'])
|
||||
})
|
||||
|
||||
it('buildCarrierContactPayload : 2e numéro ignoré tant que non révélé', () => {
|
||||
const body = buildCarrierContactPayload({
|
||||
...emptyCarrierContact(),
|
||||
phonePrimary: '0102030405',
|
||||
phoneSecondary: '0605040302',
|
||||
hasSecondaryPhone: false,
|
||||
})
|
||||
expect(body.phones).toEqual(['0102030405'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('useCarrierForm — onglet Contacts (ERP-168)', () => {
|
||||
beforeEach(() => {
|
||||
mockPost.mockReset()
|
||||
mockPatch.mockReset()
|
||||
mockDelete.mockReset()
|
||||
})
|
||||
|
||||
/** Transporteur créé, onglet Contacts accessible. */
|
||||
function createdForm() {
|
||||
const form = useCarrierForm()
|
||||
form.carrierId.value = 7
|
||||
return form
|
||||
}
|
||||
|
||||
it('RG-4.08 : « + Nouveau contact » désactivé tant que le bloc est vide', () => {
|
||||
const form = createdForm()
|
||||
expect(form.canAddContact.value).toBe(false)
|
||||
|
||||
// addContact est un no-op tant que le bloc est vide.
|
||||
form.addContact()
|
||||
expect(form.contacts.value).toHaveLength(1)
|
||||
|
||||
const first = form.contacts.value[0]
|
||||
if (first) first.lastName = 'Doe'
|
||||
expect(form.canAddContact.value).toBe(true)
|
||||
form.addContact()
|
||||
expect(form.contacts.value).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('submitContacts : POST des nouveaux contacts (phones tableau), capture id, finalise', async () => {
|
||||
mockPost.mockResolvedValueOnce({ id: 55 })
|
||||
const form = createdForm()
|
||||
const c = form.contacts.value[0]
|
||||
if (c) { c.firstName = 'Jean'; c.phonePrimary = '0102030405' }
|
||||
|
||||
const ok = await form.submitContacts(vi.fn())
|
||||
|
||||
expect(ok).toBe(true)
|
||||
const [url, body, opts] = mockPost.mock.calls[0] ?? []
|
||||
expect(url).toBe('/carriers/7/contacts')
|
||||
expect(body).toMatchObject({ firstName: 'Jean', phones: ['0102030405'] })
|
||||
expect(opts).toMatchObject({ toast: false, headers: { Accept: 'application/ld+json' } })
|
||||
expect(form.contacts.value[0]?.id).toBe(55)
|
||||
expect(form.isValidated('contacts')).toBe(true)
|
||||
})
|
||||
|
||||
it('submitContacts : PATCH des contacts existants sur /carrier_contacts/{id}', async () => {
|
||||
mockPatch.mockResolvedValueOnce({})
|
||||
const form = createdForm()
|
||||
const c = form.contacts.value[0]
|
||||
if (c) { c.id = 55; c.lastName = 'Doe' }
|
||||
|
||||
await form.submitContacts(vi.fn())
|
||||
|
||||
expect(mockPost).not.toHaveBeenCalled()
|
||||
expect(mockPatch).toHaveBeenCalledWith('/carrier_contacts/55', expect.objectContaining({ lastName: 'Doe' }), { toast: false })
|
||||
})
|
||||
|
||||
it('RG-4.08 : onglet vide → soumet l\'amorce pour déclencher la 422 inline', async () => {
|
||||
mockPost.mockRejectedValueOnce({
|
||||
response: {
|
||||
status: 422,
|
||||
_data: { violations: [{ propertyPath: 'firstName', message: 'Au moins un champ du contact est obligatoire.' }] },
|
||||
},
|
||||
})
|
||||
const form = createdForm()
|
||||
|
||||
const ok = await form.submitContacts(vi.fn())
|
||||
|
||||
expect(ok).toBe(false)
|
||||
expect(mockPost).toHaveBeenCalledTimes(1)
|
||||
expect(form.contactErrors.value[0]?.firstName).toBe('Au moins un champ du contact est obligatoire.')
|
||||
expect(form.isValidated('contacts')).toBe(false)
|
||||
})
|
||||
|
||||
it('removeContact : DELETE /carrier_contacts/{id} puis retrait du bloc', async () => {
|
||||
mockDelete.mockResolvedValueOnce({})
|
||||
const form = createdForm()
|
||||
const c = form.contacts.value[0]
|
||||
if (c) { c.id = 90; c.lastName = 'Doe' }
|
||||
form.addContact()
|
||||
const c2 = form.contacts.value[1]
|
||||
if (c2) c2.firstName = 'Jean'
|
||||
|
||||
await form.removeContact(0)
|
||||
|
||||
expect(mockDelete).toHaveBeenCalledWith('/carrier_contacts/90', {}, { toast: false })
|
||||
expect(form.contacts.value).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user