import { describe, it, expect, vi } from 'vitest';
import { createStorefrontClient } from '@craveup/storefront-sdk';
function createMockClient(responses: Record<string, unknown>) {
const mockFetch = vi.fn(async (url: string) => ({
ok: true,
status: 200,
headers: new Headers({ 'content-length': '1' }),
json: async () => responses[url] ?? {},
text: async () => JSON.stringify(responses[url] ?? {}),
}));
const client = createStorefrontClient({
apiKey: 'sk_test_mock',
fetch: mockFetch as unknown as typeof fetch,
});
return { client, mockFetch };
}
describe('cart', () => {
it('adds an item and returns updated cart', async () => {
const { client } = createMockClient({
'https://api.craveup.com/api/v1/locations/loc_123/carts/cart_456/cart-item': {
cartId: 'cart_456',
cart: {
id: 'cart_456',
totalQuantity: 1,
orderTotalWithServiceFee: '12.99',
},
},
});
const result = await client.cart.addItem('loc_123', 'cart_456', {
productId: 'prod_margherita',
quantity: 1,
selections: [],
itemUnavailableAction: 'remove_item',
});
expect(result.cart.totalQuantity).toBe(1);
expect(result.cart.orderTotalWithServiceFee).toBe('12.99');
});
});