import * as Querystring from 'querystring' import { ICreate, IShow, OrderAPI } from '../../src/apis/order' import { is, isNil, merge } from 'ramda' import Nock from 'nock' import { ORDER_API_URL } from '../../src/lib/constants' import { makeError } from '@oyst/utils' import { removeUndefinedKeys } from '@oyst/utils/lib/utils' export class MockOrderAPI { private _fixture: Object = null private _code: number = 200 private endpoint: string = null constructor (init: any = {}) { this.endpoint = ORDER_API_URL } public fixture (value: Object | string) { this._fixture = is(String, value) ? JSON.parse(value as string) : value return this } public success (code?: number): MockOrderAPI { this._code = code ? code : 200 return this } public fail (code?: number): MockOrderAPI { this._code = code ? code : 500 return this } public show (opts: IShow): Promise { return new Promise((resolve, reject) => { Nock(this.endpoint) .filteringRequestBody(_ => '*') .get(`/v2/merchants/${opts.merchantId}/orders/users/${opts.userId}`, '*') .reply(this._code, this._fixture) resolve() }) } public create (opts?: ICreate, version: number = 1): Promise { console.info(version) return new Promise((resolve, reject) => { Nock(this.endpoint) .filteringRequestBody(_ => '*') .post(`/v${version}/orders`, '*').reply(this._code, this._fixture) resolve() }) } public delete (id: string, version: number = 1): Promise { return new Promise((resolve, reject) => { Nock(this.endpoint) .delete(`/v${version}/orders/${id}`).reply(this._code, this._fixture) resolve() }) } public updateItem (orderId: string, itemId: string, payload: any): Promise { if (payload.quantity < 1) throw makeError('ORDER-400', 'quantity-must-be-positive') Object.assign(this._fixture.order.items.find(i => i.id === itemId), payload) return new Promise((resolve, reject) => { Nock(this.endpoint) .patch(`/v2/orders/${orderId}/items/${itemId}`).reply(this._code, this._fixture) resolve() }) } } export default MockOrderAPI