This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { dateKey, habitWeek, isHabitComplete, isTaskView, numericHabitInputValue, shouldToggleRowSwipe } from './mvp-utils'
|
||||
import { dateKey, defaultView, habitWeek, isHabitComplete, isTaskView, nextHabitSwipeValue, numericHabitInputValue, previousHabitSwipeValue, shouldToggleRowSwipe } from './mvp-utils'
|
||||
|
||||
describe('MVP view utilities', () => {
|
||||
it('formats a local date as YYYY-MM-DD', () => {
|
||||
@@ -29,10 +29,22 @@ describe('MVP view utilities', () => {
|
||||
expect(numericHabitInputValue(4)).toBe(4)
|
||||
})
|
||||
|
||||
it('toggles a row only for a deliberate right swipe', () => {
|
||||
it('uses today as the default landing view', () => {
|
||||
expect(defaultView()).toBe('today')
|
||||
})
|
||||
|
||||
it('increments count habits one swipe at a time and caps at target', () => {
|
||||
expect(nextHabitSwipeValue('numeric', 2, 5)).toBe(3)
|
||||
expect(nextHabitSwipeValue('numeric', 5, 5)).toBe(5)
|
||||
expect(previousHabitSwipeValue('numeric', 3)).toBe(2)
|
||||
expect(previousHabitSwipeValue('numeric', 0)).toBe(0)
|
||||
})
|
||||
|
||||
it('toggles a row for a deliberate mostly-horizontal swipe', () => {
|
||||
expect(shouldToggleRowSwipe(78, 8)).toBe(true)
|
||||
expect(shouldToggleRowSwipe(88, 28)).toBe(true)
|
||||
expect(shouldToggleRowSwipe(-78, 8)).toBe(false)
|
||||
expect(shouldToggleRowSwipe(30, 2)).toBe(false)
|
||||
expect(shouldToggleRowSwipe(80, 35)).toBe(false)
|
||||
expect(shouldToggleRowSwipe(80, 60)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -24,11 +24,27 @@ export function isTaskView(view: string) {
|
||||
return view === 'tasks' || view === 'today' || view === 'upcoming'
|
||||
}
|
||||
|
||||
export function defaultView() {
|
||||
return 'today' as const
|
||||
}
|
||||
|
||||
export function isHabitComplete(kind: string | undefined, value: number | boolean | undefined, target = 1) {
|
||||
if (kind === 'numeric') return Number(value ?? 0) >= target
|
||||
return Boolean(value)
|
||||
}
|
||||
|
||||
export function nextHabitSwipeValue(kind: string | undefined, current: number | boolean | undefined, target = 1) {
|
||||
const value = Number(current ?? 0)
|
||||
if (kind === 'numeric') return Math.min(value + 1, target)
|
||||
return value > 0 ? 0 : 1
|
||||
}
|
||||
|
||||
export function previousHabitSwipeValue(kind: string | undefined, current: number | boolean | undefined) {
|
||||
const value = Number(current ?? 0)
|
||||
if (kind === 'numeric') return Math.max(0, value - 1)
|
||||
return 0
|
||||
}
|
||||
|
||||
export function numericHabitInputValue(input: number | string | undefined) {
|
||||
if (input === undefined || input === '') return null
|
||||
const value = Number(input)
|
||||
@@ -36,5 +52,5 @@ export function numericHabitInputValue(input: number | string | undefined) {
|
||||
}
|
||||
|
||||
export function shouldToggleRowSwipe(deltaX: number, deltaY: number) {
|
||||
return deltaX >= 64 && Math.abs(deltaY) <= 24 && deltaX > Math.abs(deltaY) * 1.5
|
||||
return deltaX >= 64 && deltaX > Math.abs(deltaY) * 1.5
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user