169 lines
5.1 KiB
TypeScript
169 lines
5.1 KiB
TypeScript
/**
|
|
* hooks/cart-query-hooks.tsx — the local (AsyncStorage-backed) cart.
|
|
*
|
|
* useGetCart only builds items when productsById/productSlotsMap are populated,
|
|
* so each test seeds the central stores first.
|
|
*/
|
|
|
|
import React from 'react'
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { act, renderHook, waitFor } from '@testing-library/react-native'
|
|
import { StorageServiceCasual } from 'common-ui/src/services/StorageServiceCasual'
|
|
|
|
import {
|
|
clearLocalCart,
|
|
useAddToCart,
|
|
useGetCart,
|
|
useRemoveFromCart,
|
|
useUpdateCartItem,
|
|
} from '@/hooks/cart-query-hooks'
|
|
import { resetStorage } from '../mocks/api'
|
|
import { useCentralProductStore } from '@/src/store/centralProductStore'
|
|
import { useCentralSlotStore } from '@/src/store/centralSlotStore'
|
|
|
|
const PRODUCT_ID = 2
|
|
|
|
const seedCentralStores = () => {
|
|
useCentralProductStore.setState({
|
|
products: [],
|
|
productsById: {
|
|
[PRODUCT_ID]: {
|
|
id: PRODUCT_ID,
|
|
name: 'Chicken Breast 500 g',
|
|
price: 215,
|
|
marketPrice: 220,
|
|
flashPrice: 199,
|
|
images: [],
|
|
incrementStep: 1,
|
|
productQuantity: 1,
|
|
unitNotation: '0.5Kg',
|
|
},
|
|
} as never,
|
|
})
|
|
|
|
useCentralSlotStore.setState({
|
|
productSlotsMap: {
|
|
[PRODUCT_ID]: {
|
|
slots: [{ id: 1849 }],
|
|
isOutOfStock: false,
|
|
isFlashAvailable: true,
|
|
isSuspended: false,
|
|
},
|
|
} as never,
|
|
})
|
|
}
|
|
|
|
const readCart = async (key: string) => {
|
|
const raw = await StorageServiceCasual.getItem(key)
|
|
return raw ? JSON.parse(raw) : []
|
|
}
|
|
|
|
const wrapper = ({ children }: { children: React.ReactNode }) => {
|
|
const client = new QueryClient({
|
|
defaultOptions: { queries: { retry: false, gcTime: 0 } },
|
|
})
|
|
return <QueryClientProvider client={client}>{children}</QueryClientProvider>
|
|
}
|
|
|
|
describe('cart-query-hooks', () => {
|
|
beforeEach(async () => {
|
|
await resetStorage()
|
|
seedCentralStores()
|
|
})
|
|
|
|
it('adds an item to the regular cart', async () => {
|
|
const { result } = renderHook(() => useAddToCart({}, 'regular'), { wrapper })
|
|
|
|
await act(async () => {
|
|
result.current.addToCart(PRODUCT_ID, 1, 1849)
|
|
})
|
|
|
|
await waitFor(async () => {
|
|
const items = await readCart('cart_items')
|
|
expect(items).toHaveLength(1)
|
|
expect(items[0]).toMatchObject({ skuId: PRODUCT_ID, quantity: 1, slotId: 1849 })
|
|
})
|
|
})
|
|
|
|
it('keeps the flash cart separate from the regular cart', async () => {
|
|
const { result } = renderHook(() => useAddToCart({}, 'flash'), { wrapper })
|
|
|
|
await act(async () => {
|
|
result.current.addToCart(PRODUCT_ID, 1, 0)
|
|
})
|
|
|
|
expect(await readCart('flash_cart_items')).toHaveLength(1)
|
|
expect(await readCart('cart_items')).toHaveLength(0)
|
|
})
|
|
|
|
it('increments the quantity of an item already in the cart', async () => {
|
|
const { result } = renderHook(() => useAddToCart({}, 'regular'), { wrapper })
|
|
|
|
// Separate ticks: concurrent adds both read the pre-add cart.
|
|
await act(async () => {
|
|
result.current.addToCart(PRODUCT_ID, 1, 1849)
|
|
})
|
|
await act(async () => {
|
|
result.current.addToCart(PRODUCT_ID, 2, 1849)
|
|
})
|
|
|
|
await waitFor(async () => {
|
|
const items = await readCart('cart_items')
|
|
expect(items).toHaveLength(1)
|
|
expect(items[0].quantity).toBe(3)
|
|
})
|
|
})
|
|
|
|
it('updates and removes a cart line', async () => {
|
|
const add = renderHook(() => useAddToCart({}, 'regular'), { wrapper })
|
|
await act(async () => {
|
|
add.result.current.addToCart(PRODUCT_ID, 1, 1849)
|
|
})
|
|
|
|
const [item] = await readCart('cart_items')
|
|
|
|
const update = renderHook(() => useUpdateCartItem({}, 'regular'), { wrapper })
|
|
await act(async () => {
|
|
update.result.current.mutate({ itemId: item.id, quantity: 4 })
|
|
})
|
|
await waitFor(async () => {
|
|
expect((await readCart('cart_items'))[0].quantity).toBe(4)
|
|
})
|
|
|
|
const remove = renderHook(() => useRemoveFromCart({}, 'regular'), { wrapper })
|
|
await act(async () => {
|
|
remove.result.current.mutate({ itemId: item.id })
|
|
})
|
|
await waitFor(async () => {
|
|
expect(await readCart('cart_items')).toHaveLength(0)
|
|
})
|
|
})
|
|
|
|
it('derives cart items and the total from the central stores', async () => {
|
|
const add = renderHook(() => useAddToCart({}, 'regular'), { wrapper })
|
|
await act(async () => {
|
|
add.result.current.addToCart(PRODUCT_ID, 2, 1849)
|
|
})
|
|
|
|
const { result } = renderHook(() => useGetCart({}, 'regular'), { wrapper })
|
|
|
|
await waitFor(() => expect(result.current.totalItems).toBe(1))
|
|
expect(result.current.cartItems[0].subtotal).toBe(430)
|
|
expect(result.current.totalPrice).toBe(430)
|
|
expect(result.current.hasItems).toBe(true)
|
|
expect(result.current.isEmpty).toBe(false)
|
|
})
|
|
|
|
it('clears the local cart', async () => {
|
|
const add = renderHook(() => useAddToCart({}, 'regular'), { wrapper })
|
|
await act(async () => {
|
|
add.result.current.addToCart(PRODUCT_ID, 1, 1849)
|
|
})
|
|
expect(await readCart('cart_items')).toHaveLength(1)
|
|
|
|
await act(async () => {
|
|
await clearLocalCart('regular')
|
|
})
|
|
expect(await readCart('cart_items')).toHaveLength(0)
|
|
})
|
|
})
|