49 lines
2 KiB
TypeScript
49 lines
2 KiB
TypeScript
|
|
import { describe, it, expect } from 'vitest'
|
||
|
|
import { mapCurrentShoppingList, mapPurchasedShoppingList } from '@/api/sdk'
|
||
|
|
|
||
|
|
// These tests lock boundary behavior for partial/missing lookups and date normalization
|
||
|
|
|
||
|
|
describe('shopping mappers boundary', () => {
|
||
|
|
it('handles missing lookups gracefully (no refs attached)', () => {
|
||
|
|
const dto = {
|
||
|
|
outstandingItems: [
|
||
|
|
{ ingredientId: 10, mealId: 1, recipeId: 5, listId: 7, createdDate: '2025-01-01T00:00:00Z' },
|
||
|
|
],
|
||
|
|
requestedMeals: [
|
||
|
|
{ mealId: 2, createdDate: '2025-01-02T00:00:00Z' },
|
||
|
|
],
|
||
|
|
purchasedItems: [],
|
||
|
|
}
|
||
|
|
const mapped = mapCurrentShoppingList(dto as any)
|
||
|
|
expect(mapped.outstandingItems[0].ingredient).toBeUndefined()
|
||
|
|
expect(mapped.outstandingItems[0].meal).toBeUndefined()
|
||
|
|
expect(mapped.outstandingItems[0].recipe).toBeUndefined()
|
||
|
|
expect(mapped.outstandingItems[0].list).toBeUndefined()
|
||
|
|
expect(mapped.requestedMeals[0].meal).toBeUndefined()
|
||
|
|
// Dates normalized
|
||
|
|
expect(mapped.outstandingItems[0].createdDate).toBeInstanceOf(Date)
|
||
|
|
expect(mapped.requestedMeals[0].createdDate).toBeInstanceOf(Date)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('normalizes dates on purchased list and items even with partial lookups', () => {
|
||
|
|
const dto = {
|
||
|
|
ingredientsLookup: { 10: { id: 10, name: 'Milk' } },
|
||
|
|
list: {
|
||
|
|
id: 11,
|
||
|
|
createdDate: '2025-03-03T00:00:00Z',
|
||
|
|
items: [
|
||
|
|
{ ingredientId: 10, listId: 11, createdDate: '2025-03-03T00:00:00Z' },
|
||
|
|
{ ingredientId: 99, listId: 11, createdDate: '2025-03-03T00:00:00Z' },
|
||
|
|
],
|
||
|
|
},
|
||
|
|
}
|
||
|
|
const mapped = mapPurchasedShoppingList(dto as any)
|
||
|
|
expect(mapped.list.createdDate).toBeInstanceOf(Date)
|
||
|
|
expect(mapped.list.items[0].createdDate).toBeInstanceOf(Date)
|
||
|
|
expect(mapped.list.items[1].createdDate).toBeInstanceOf(Date)
|
||
|
|
// First item gets ingredient ref, second does not
|
||
|
|
expect(mapped.list.items[0].ingredient?.name).toBe('Milk')
|
||
|
|
expect(mapped.list.items[1].ingredient).toBeUndefined()
|
||
|
|
})
|
||
|
|
})
|