freshyo/apps/user-ui/tests/stores/stores.test.ts
2026-09-26 13:41:46 +05:30

130 lines
4.9 KiB
TypeScript

/**
* Zustand stores: initial state + actions.
*
* Stores are module singletons; each test pokes the one it needs and resets it
* afterwards where the shape is shared with other tests.
*/
import { useAddressStore } from '@/src/store/addressStore'
import { useAppStore } from '@/src/store/appStore'
import { useCartStore } from '@/src/store/cartStore'
import { useCentralProductStore } from '@/src/store/centralProductStore'
import { useCentralSlotStore } from '@/src/store/centralSlotStore'
import { useFlashCartStore } from '@/src/store/flashCartStore'
import { useNavigationStore } from '@/src/store/navigationStore'
import { useQuickDeliveryStore } from '@/src/store/quickDeliveryStore'
import { useStoreHeaderStore } from '@/src/store/storeHeaderStore'
import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore'
import { useSlotStore } from '@/components/stores/slotStore'
describe('zustand stores', () => {
it('appStore tracks tab-bar visibility per caller', () => {
useAppStore.getState().setHideTabsNavKey('cart', true)
expect(useAppStore.getState().hideTabsNav.cart).toBe(true)
useAppStore.getState().setHideTabsNavKey('cart', false)
expect(useAppStore.getState().hideTabsNav.cart).toBe(false)
})
it('addressStore holds the selected checkout address', () => {
useAddressStore.getState().setSelectedAddressId(7)
expect(useAddressStore.getState().selectedAddressId).toBe(7)
})
it('cartStore queues and clears the add-to-cart product', () => {
const product = { id: 2, name: 'Chicken Breast 500 g' }
useCartStore.getState().setAddedToCartProduct({ productId: 2, product })
expect(useCartStore.getState().addedToCartProduct).toEqual({ productId: 2, product })
useCartStore.getState().clearAddedToCartProduct()
expect(useCartStore.getState().addedToCartProduct).toBeNull()
})
it('flashCartStore queues and clears the flash product', () => {
const product = { id: 5, name: 'Cucumber' }
useFlashCartStore.getState().setAddedFlashProduct({ productId: 5, product })
expect(useFlashCartStore.getState().addedFlashProduct).toEqual({ productId: 5, product })
useFlashCartStore.getState().clearAddedFlashProduct()
expect(useFlashCartStore.getState().addedFlashProduct).toBeNull()
})
it('navigationStore tracks the from-home flag and selected store', () => {
useNavigationStore.getState().setNavigatedFromHome(true)
useNavigationStore.getState().setSelectedStoreId(4)
const state = useNavigationStore.getState()
expect(state.isNavigatedFromHome).toBe(true)
expect(state.selectedStoreId).toBe(4)
})
it('quickDeliveryStore holds the selected slot', () => {
useQuickDeliveryStore.getState().setSelectedSlotId(1849)
expect(useQuickDeliveryStore.getState().selectedSlotId).toBe(1849)
})
it('storeHeaderStore holds the screen title', () => {
useStoreHeaderStore.getState().setTitle('Meat Store')
expect(useStoreHeaderStore.getState().title).toBe('Meat Store')
})
it('slotStore holds the quick-delivery slot and store', () => {
useSlotStore.getState().setSlotId(1849)
useSlotStore.getState().setStoreId(1)
expect(useSlotStore.getState().slotId).toBe(1849)
expect(useSlotStore.getState().storeId).toBe(1)
})
it('flashNavigationStore flags navigation back to the cart and resets', () => {
useFlashNavigationStore.getState().setShouldNavigateToCart(true)
expect(useFlashNavigationStore.getState().shouldNavigateToCart).toBe(true)
useFlashNavigationStore.getState().reset()
expect(useFlashNavigationStore.getState().shouldNavigateToCart).toBe(false)
})
it('centralProductStore indexes products by id', () => {
useCentralProductStore.getState().setProducts([
{ id: 2, name: 'Chicken Breast 500 g', price: 215 },
{ id: 5, name: 'Cucumber', price: 15 },
] as never)
const state = useCentralProductStore.getState()
expect(state.products).toHaveLength(2)
expect(state.productsById[2].name).toBe('Chicken Breast 500 g')
expect(state.productsById[5].price).toBe(15)
})
it('centralSlotStore builds productSlotsMap from slots + availability', () => {
useCentralSlotStore.getState().setSlotsData(
[
{
id: 1849,
deliveryTime: '2026-09-26T10:00:00.000Z',
freezeTime: '2026-09-26T09:00:00.000Z',
products: [{ id: 2, images: [] }],
},
] as never,
[{ id: 2 }] as never,
[
{
id: 2,
price: 215,
marketPrice: 220,
flashPrice: null,
isFlashAvailable: true,
isOutOfStock: false,
isSuspended: false,
},
] as never
)
const state = useCentralSlotStore.getState()
expect(state.isSlotsLoaded).toBe(true)
expect(state.slots).toHaveLength(1)
expect(state.productSlotsMap[2].slots.map((s) => s.id)).toEqual([1849])
expect(state.productSlotsMap[2].isFlashAvailable).toBe(true)
expect(state.productSlotsMap[2].isOutOfStock).toBe(false)
})
})