/** * Vendor snippets list (app/(drawer)/dashboard/vendor-snippets/index.tsx) — * list rows with expiry status, empty/loading/error states, share link, the * per-row options menu (edit / delete / view orders) and FAB create flow. */ jest.mock('expo-router', () => require('../mocks/router').expoRouterMock()) import React from 'react' import { Alert, Share, TouchableOpacity } from 'react-native' import { act, fireEvent, screen, waitFor } from '@testing-library/react-native' import MaterialIcons from '@expo/vector-icons/MaterialIcons' import VendorSnippets from '@/app/(drawer)/dashboard/vendor-snippets/index' import { resetStorage, setupApiMocks } from '../mocks/api' import { expectProcedureCalled, lastProcedureInput, procCount, } from '../mocks/procedures' import { renderWithProviders } from '../helpers/render' const snippet = (overrides: Record = {}) => ({ id: 1, snippetCode: 'VS_1', slotId: 2, isPermanent: false, skuIds: [10, 11], products: [ { id: 10, name: 'Chicken' }, { id: 11, name: 'Mutton' }, ], validTill: '2027-01-01T00:00:00.000Z', createdAt: '2026-01-01T00:00:00.000Z', accessUrl: 'https://app.freshyo.in/vendor-order-list?id=VS_1', slot: { id: 2, deliveryTime: '2026-12-31T10:00:00.000Z', freezeTime: '2026-12-30T10:00:00.000Z', isActive: true, deliverySequence: 1, }, ...overrides, }) const order = { orderId: 'ORD1', orderDate: '2026-01-02T09:00:00.000Z', customerName: 'Alice', totalAmount: 100, slotInfo: { time: '2026-01-03T10:00:00.000Z', sequence: [] }, snippetCode: 'VS_1', matchedProducts: [10], products: [ { productId: 10, productName: 'Chicken', quantity: 2, price: 50, unit: 'kg', subtotal: 100 }, ], } const openFirstMenu = (result: ReturnType) => { fireEvent.press(result.UNSAFE_getAllByType(TouchableOpacity)[0]) } const pressLastAlertButton = (label: string) => { const calls = (Alert.alert as jest.Mock).mock.calls const call = calls[calls.length - 1] const buttons = (call?.[2] ?? []) as { text: string; onPress?: () => void }[] act(() => buttons.find((b) => b.text === label)?.onPress?.()) } describe('Vendor snippets list', () => { beforeEach(async () => { await resetStorage() setupApiMocks() jest.spyOn(Alert, 'alert').mockImplementation(() => {}) jest.spyOn(Share, 'share').mockResolvedValue({ action: Share.sharedAction } as never) }) it('shows the loading state while the query is pending', () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': () => new Promise(() => {}) }, }) renderWithProviders() expect(screen.getByText('Loading vendor snippets...')).toBeVisible() }) it('shows the error state when the query fails', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': () => { throw new Error('boom') }, }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('Error loading snippets')).toBeVisible()) }) it('shows the empty state when there are no snippets', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [] }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('No Snippets Yet')).toBeVisible()) }) it('renders snippet rows with status and item count', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [ snippet(), snippet({ id: 2, snippetCode: 'VS_EXPIRED', isPermanent: true, validTill: '2020-01-01T00:00:00.000Z', slot: null, }), ], }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) expect(screen.getByText('VS_EXPIRED')).toBeVisible() expect(screen.getByText('Active')).toBeVisible() expect(screen.getByText('Expired')).toBeVisible() expect(screen.getByText('Permanent')).toBeVisible() expect(screen.getByText('Always Active')).toBeVisible() expect(screen.getAllByText('2 Items')).toHaveLength(2) }) it('shares the snippet access link', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet()] }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) fireEvent.press(screen.getByText('Share')) await waitFor(() => expect(Share.share).toHaveBeenCalledWith({ message: 'https://app.freshyo.in/vendor-order-list?id=VS_1', }) ) }) it('warns instead of sharing when the link is missing', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet({ accessUrl: undefined })], }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) fireEvent.press(screen.getByText('Share')) expect(Alert.alert).toHaveBeenCalledWith('Error', 'No link available') expect(Share.share).not.toHaveBeenCalled() }) it('opens the create form from the FAB', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet()] }, }) const result = renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) const addIcon = result.UNSAFE_getAllByType(MaterialIcons).find( (node) => node.props.name === 'add' )! fireEvent.press(addIcon) expect(await screen.findByText('Create Vendor Snippet')).toBeVisible() }) it('deletes a snippet through the row menu', async () => { const api = setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet()], 'admin.vendorSnippets.delete': { message: 'Vendor snippet deleted successfully' }, }, }) const result = renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) openFirstMenu(result) fireEvent.press(await screen.findByText('Delete')) pressLastAlertButton('Delete') await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.vendorSnippets.delete')) expect(lastProcedureInput(api.fetchMock, 'admin.vendorSnippets.delete')).toEqual({ id: 1 }) }) it('loads the matched orders through the row menu', async () => { const api = setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet()], 'admin.vendorSnippets.getOrdersBySnippet': { success: true, data: [order], snippet: snippet(), }, }, }) const result = renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) openFirstMenu(result) fireEvent.press(await screen.findByText('View Orders')) await waitFor(() => expectProcedureCalled(api.fetchMock, 'admin.vendorSnippets.getOrdersBySnippet')) expect(lastProcedureInput(api.fetchMock, 'admin.vendorSnippets.getOrdersBySnippet')).toEqual({ snippetCode: 'VS_1', }) expect(await screen.findByText('Orders for "VS_1"')).toBeVisible() expect(screen.getByText('ORD1')).toBeVisible() }) it('opens the edit form from the row menu', async () => { setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet()] }, }) const result = renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) openFirstMenu(result) fireEvent.press(await screen.findByText('Edit')) expect(await screen.findByText('Edit Vendor Snippet')).toBeVisible() }) it('refetches through the mark-data-fetchers hook', async () => { const api = setupApiMocks({ procedures: { 'admin.vendorSnippets.getAll': [snippet()] }, }) renderWithProviders() await waitFor(() => expect(screen.getByText('VS_1')).toBeVisible()) const before = procCount(api.fetchMock, 'admin.vendorSnippets.getAll') const { refreshHooks } = require('../mocks/refresh') refreshHooks.markDataFetchers?.() await waitFor(() => expect(procCount(api.fetchMock, 'admin.vendorSnippets.getAll')).toBeGreaterThan(before) ) }) })