93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
import { Measurement } from '../src/types'
|
|
import * as SQLite from 'expo-sqlite'
|
|
|
|
jest.mock('expo-sqlite', () => ({
|
|
openDatabaseAsync: jest.fn(),
|
|
}))
|
|
|
|
const mockMeasurement: Measurement = {
|
|
id: '550e8400-e29b-41d4-a716-446655440000',
|
|
weight: 75.5,
|
|
bmi: 22.3,
|
|
bodyFatPercentage: 15.2,
|
|
fatMass: 11.5,
|
|
musclePercentage: 40.1,
|
|
muscleMass: 30.3,
|
|
visceralFat: 8,
|
|
bmr: 1650,
|
|
measuredAt: '2024-01-01T08:00:00.000Z',
|
|
createdAt: '2024-01-01T08:00:00.000Z',
|
|
updatedAt: '2024-01-01T08:00:00.000Z',
|
|
}
|
|
|
|
describe('database', () => {
|
|
let db: any
|
|
let initDatabase: any
|
|
let insertMeasurement: any
|
|
let getAllMeasurements: any
|
|
let getLatestMeasurement: any
|
|
let setDeletedAt: any
|
|
|
|
beforeEach(async () => {
|
|
jest.resetModules()
|
|
jest.doMock('expo-sqlite', () => ({
|
|
openDatabaseAsync: jest.fn(),
|
|
}))
|
|
db = {
|
|
execAsync: jest.fn(),
|
|
runAsync: jest.fn(),
|
|
getAllAsync: jest.fn(),
|
|
}
|
|
const sqlite = require('expo-sqlite')
|
|
;(sqlite.openDatabaseAsync as jest.Mock).mockResolvedValue(db)
|
|
const mod = require('../src/database/db')
|
|
initDatabase = mod.initDatabase
|
|
insertMeasurement = mod.insertMeasurement
|
|
getAllMeasurements = mod.getAllMeasurements
|
|
getLatestMeasurement = mod.getLatestMeasurement
|
|
setDeletedAt = mod.setDeletedAt
|
|
})
|
|
|
|
it('initDatabase creates the measurements table', async () => {
|
|
await initDatabase()
|
|
expect(db.execAsync).toHaveBeenCalledWith(
|
|
expect.stringContaining('CREATE TABLE IF NOT EXISTS measurements')
|
|
)
|
|
})
|
|
|
|
it('insertMeasurement inserts a row', async () => {
|
|
db.runAsync.mockResolvedValue(undefined)
|
|
await initDatabase()
|
|
await insertMeasurement(mockMeasurement)
|
|
expect(db.runAsync).toHaveBeenCalledWith(
|
|
expect.stringContaining('INSERT INTO measurements'),
|
|
expect.arrayContaining([mockMeasurement.id])
|
|
)
|
|
})
|
|
|
|
it('getAllMeasurements returns all measurements ordered by measuredAt desc', async () => {
|
|
db.getAllAsync.mockResolvedValue([{ ...mockMeasurement }])
|
|
await initDatabase()
|
|
const result = await getAllMeasurements()
|
|
expect(result).toHaveLength(1)
|
|
expect(result[0].id).toBe(mockMeasurement.id)
|
|
})
|
|
|
|
it('getLatestMeasurement returns the most recent', async () => {
|
|
db.getAllAsync.mockResolvedValue([{ ...mockMeasurement }])
|
|
await initDatabase()
|
|
const result = await getLatestMeasurement()
|
|
expect(result).not.toBeNull()
|
|
expect(result!.id).toBe(mockMeasurement.id)
|
|
})
|
|
|
|
it('setDeletedAt sets the deletedAt timestamp', async () => {
|
|
db.runAsync.mockResolvedValue(undefined)
|
|
await initDatabase()
|
|
await setDeletedAt(mockMeasurement.id)
|
|
expect(db.runAsync).toHaveBeenCalledWith(
|
|
expect.stringContaining('UPDATE measurements SET deletedAt'),
|
|
expect.arrayContaining([mockMeasurement.id])
|
|
)
|
|
})
|
|
})
|