UNPKG

tonb-merchant-api-client

Version:

Merchant API client is a library to interact with TONB Merchant API.

268 lines (267 loc) 11 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const axios_mock_adapter_1 = __importDefault(require("axios-mock-adapter")); const provider_1 = require("./provider"); const invoice_1 = require("../../models/invoice"); jest.mock('axios', () => { return Object.assign(Object.assign({}, jest.requireActual('axios')), { create: jest.fn().mockReturnValue(jest.requireActual('axios')) }); }); const mockAdapter = new axios_mock_adapter_1.default(axios_1.default); describe('HttpProvider', () => { let httpProvider; let axiosInstance; beforeEach(() => { axiosInstance = axios_1.default.create({ baseURL: 'http://example.com/', }); httpProvider = new provider_1.HttpProvider({ client: axiosInstance }); }); afterEach(() => { jest.clearAllMocks(); }); describe('createInvoice', () => { it('should send a POST request with the correct data and return the response', () => __awaiter(void 0, void 0, void 0, function* () { const data = { order_id: 1, amount: 1, }; const responseData = { data: { id: '1', amount: '1', order_id: '1', status: invoice_1.InvoiceEnumStatus.created, createdAt: '2023-04-18T12:55:22.951Z', updatedAt: '2023-04-18T12:55:22.951Z', }, }; const expected = { data: { id: 1, amount: BigInt(1), order_id: BigInt(1), status: invoice_1.InvoiceEnumStatus.created, createdAt: Date.parse('2023-04-18T12:55:22.951Z'), updatedAt: Date.parse('2023-04-18T12:55:22.951Z'), }, isValid: true, }; mockAdapter.onPost('/invoice').reply(200, responseData); const invoice = yield httpProvider.createInvoice(data); expect(invoice).toEqual(expected); })); it('should handle HTTP error', () => __awaiter(void 0, void 0, void 0, function* () { const data = { order_id: 1, amount: 1, }; const responseData = {}; const expected = { data: null, isValid: false, error: expect.any(Error), }; mockAdapter.onPost('/invoice').reply(500, responseData); const invoice = yield httpProvider.createInvoice(data); expect(invoice).toEqual(expected); })); it('should handle unexpected empty body', () => __awaiter(void 0, void 0, void 0, function* () { const data = { order_id: 1, amount: 1, }; const expected = { data: null, isValid: false, }; mockAdapter.onPost('/invoice').reply(200, null); const invoice = yield httpProvider.createInvoice(data); expect(invoice).toEqual(expected); })); }); describe('cancelInvoice', () => { it('should send a PATCH request with the correct data and return the response', () => __awaiter(void 0, void 0, void 0, function* () { const invoiceID = 1; const responseData = { data: { id: 1, amount: 1, order_id: 1, status: invoice_1.InvoiceEnumStatus.canceled, }, }; const expected = { data: { id: 1, amount: BigInt(1), order_id: BigInt(1), status: invoice_1.InvoiceEnumStatus.canceled, }, isValid: true, }; mockAdapter.onPatch('/invoice/cancel').reply(200, responseData); const invoice = yield httpProvider.cancelInvoice(invoiceID); expect(invoice).toEqual(expected); })); it('should handle HTTP error', () => __awaiter(void 0, void 0, void 0, function* () { const invoiceID = 1; const responseData = {}; const expected = { data: null, isValid: false, error: expect.any(Error), }; mockAdapter.onPatch('/invoice/cancel').reply(500, responseData); const invoice = yield httpProvider.cancelInvoice(invoiceID); expect(invoice).toEqual(expected); })); it('should handle unexpected empty body', () => __awaiter(void 0, void 0, void 0, function* () { const invoiceID = 1; const expected = { data: null, isValid: false, error: expect.any(Error), }; mockAdapter.onPatch('/invoice/cancel').reply(200, null); const invoice = yield httpProvider.getInvoiceInfo(invoiceID); expect(invoice).toEqual(expected); })); }); describe('getInvoiceInfo', () => { it('should send a GET request with the correct data and return the response', () => __awaiter(void 0, void 0, void 0, function* () { const invoiceID = 1; const responseData = { data: { id: '1', amount: 1, order_id: '1', status: invoice_1.InvoiceEnumStatus.created, domain: 'abc.com', wallet_from_id: '12345', }, }; const expected = { data: { id: 1, amount: BigInt(1), order_id: BigInt(1), status: invoice_1.InvoiceEnumStatus.created, domain: 'abc.com', wallet_from_id: 12345, }, isValid: true, }; mockAdapter.onGet('/invoice/info?id=1').reply(200, responseData); const invoice = yield httpProvider.getInvoiceInfo(invoiceID); expect(invoice).toEqual(expected); })); it('should handle HTTP error', () => __awaiter(void 0, void 0, void 0, function* () { const invoiceID = 1; const responseData = {}; const expected = { data: null, isValid: false, error: expect.any(Error), }; mockAdapter.onGet('/invoice/info?id=1').reply(404, responseData); const invoice = yield httpProvider.getInvoiceInfo(invoiceID); expect(invoice).toEqual(expected); })); it('should handle unexpected empty body', () => __awaiter(void 0, void 0, void 0, function* () { const invoiceID = 1; const expected = { data: null, isValid: false, }; mockAdapter.onGet('/invoice/info?id=1').reply(200, null); const invoice = yield httpProvider.getInvoiceInfo(invoiceID); expect(invoice).toEqual(expected); })); }); describe('getInvoiceStats', () => { it('should send a GET request with the correct data and return the response', () => __awaiter(void 0, void 0, void 0, function* () { const responseData = { data: { count: 1, invoice_sum: 10, }, }; const expected = { data: { count: 1, invoice_sum: 10, }, isValid: true, }; mockAdapter.onGet('/invoice/stats').reply(200, responseData); const stats = yield httpProvider.getInvoiceStats(); expect(stats).toEqual(expected); })); it('should handle HTTP error', () => __awaiter(void 0, void 0, void 0, function* () { const responseData = { data: { count: 1, invoice_sum: 10, }, }; const expected = { data: null, isValid: false, error: expect.any(Error), }; mockAdapter.onGet('/invoice/stats').reply(403, responseData); const stats = yield httpProvider.getInvoiceStats(); expect(stats).toEqual(expected); })); it('should handle unexpected empty body', () => __awaiter(void 0, void 0, void 0, function* () { const expected = { data: null, isValid: false, }; mockAdapter.onGet('/invoice/stats').reply(200, null); const stats = yield httpProvider.getInvoiceStats(); expect(stats).toEqual(expected); })); }); describe('isUpdateValid', () => { it('should return true for a valid update', () => { const update = { data: { status: invoice_1.InvoiceEnumStatus.canceled, code: '123456789', amount: 500, order_id: 12345, sign: '9403ff6d2edd7eede8a1172c3eb0dbab5a9a316c90fcafccea3a937618176580', }, }; const isValid = httpProvider.isUpdateValid(update); expect(isValid).toBe(true); }); it('should return false for an invalid update', () => { const update = { data: { status: invoice_1.InvoiceEnumStatus.canceled, code: '123456789', amount: 500, order_id: 12345, sign: 'invalid_sign', // Replace with an invalid sign }, }; const isValid = httpProvider.isUpdateValid(update); expect(isValid).toBe(false); }); }); });