Files
parenthese/frontend/src/utils/saveOnLeave.test.js
T
IliesAllaliandClaude Haiku 5.5 a25f263451 fiche: enregistrer automatiquement en quittant la fiche
Les modifications en cours d'une fiche ne sont plus perdues quand on
change de personne, ferme la fiche, sort du mode modification, ouvre
un souvenir, ajoute une personne ou envoie les contributions.

- La fiche enregistre par le même chemin que « Enregistrer » (onSave),
  pour la personne quittée. Un échec ou une validation la garde ouverte
  avec le message d'erreur.
- Invité en contribution : les changements vont dans le brouillon, puis
  partent avec « Envoyer ».
- Un petit message confirme l'enregistrement. Rien n'est enregistré
  quand rien n'a changé, ni en démo.
- Un second enregistrement pendant l'envoi reçoit la même promesse
  (utils/saveOnLeave.js), sans double appel.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-19 01:08:26 +02:00

36 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it, vi } from 'vitest'
import { runOnce } from './saveOnLeave.js'
const tick = () => new Promise((resolve) => setTimeout(resolve, 0))
describe('runOnce (enregistrement de fiche)', () => {
it('un second appel pendant l’envoi reçoit le même résultat, sans second envoi', async () => {
const ref = { current: null }
const send = vi.fn(async () => { await tick(); return 'saved' })
const first = runOnce(ref, send)
const second = runOnce(ref, send)
expect(second).toBe(first)
expect(await first).toBe('saved')
expect(send).toHaveBeenCalledTimes(1)
})
it('une fois l’envoi terminé, un nouvel appel enregistre à nouveau (nouvelle modification)', async () => {
const ref = { current: null }
const send = vi.fn(async () => 'saved')
await runOnce(ref, send)
expect(ref.current).toBeNull()
await runOnce(ref, send)
expect(send).toHaveBeenCalledTimes(2)
})
it('un échec se propage au résultat et libère le verrou pour une nouvelle tentative', async () => {
const ref = { current: null }
const send = vi.fn()
.mockImplementationOnce(async () => { throw new Error('réseau') })
.mockImplementationOnce(async () => 'saved')
await expect(runOnce(ref, send)).rejects.toThrow('réseau')
expect(ref.current).toBeNull()
expect(await runOnce(ref, send)).toBe('saved')
})
})