@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
126 lines (125 loc) • 6.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const msw_1 = require("msw");
const node_1 = require("msw/node");
const vitest_1 = require("vitest");
const config_1 = require("./config");
const SDKError_1 = require("./errors/SDKError");
const errors_1 = require("./errors/errors");
const request_1 = require("./request");
const api_unit_handlers_1 = require("./services/api.unit.handlers");
const setup_1 = require("./tests/setup");
const version_1 = require("./version");
const apiUrl = config_1.config.get().apiUrl;
(0, vitest_1.describe)('request new', () => {
const server = (0, node_1.setupServer)(...api_unit_handlers_1.handlers);
(0, vitest_1.beforeAll)(() => {
(0, setup_1.setupTestEnvironment)();
server.listen({
onUnhandledRequest: 'warn',
});
vitest_1.vi.spyOn(global, 'fetch');
});
(0, vitest_1.beforeEach)(() => {
vitest_1.vi.clearAllMocks();
});
(0, vitest_1.afterEach)(() => server.resetHandlers());
(0, vitest_1.afterAll)(() => {
server.close();
vitest_1.vi.clearAllMocks();
});
(0, vitest_1.it)('should be able to successfully make a fetch request', async () => {
const url = `${apiUrl}/advanced/routes`;
const response = await (0, request_1.request)(url, {
method: 'POST',
retries: 0,
});
(0, vitest_1.expect)(response).toEqual({});
});
(0, vitest_1.it)('should remove the extended request init properties that fetch does not care about', async () => {
const url = `${apiUrl}/advanced/routes`;
const successResponse = { message: 'it works' };
server.use(msw_1.http.post(url, async ({ request }) => {
(0, vitest_1.expect)(request.headers.get('x-lifi-integrator')).toEqual('lifi-sdk');
(0, vitest_1.expect)(request.headers.get('x-lifi-sdk')).toEqual(version_1.version);
(0, vitest_1.expect)(request.headers.get('x-lifi-api-key')).toBeNull();
(0, vitest_1.expect)(request.headers.get('x-lifi-userid')).toBeNull();
(0, vitest_1.expect)(request.headers.get('x-lifi-widget')).toBeNull();
return msw_1.HttpResponse.json(successResponse, { status: 200 });
}));
const options = {
method: 'POST',
retries: 0,
};
const response = await (0, request_1.request)(url, options);
(0, vitest_1.expect)(response).toEqual(successResponse);
});
(0, vitest_1.it)('should update the headers information available from config', async () => {
const url = `${apiUrl}/advanced/routes`;
const successResponse = { message: 'it works' };
server.use(msw_1.http.post(url, async ({ request }) => {
(0, vitest_1.expect)(request.headers.get('x-lifi-api-key')).toEqual('mock-apikey');
(0, vitest_1.expect)(request.headers.get('x-lifi-integrator')).toEqual('lifi-sdk');
(0, vitest_1.expect)(request.headers.get('x-lifi-sdk')).toEqual(version_1.version);
(0, vitest_1.expect)(request.headers.get('x-lifi-userid')).toEqual('user-id');
(0, vitest_1.expect)(request.headers.get('x-lifi-widget')).toEqual('mock-widget-version');
return msw_1.HttpResponse.json(successResponse, { status: 200 });
}));
const options = {
method: 'POST',
retries: 0,
headers: {
'x-lifi-api-key': 'mock-apikey',
'x-lifi-userid': 'user-id',
'x-lifi-widget': 'mock-widget-version',
},
};
const response = await (0, request_1.request)(url, options);
(0, vitest_1.expect)(response).toEqual(successResponse);
});
(0, vitest_1.describe)('when dealing with errors', () => {
(0, vitest_1.it)('should throw an error if the Integrator property is missing from the config', async () => {
const originalIntegrator = config_1.config.get().integrator;
config_1.config.set({ integrator: '' });
const url = `${apiUrl}/advanced/routes`;
await (0, vitest_1.expect)((0, request_1.request)(url, {
method: 'POST',
retries: 0,
})).rejects.toThrowError(new SDKError_1.SDKError(new errors_1.ValidationError('You need to provide the Integrator property. Please see documentation https://docs.li.fi/integrate-li.fi-js-sdk/set-up-the-sdk')));
config_1.config.set({ integrator: originalIntegrator });
});
(0, vitest_1.it)('should throw a error with when the request fails', async () => {
vitest_1.expect.assertions(2);
const url = `${apiUrl}/advanced/routes`;
const errorResponse = { message: 'something went wrong on the server' };
server.use(msw_1.http.post(url, async () => {
return msw_1.HttpResponse.json(errorResponse, { status: 400 });
}));
try {
await (0, request_1.request)(url, { method: 'POST', retries: 0 });
}
catch (e) {
(0, vitest_1.expect)(e.name).toEqual('SDKError');
(0, vitest_1.expect)(e.cause.status).toEqual(400);
}
});
(0, vitest_1.it)('should throw a error and attempt retries when the request fails with a 500', async () => {
vitest_1.expect.assertions(2);
const url = `${apiUrl}/advanced/routes`;
const errorResponse = { message: 'something went wrong on the server' };
server.use(msw_1.http.post(url, async () => {
return msw_1.HttpResponse.json(errorResponse, { status: 500 });
}));
try {
await (0, request_1.request)(url, {
method: 'POST',
retries: 0,
});
}
catch (e) {
(0, vitest_1.expect)(e.name).toEqual('SDKError');
(0, vitest_1.expect)(e.cause.status).toEqual(500);
}
});
});
});