Some checks failed
CI / build-test (push) Has been cancelled
commit 594a14009096f7321ff92638e05cfa7c5ae6ca07
Author: jableader <jacobdunk@gmail.com>
Date: Sat Nov 1 12:20:33 2025 +1100
API update - avoid optional arrays
commit 85787384cfc24dc28953ebb4630fe7614cb0ead1
Author: jableader <jacobdunk@gmail.com>
Date: Sat Nov 1 10:14:51 2025 +1100
refactor(types): add NonNullableArrays and apply to Meal arrays; test: add shopping mapper boundary tests; all checks green
commit 1c50b7e23585cd4a34b5a691ffbf87d6d123f474
Author: jableader <jacobdunk@gmail.com>
Date: Sun Oct 26 15:30:45 2025 +1100
Use discriminated unions and overloads
48 lines
2 KiB
TypeScript
48 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()
|
|
})
|
|
})
|