/* eslint-disable @typescript-eslint/no-explicit-any */ /** * Catalogue section 25 — "Stores" (`tests/lib/stores.test.ts`). * * Direct unit tests over the real Zustand stores: no rendering, only actions and * the resulting state. `resetAllMocks()` restores every store to its boot state * before each case. */ import { resetAllMocks } from '../helpers/reset' import { __set, __snapshot } from '../mocks/secure-store-mock' import { useAppStore } from '@/src/store/appStore' import { useAddressStore } from '@/src/store/addressStore' import { useCartStore } from '@/src/store/cartStore' 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 { useCheckoutStore } from '@/src/store/checkoutStore' import { useCentralProductStore } from '@/src/store/centralProductStore' import { useCentralSlotStore } from '@/src/store/centralSlotStore' import { useRegionStore } from '@/src/store/regionStore' import { useSlotStore } from '@/components/stores/slotStore' import { useFlashNavigationStore } from '@/components/stores/flashNavigationStore' import { AVAILABILITY, MERGED_PRODUCTS, P_CHICKEN, PRODUCT_AVAILABILITY, REGION_1, REGION_2, SLOTS, } from '../fixtures' const addedProduct = P_CHICKEN as any describe('stores', () => { beforeEach(() => { resetAllMocks() }) it('STORE-01: appStore updates the hideTabsNav map immutably', () => { const before = useAppStore.getState().hideTabsNav useAppStore.getState().setHideTabsNavKey('quick_delivery', true) const hidden = useAppStore.getState().hideTabsNav expect(hidden).toEqual({ quick_delivery: true }) expect(hidden).not.toBe(before) useAppStore.getState().setHideTabsNavKey('quick_delivery', false) expect(useAppStore.getState().hideTabsNav).toEqual({ quick_delivery: false }) }) it('STORE-02: addressStore selects and clears an address id', () => { useAddressStore.getState().setSelectedAddressId(11) expect(useAddressStore.getState().selectedAddressId).toBe(11) useAddressStore.getState().setSelectedAddressId(null) expect(useAddressStore.getState().selectedAddressId).toBeNull() }) it('STORE-03: cartStore queues and clears the added-to-cart product', () => { useCartStore.getState().setAddedToCartProduct(addedProduct) expect(useCartStore.getState().addedToCartProduct).toBe(addedProduct) useCartStore.getState().clearAddedToCartProduct() expect(useCartStore.getState().addedToCartProduct).toBeNull() }) it('STORE-04: flashCartStore queues and clears the added flash product', () => { useFlashCartStore.getState().setAddedFlashProduct(addedProduct) expect(useFlashCartStore.getState().addedFlashProduct).toBe(addedProduct) useFlashCartStore.getState().clearAddedFlashProduct() expect(useFlashCartStore.getState().addedFlashProduct).toBeNull() }) it('STORE-05: navigationStore sets and clears its flags', () => { useNavigationStore.getState().setNavigatedFromHome(true) useNavigationStore.getState().setSelectedStoreId(2) expect(useNavigationStore.getState().isNavigatedFromHome).toBe(true) expect(useNavigationStore.getState().selectedStoreId).toBe(2) useNavigationStore.getState().setNavigatedFromHome(false) useNavigationStore.getState().setSelectedStoreId(null) expect(useNavigationStore.getState().isNavigatedFromHome).toBe(false) expect(useNavigationStore.getState().selectedStoreId).toBeNull() }) it('STORE-06: quickDeliveryStore sets the selected slot id', () => { useQuickDeliveryStore.getState().setSelectedSlotId(1849) expect(useQuickDeliveryStore.getState().selectedSlotId).toBe(1849) useQuickDeliveryStore.getState().setSelectedSlotId(null) expect(useQuickDeliveryStore.getState().selectedSlotId).toBeNull() }) it('STORE-07: storeHeaderStore sets the header title', () => { useStoreHeaderStore.getState().setTitle('The Freshyo Meat House') expect(useStoreHeaderStore.getState().title).toBe('The Freshyo Meat House') }) it('STORE-08: slotStore sets the slot and store ids', () => { useSlotStore.getState().setSlotId(1849) useSlotStore.getState().setStoreId(2) expect(useSlotStore.getState().slotId).toBe(1849) expect(useSlotStore.getState().storeId).toBe(2) useSlotStore.getState().setSlotId(undefined) useSlotStore.getState().setStoreId(undefined) expect(useSlotStore.getState().slotId).toBeUndefined() expect(useSlotStore.getState().storeId).toBeUndefined() }) it('STORE-09: flashNavigationStore flag is set then reset', () => { useFlashNavigationStore.getState().setShouldNavigateToCart(true) expect(useFlashNavigationStore.getState().shouldNavigateToCart).toBe(true) useFlashNavigationStore.getState().reset() expect(useFlashNavigationStore.getState().shouldNavigateToCart).toBe(false) }) it('STORE-10: centralProductStore maps products by id', () => { useCentralProductStore.getState().setProducts(MERGED_PRODUCTS as any) const state = useCentralProductStore.getState() expect(state.products).toHaveLength(MERGED_PRODUCTS.length) expect(state.productsById[2]?.name).toBe('Chicken Breast') expect(Object.keys(state.productsById)).toHaveLength(MERGED_PRODUCTS.length) }) it('STORE-11: centralProductStore accepts a refetch setter', async () => { const refetch = jest.fn(async () => undefined) useCentralProductStore.getState().setRefetchProducts(refetch) expect(useCentralProductStore.getState().refetchProducts).toBe(refetch) await useCentralProductStore.getState().refetchProducts?.() expect(refetch).toHaveBeenCalledTimes(1) }) it('STORE-12: centralSlotStore builds a slot map for every product with flags', () => { useCentralSlotStore.getState().setSlotsData(SLOTS as any, PRODUCT_AVAILABILITY as any, AVAILABILITY as any) const state = useCentralSlotStore.getState() expect(state.isSlotsLoaded).toBe(true) expect(state.slots).toBe(SLOTS) // Every productAvailability row gets an entry, even those in no slot. expect(Object.keys(state.productSlotsMap)).toHaveLength(PRODUCT_AVAILABILITY.length) expect(state.productSlotsMap[2].slots.map((slot: any) => slot.id)).toEqual([1849, 1850, 1851, 1852]) expect(state.productSlotsMap[5].slots).toHaveLength(2) expect(state.productSlotsMap[6].slots).toHaveLength(0) // Flags come from the availability rows. expect(state.productSlotsMap[6].isOutOfStock).toBe(true) expect(state.productSlotsMap[8].isFlashAvailable).toBe(true) }) it('STORE-13: centralSlotStore accepts a refetch setter', async () => { const refetch = jest.fn(async () => undefined) useCentralSlotStore.getState().setRefetchSlots(refetch) expect(useCentralSlotStore.getState().refetchSlots).toBe(refetch) await useCentralSlotStore.getState().refetchSlots?.() expect(refetch).toHaveBeenCalledTimes(1) }) it('STORE-14: checkoutStore sets the delivery notes', () => { useCheckoutStore.getState().setUserNotes('Leave at the door') expect(useCheckoutStore.getState().userNotes).toBe('Leave at the door') }) it('STORE-15: regionStore sets the region and bumps the generation', () => { useRegionStore.getState().setRegion(REGION_1 as any, 7) const state = useRegionStore.getState() expect(state.selectedRegionId).toBe(1) expect(state.selectedRegion).toEqual(REGION_1) expect(state.selectedBoundId).toBe(7) expect(state.regionGeneration).toBe(1) }) it('STORE-16: regionStore setRegion with the identical region does not bump', () => { useRegionStore.getState().setRegion(REGION_1 as any, 7) const generation = useRegionStore.getState().regionGeneration useRegionStore.getState().setRegion(REGION_1 as any, 7) expect(useRegionStore.getState().regionGeneration).toBe(generation) }) it('STORE-17: regionStore clearRegion nulls the region and bumps', () => { useRegionStore.getState().setRegion(REGION_2 as any, 8) const generation = useRegionStore.getState().regionGeneration useRegionStore.getState().clearRegion() const state = useRegionStore.getState() expect(state.selectedRegionId).toBeNull() expect(state.selectedRegion).toBeNull() expect(state.selectedBoundId).toBeNull() expect(state.regionGeneration).toBe(generation + 1) }) it('STORE-18: regionStore clearRegion on an empty region does not bump', () => { expect(useRegionStore.getState().regionGeneration).toBe(0) useRegionStore.getState().clearRegion() expect(useRegionStore.getState().regionGeneration).toBe(0) }) it('STORE-19: regionStore is memory-only and never persists', () => { // An unrelated key proves the snapshot comparison below is meaningful. __set('unrelated_key', 'keep-me') const before = __snapshot() useRegionStore.getState().setRegion(REGION_1 as any, 7) useRegionStore.getState().clearRegion() // No zustand persist middleware: the storage snapshot is untouched. expect(__snapshot()).toEqual(before) expect(Object.keys(__snapshot()).some((key) => key.includes('region'))).toBe(false) }) })