fix(captura): inputs de text/fecha se borraban al teclear (valor desde dirty)
This commit is contained in:
@@ -245,10 +245,36 @@ describe('TablaCaptura', () => {
|
|||||||
|
|
||||||
// Cambiar la fecha marca dirty en el campo _fecha y se guarda en el bulk
|
// Cambiar la fecha marca dirty en el campo _fecha y se guarda en el bulk
|
||||||
fireEvent.change(fechas[1], { target: { value: '2026-12-01' } })
|
fireEvent.change(fechas[1], { target: { value: '2026-12-01' } })
|
||||||
|
// El input muestra el valor recién elegido (no se resetea al guardado)
|
||||||
|
expect((fechas[1] as HTMLInputElement).value).toBe('2026-12-01')
|
||||||
fireEvent.click(screen.getByText('Guardar cambios'))
|
fireEvent.click(screen.getByText('Guardar cambios'))
|
||||||
|
|
||||||
expect(onGuardar).toHaveBeenCalledTimes(1)
|
expect(onGuardar).toHaveBeenCalledTimes(1)
|
||||||
const dirty = onGuardar.mock.calls[0][0]
|
const dirty = onGuardar.mock.calls[0][0]
|
||||||
expect(dirty['409-2']).toEqual({ certificacion_gas_fecha: '2026-12-01' })
|
expect(dirty['409-2']).toEqual({ certificacion_gas_fecha: '2026-12-01' })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('regresión: escribir en un campo text no se borra (valor viene de dirty)', () => {
|
||||||
|
const onGuardar = vi.fn()
|
||||||
|
const { container } = render(
|
||||||
|
<TablaCaptura
|
||||||
|
unidades={UNIDADES}
|
||||||
|
campos={CAMPOS_OBRA}
|
||||||
|
datos={DATOS}
|
||||||
|
onGuardar={onGuardar}
|
||||||
|
guardando={false}
|
||||||
|
/>,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Teclear en comentarios de la primera unidad (original: 'Listo')
|
||||||
|
const textos = container.querySelectorAll('input[type="text"]')
|
||||||
|
expect(textos.length).toBe(2) // 1 campo text * 2 unidades
|
||||||
|
fireEvent.change(textos[0], { target: { value: 'Nota X' } })
|
||||||
|
expect((textos[0] as HTMLInputElement).value).toBe('Nota X')
|
||||||
|
|
||||||
|
// Guardar persiste el texto tecleado
|
||||||
|
fireEvent.click(screen.getByText('Guardar cambios'))
|
||||||
|
expect(onGuardar).toHaveBeenCalledTimes(1)
|
||||||
|
expect(onGuardar.mock.calls[0][0]['409-1']).toEqual({ comentarios_obra: 'Nota X' })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -24,6 +24,18 @@ function valorOriginal(datos: DatosCaptura, unidad: string, campo: string): stri
|
|||||||
return datos[unidad]?.[campo] ?? null
|
return datos[unidad]?.[campo] ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Valor efectivo: el editado (dirty) si existe, si no el guardado.
|
||||||
|
* De lo contrario el input controlado se resetea al escribir. */
|
||||||
|
function valorEfectivo(
|
||||||
|
dirty: DirtyMap,
|
||||||
|
datos: DatosCaptura,
|
||||||
|
unidad: string,
|
||||||
|
campo: string,
|
||||||
|
): string | null {
|
||||||
|
const d = dirty[unidad]?.[campo]
|
||||||
|
return d !== undefined ? d : valorOriginal(datos, unidad, campo)
|
||||||
|
}
|
||||||
|
|
||||||
/** Fecha de hoy en formato ISO local (YYYY-MM-DD) */
|
/** Fecha de hoy en formato ISO local (YYYY-MM-DD) */
|
||||||
function hoyISO(): string {
|
function hoyISO(): string {
|
||||||
const d = new Date()
|
const d = new Date()
|
||||||
@@ -283,7 +295,7 @@ export function TablaCaptura({
|
|||||||
{unidad}
|
{unidad}
|
||||||
</td>
|
</td>
|
||||||
{camposVisibles.map((c) => {
|
{camposVisibles.map((c) => {
|
||||||
const val = valorOriginal(datos, unidad, c.campo)
|
const val = valorEfectivo(dirty, datos, unidad, c.campo)
|
||||||
const isDirty = dirty[unidad]?.[c.campo] !== undefined
|
const isDirty = dirty[unidad]?.[c.campo] !== undefined
|
||||||
const campoFecha = c.conFecha
|
const campoFecha = c.conFecha
|
||||||
return (
|
return (
|
||||||
@@ -299,7 +311,7 @@ export function TablaCaptura({
|
|||||||
dirty={isDirty}
|
dirty={isDirty}
|
||||||
conFecha={campoFecha}
|
conFecha={campoFecha}
|
||||||
valorFecha={
|
valorFecha={
|
||||||
campoFecha ? valorOriginal(datos, unidad, campoFecha) : undefined
|
campoFecha ? valorEfectivo(dirty, datos, unidad, campoFecha) : undefined
|
||||||
}
|
}
|
||||||
dirtyFecha={campoFecha ? dirty[unidad]?.[campoFecha] !== undefined : false}
|
dirtyFecha={campoFecha ? dirty[unidad]?.[campoFecha] !== undefined : false}
|
||||||
onFechaChange={
|
onFechaChange={
|
||||||
|
|||||||
Reference in New Issue
Block a user