Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

2.3 KB
Raw Blame
import { describe, it, expect } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import { useNumericText } from '@lyftr/shared'

// The hook itself lives in @lyftr/shared; this suite stays here because exercising a
// React hook needs jsdom + @testing-library, which shared's plain-node jest setup
// does not carry. ci.yml runs web's unit job on any packages/shared/** change, so a
// shared edit still trips these.

// Regression guard for the gym weight/reps in-progress-typing bug fixed in PR #59:
// the parent re-derives `value` from a rounded / 0→'' number on every keystroke, and
// must NOT clobber what the user is mid-typing (a trailing ".", a leading "0"), while
// still re-syncing for genuinely different numbers (stepper / programmatic changes).
describe('useNumericText', () => {
  it('initializes to the given value', () => {
    const { result } = renderHook(() => useNumericText('90'))
    expect(result.current[0]).toBe('90')
  })

  it('preserves an in-progress "2." when the parent re-derives an equal number', () => {
    const { result, rerender } = renderHook(({ v }) => useNumericText(v), { initialProps: { v: '2' } })
    act(() => result.current[1]('2.')) // user typed a trailing dot
    rerender({ v: '2.0' }) // parent re-derived; 2.0 is numerically equal to 2.
    expect(result.current[0]).toBe('2.')
  })

  it('preserves a leading "0" even when the parent maps 0 → ""', () => {
    const { result, rerender } = renderHook(({ v }) => useNumericText(v), { initialProps: { v: '5' } })
    act(() => result.current[1]('0')) // user cleared and typed "0"
    rerender({ v: '' }) // parent stored 0 and maps it to ''
    expect(result.current[0]).toBe('0')
  })

  it('re-syncs when the parent value is a genuinely different number (stepper/prefill)', () => {
    const { result, rerender } = renderHook(({ v }) => useNumericText(v), { initialProps: { v: '90' } })
    rerender({ v: '95' }) // stepper / programmatic change / deferred prefill
    expect(result.current[0]).toBe('95')
  })

  it('populates a deferred prefill (empty → value), e.g. autofocus edit modals', () => {
    const { result, rerender } = renderHook(({ v }) => useNumericText(v), { initialProps: { v: '' } })
    rerender({ v: '180' }) // parent effect prefills after mount
    expect(result.current[0]).toBe('180')
  })
})