import { whenRequest } from '@wix/fe-essentials/http-client/testkit/client'; import { queryOrders } from '@wix/ambassador-ecom-v1-order/http'; import { Order, PaymentStatus, FulfillmentStatus, OrderStatus, } from '@wix/ambassador-ecom-v1-order/types'; import { InMemoryBackend } from '../testkit/backend'; import { Chance } from 'chance'; export function createOrder(chance: Chance.Chance) { return ( { createdDate = chance.date({ min: new Date(Date.now() - 365 * 24 * 60 * 60 * 1000), max: new Date(), }) as Date, updatedDate = createdDate, ...overrides }: Partial = {}, index: number, ) => ({ id: chance.guid(), number: index * 1000, createdDate, updatedDate, paymentStatus: chance.pickone(Object.values(PaymentStatus)), fulfillmentStatus: chance.pickone(Object.values(FulfillmentStatus)), status: chance.pickone(Object.values(OrderStatus)), buyerNote: chance.sentence(), ...overrides, } as Order); } export function createBackend() { const chance = new Chance(); return new InMemoryBackend({ total: 150, paginationMode: 'cursor', delay: { min: 100, max: 2000 }, createOne: (index) => createOrder(chance)({}, index), predicate: (query) => { const { search } = query; const filters = query.filters ?? {}; const rgx = search ? new RegExp(search, 'i') : null; return (item) => { if (rgx != null && !rgx.test(item.buyerNote!)) { return false; } return !( filters.paymentStatus && filters.paymentStatus.length > 0 && filters.paymentStatus.indexOf(item.paymentStatus) === -1 ); }; }, orderBy: (query) => { return [...(query.sort ?? [])]; }, itemKey: (item) => item.id!, }); } export const ordersAPIHttpClientMocks = () => { const ordersBackend = createBackend(); return [ whenRequest(queryOrders) .reply(200, async (data: any) => { const { query: { cursorPaging: { limit, cursor }, }, search, } = data; return ordersBackend .fetchData({ page: 1, limit, cursor, search, filters: {}, }) .then(({ items, total, cursor: newCursor }) => ({ orders: items, metadata: { total, cursors: { next: newCursor }, }, })); }) .persist(), ]; };