added test for reordering qr.code-cards, last selected qr-code-card, edit card

This commit is contained in:
Sebastian G committed 2026-09-07 10:24:06 +02:00
1 parent 0ef72ae619
commit b7a4277870
3 files changed
+152 -11

No files matched your search

+18 -11
View File
@@ -16,8 +16,22 @@ import { confirmSheet } from '../sheets.jsx'
// Long-press before a card lifts for reordering, and the finger travel that instead counts as a
// horizontal swipe of the rail. Mirrors the routine-editor drag (views/RoutineEdit.jsx) so the
// gesture feels the same across the app.
const CI_LONG_PRESS_MS = 380
const CI_DRAG_SLOP = 8
export const CI_LONG_PRESS_MS = 380
export const CI_DRAG_SLOP = 8
// Move the card at `from` to sit at index `to`, returning a new array (the input is left alone).
// This is the whole of the reorder once the drop target is known — kept as a pure function, apart
// from the pointer maths that computes `to`, so the ordering contract can be tested without a DOM.
// Out-of-range or no-op moves return an unchanged copy rather than throwing.
export function moveGymCard(cards, from, to) {
const next = [...cards]
if (from < 0 || from >= next.length) return next
const target = Math.max(0, Math.min(to, next.length - 1))
if (target === from) return next
const [moved] = next.splice(from, 1)
next.splice(target, 0, moved)
return next
}
// Gym check-in (reached from the Home "Check in" card; app and PWA alike). Shows each saved
// membership code as a QR the turnstile can read, swiped through horizontally, with a trailing
@@ -158,15 +172,8 @@ function useRailReorder(railRef, cards) {
if (from >= 0) {
const cardEls = [...rail.querySelectorAll('[data-ci-card]')]
const centres = cardEls.map(el => { const r = el.getBoundingClientRect(); return r.left + r.width / 2 })
let to = centres.reduce((n, cx, i) => n + (i !== from && g.lastX > cx ? 1 : 0), 0)
to = Math.min(to, list.length - 1)
if (to !== from) {
useStore.getState().update(s => {
const arr = s.gymCards
const [moved] = arr.splice(from, 1)
arr.splice(to, 0, moved)
})
}
const to = centres.reduce((n, cx, i) => n + (i !== from && g.lastX > cx ? 1 : 0), 0)
if (to !== from) useStore.getState().update(s => { s.gymCards = moveGymCard(s.gymCards, from, to) })
}
setState({ dragging: false, dragId: null })
}
@@ -0,0 +1,58 @@
// @vitest-environment happy-dom
// @vitest-environment happy-dom
// The rail's reorder maths. The drop handler in CheckIn.jsx turns a pointer position into a target
// index; moveGymCard is the pure part that then rearranges the array. These tests cover that
// contract — a move is a splice, the input is never mutated, and degenerate moves (in place, out of
// range) are safe no-ops — the same shape reorderRoutineUnit is tested to.
//
// moveGymCard itself is pure, but importing CheckIn.jsx loads the store (which registers a
// visibilitychange listener at module load) and the scanner/sheet modules, so the file runs under
// happy-dom with those heavy imports stubbed. The function under test touches none of them.
import { describe, expect, it, vi } from 'vitest'
vi.mock('../lib/scan.js', () => ({ scanCode: vi.fn(), importCodeFromImage: vi.fn() }))
vi.mock('../components/CameraScan.jsx', () => ({ default: () => null }))
vi.mock('../sheets.jsx', () => ({ confirmSheet: vi.fn() }))
import { moveGymCard } from './CheckIn.jsx'
const cards = (...ids) => ids.map(id => ({ id, label: id.toUpperCase(), value: id, fmt: 'qrcode' }))
const ids = list => list.map(c => c.id)
describe('moveGymCard', () => {
it('moves a card forward to a later slot', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c', 'd'), 0, 2))).toEqual(['b', 'c', 'a', 'd'])
})
it('moves a card backward to an earlier slot', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c', 'd'), 3, 1))).toEqual(['a', 'd', 'b', 'c'])
})
it('is a permutation — no card is lost or duplicated', () => {
const before = cards('a', 'b', 'c', 'd', 'e')
const after = moveGymCard(before, 4, 0)
expect(ids(after).sort()).toEqual(['a', 'b', 'c', 'd', 'e'])
expect(after).toHaveLength(before.length)
})
it('leaves the array unchanged when dropped in place', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c'), 1, 1))).toEqual(['a', 'b', 'c'])
})
it('clamps a target past the end to the last slot', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c'), 0, 99))).toEqual(['b', 'c', 'a'])
})
it('ignores an out-of-range source without throwing', () => {
expect(ids(moveGymCard(cards('a', 'b'), 5, 0))).toEqual(['a', 'b'])
})
it('does not mutate the input array', () => {
const before = cards('a', 'b', 'c')
const snapshot = ids(before)
moveGymCard(before, 0, 2)
expect(ids(before)).toEqual(snapshot)
})
})