/** * Integration Tests: Orders Store with API * Tests Pinia store integration with mocked API endpoints * * Run: npm run test:integration */ import { beforeEach, describe, expect, it, vi } from 'vitest' import { setActivePinia, createPinia } from 'pinia' import { useOrderStore } from '@/stores/modules/orders' import { ordersApi } from '@/services/api/client' import { server } from '@/tests/mocks/server' import { http, HttpResponse } from 'msw' /** * Test Suite: Orders Store */ describe('Orders Store with API Integration', () => { beforeEach(() => { setActivePinia(createPinia()) }) describe('fetchOrders', () => { it('loads orders from API', async () => { const store = useOrderStore() await store.fetchOrders(100, 0) expect(store.orders).toHaveLength(2) expect(store.orders[0].orderNo).toBe('ORD-001') expect(store.loading).toBe(false) expect(store.error).toBeNull() }) it('sets loading state during fetch', async () => { const store = useOrderStore() // Delay the API response server.use( http.get('*/api/orders', async () => { await new Promise((resolve) => setTimeout(resolve, 100)) return HttpResponse.json({ data: [] }) }) ) const fetchPromise = store.fetchOrders() expect(store.loading).toBe(true) await fetchPromise expect(store.loading).toBe(false) }) it('handles API errors gracefully', async () => { const store = useOrderStore() server.use( http.get('*/api/orders', () => { return HttpResponse.json( { message: 'Internal Server Error' }, { status: 500 } ) }) ) await store.fetchOrders() expect(store.error).not.toBeNull() expect(store.orders).toHaveLength(0) }) }) describe('fetchOrderById', () => { it('loads single order by ID', async () => { const store = useOrderStore() await store.fetchOrderById('ORD-001') expect(store.selectedOrder).not.toBeNull() expect(store.selectedOrder?.orderId).toBe('ORD-001') expect(store.loading).toBe(false) }) it('handles 404 not found', async () => { const store = useOrderStore() server.use( http.get('*/api/orders/:orderId', () => { return HttpResponse.json( { message: 'Not Found' }, { status: 404 } ) }) ) await store.fetchOrderById('ORD-NONEXISTENT') expect(store.error).not.toBeNull() expect(store.selectedOrder).toBeNull() }) }) describe('createOrder', () => { it('creates new order and updates store', async () => { const store = useOrderStore() const payload = { customerId: 'CUST-001', totalAmount: 100000 } const createdOrder = await store.createOrder(payload) expect(createdOrder).toBeDefined() expect(createdOrder.orderId).toBe('ORD-NEW') expect(store.selectedOrder?.orderId).toBe('ORD-NEW') expect(store.loading).toBe(false) }) it('adds created order to store', async () => { const store = useOrderStore() const initialCount = store.orders.length await store.createOrder({ customerId: 'CUST-002', totalAmount: 50000 }) expect(store.orders.length).toBeGreaterThan(initialCount) }) it('throws error on creation failure', async () => { const store = useOrderStore() server.use( http.post('*/api/orders', () => { return HttpResponse.json( { message: 'Validation failed' }, { status: 400 } ) }) ) await expect( store.createOrder({ customerId: 'CUST-001' }) ).rejects.toThrow() }) }) describe('updateOrder', () => { it('updates order and reflects changes', async () => { const store = useOrderStore() const orderId = 'ORD-001' const updates = { status: 'CONFIRMED', totalAmount: 150000 } const updatedOrder = await store.updateOrder(orderId, updates) expect(updatedOrder?.status).toBe('CONFIRMED') expect(updatedOrder?.totalAmount).toBe(150000) }) }) describe('deleteOrder', () => { it('removes order from store', async () => { const store = useOrderStore() // Mock: add an order first store.orders.push({ orderId: 'ORD-001', orderNo: 'ORD-001', status: 'DRAFT', totalAmount: 100000 } as any) const initialCount = store.orders.length await store.deleteOrder('ORD-001') expect(store.orders.length).toBeLessThan(initialCount) }) }) describe('filtering', () => { it('applies filters to orders', () => { const store = useOrderStore() store.setFilter('status', 'DRAFT') store.setFilter('customerId', 'CUST-001') expect(store.filters.status).toBe('DRAFT') expect(store.filters.customerId).toBe('CUST-001') }) it('clears all filters', () => { const store = useOrderStore() store.setFilter('status', 'DRAFT') store.setFilter('customerId', 'CUST-001') store.clearFilters() expect(Object.keys(store.filters)).toHaveLength(0) }) }) }) /** * Test Suite: Orders API Client */ describe('Orders API Client', () => { it('lists orders via API', async () => { const orders = await ordersApi.listOrders({ limit: 100 }) expect(orders).toBeDefined() expect(Array.isArray(orders.data || orders)).toBe(true) }) it('gets single order via API', async () => { const order = await ordersApi.getOrder('ORD-001') expect(order.orderId).toBe('ORD-001') }) it('creates order via API', async () => { const payload = { customerId: 'CUST-001', totalAmount: 100000 } const created = await ordersApi.createOrder(payload) expect(created.orderId).toBeDefined() }) })