weegschaal-app/tests/syncService.test.ts
eddy ed714af8d1 feat: add MeasurementDetailScreen, 365-day charts, and edit support
- New MeasurementDetailScreen with wijzigen/verwijderen
- WeightChart now supports 30 and 365 days
- NewMeasurementScreen now handles both create and edit
- Updated README to match current code
- Add build-toolchain documentation
- Ignore *.apk build artifacts
2026-07-14 09:14:59 +02:00

72 lines
2.3 KiB
TypeScript

import { mergeMeasurements } from '../src/sync/syncService'
import { Measurement } from '../src/types'
jest.mock('expo-sqlite', () => ({
openDatabaseAsync: jest.fn(),
}))
const base: Measurement = {
id: 'id-1',
weight: 75,
bmi: 22,
bodyFatPercentage: 15,
fatMass: 10,
musclePercentage: 40,
muscleMass: 30,
visceralFat: 8,
bmr: 1600,
measuredAt: '2024-06-01T08:00:00.000Z',
createdAt: '2024-06-01T08:00:00.000Z',
updatedAt: '2024-06-01T08:00:00.000Z',
syncStatus: 'local',
}
describe('mergeMeasurements', () => {
it('returns empty array when both are empty', () => {
expect(mergeMeasurements([], [])).toEqual([])
})
it('keeps local items not on remote', () => {
const result = mergeMeasurements([base], [])
expect(result).toHaveLength(1)
expect(result[0].id).toBe('id-1')
})
it('keeps remote items not on local', () => {
const remote = { ...base, syncStatus: 'synced' as const }
const result = mergeMeasurements([], [remote])
expect(result).toHaveLength(1)
expect(result[0].id).toBe('id-1')
})
it('picks the newer version when both have same id', () => {
const local = { ...base, weight: 75, updatedAt: '2024-06-02T08:00:00.000Z' }
const remote = { ...base, weight: 76, updatedAt: '2024-06-03T08:00:00.000Z' }
const result = mergeMeasurements([local], [remote])
expect(result).toHaveLength(1)
expect(result[0].weight).toBe(76)
})
it('picks local when local is newer', () => {
const local = { ...base, weight: 77, updatedAt: '2024-06-04T08:00:00.000Z' }
const remote = { ...base, weight: 76, updatedAt: '2024-06-03T08:00:00.000Z' }
const result = mergeMeasurements([local], [remote])
expect(result).toHaveLength(1)
expect(result[0].weight).toBe(77)
})
it('excludes ids in excludeIds from both sides', () => {
const local = { ...base, id: 'id-1' }
const remote = { ...base, id: 'id-2' }
const result = mergeMeasurements([local], [remote], new Set(['id-1', 'id-2']))
expect(result).toHaveLength(0)
})
it('keeps ids not in excludeIds', () => {
const local = { ...base, id: 'id-1' }
const remote = { ...base, id: 'id-2' }
const result = mergeMeasurements([local], [remote], new Set(['id-1']))
expect(result).toHaveLength(1)
expect(result[0].id).toBe('id-2')
})
})