feat(commercial) : submitRows collecte les erreurs de tous les blocs de collection (ERP-110)
This commit is contained in:
@@ -52,3 +52,71 @@ describe('useClientFormErrors', () => {
|
||||
expect(f.addressErrors.value[0]).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
// Construit une erreur facon useApi : 422 avec violations Hydra.
|
||||
function http422(path: string, message: string) {
|
||||
return { response: { status: 422, _data: { violations: [{ propertyPath: path, message }] } } }
|
||||
}
|
||||
|
||||
/**
|
||||
* `submitRows` factorise la soumission d'une collection de blocs (contacts /
|
||||
* adresses / RIB) : on tente TOUS les blocs et on collecte les erreurs par index
|
||||
* sans stopper au premier echec (ERP-110 / ERP-101).
|
||||
*/
|
||||
describe('useClientFormErrors.submitRows', () => {
|
||||
it('tente TOUS les blocs et mappe les erreurs par index, sans stopper au premier echec', async () => {
|
||||
const { contactErrors, submitRows } = useClientFormErrors()
|
||||
const seen: number[] = []
|
||||
const onUnmapped = vi.fn()
|
||||
|
||||
const saveRow = async (_row: unknown, index: number) => {
|
||||
seen.push(index)
|
||||
if (index === 1) throw http422('email', 'Email invalide')
|
||||
}
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ a: 0 }, { a: 1 }, { a: 2 }],
|
||||
contactErrors,
|
||||
saveRow,
|
||||
onUnmapped,
|
||||
)
|
||||
|
||||
expect(seen).toEqual([0, 1, 2]) // tous les blocs tentes
|
||||
expect(hasError).toBe(true)
|
||||
expect(contactErrors.value[1]).toEqual({ email: 'Email invalide' })
|
||||
expect(contactErrors.value[0]).toBeUndefined()
|
||||
expect(onUnmapped).not.toHaveBeenCalled() // 422 mappee, pas de fallback
|
||||
})
|
||||
|
||||
it('delegue le fallback onUnmappedError pour une erreur non mappable et marque hasError', async () => {
|
||||
const { ribErrors, submitRows } = useClientFormErrors()
|
||||
const onUnmapped = vi.fn()
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ a: 0 }],
|
||||
ribErrors,
|
||||
async () => { throw { response: { status: 500, _data: {} } } },
|
||||
onUnmapped,
|
||||
)
|
||||
|
||||
expect(hasError).toBe(true)
|
||||
expect(onUnmapped).toHaveBeenCalledTimes(1)
|
||||
expect(ribErrors.value[0]).toBeUndefined()
|
||||
})
|
||||
|
||||
it('saute les lignes filtrees par shouldSkip et renvoie false si tout passe', async () => {
|
||||
const { contactErrors, submitRows } = useClientFormErrors()
|
||||
const saved: number[] = []
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ skip: true }, { skip: false }],
|
||||
contactErrors,
|
||||
async (_row, index) => { saved.push(index) },
|
||||
vi.fn(),
|
||||
(row: { skip: boolean }) => row.skip,
|
||||
)
|
||||
|
||||
expect(saved).toEqual([1])
|
||||
expect(hasError).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user