@yoroi/exchange
Version:
The Exchange package of Yoroi SDK
106 lines (105 loc) • 3.41 kB
JavaScript
"use strict";
var _types = require("@yoroi/types");
var _api = require("./api");
var _api2 = require("./api.mocks");
describe('getEncryptusBaseUrl', () => {
it('should return the link - sandbox', async () => {
const isProduction = false;
const expectedUrl = _api.encryptusApiConfig.sandbox.getBaseUrl;
const mockFetchDataResponse = {
tag: 'right',
value: {
data: _api2.getEncryptusBaseUrlResponse,
status: 200
}
};
const mockFetchData = jest.fn().mockReturnValue(mockFetchDataResponse);
const getBaseUrl = (0, _api.encryptusApiGetBaseUrl)({
isProduction
}, {
request: mockFetchData
});
const result = await getBaseUrl();
expect(mockFetchData).toHaveBeenCalledWith({
url: expectedUrl
}, undefined);
expect(result).toBe(_api2.getEncryptusBaseUrlResponse.data.link);
});
it('should return the link - production', async () => {
const isProduction = true;
const expectedUrl = _api.encryptusApiConfig.production.getBaseUrl;
const mockFetchDataResponse = {
tag: 'right',
value: {
data: _api2.getEncryptusBaseUrlResponse,
status: 200
}
};
const mockFetchData = jest.fn().mockReturnValue(mockFetchDataResponse);
const getBaseUrl = (0, _api.encryptusApiGetBaseUrl)({
isProduction
}, {
request: mockFetchData
});
const result = await getBaseUrl();
expect(mockFetchData).toHaveBeenCalledWith({
url: expectedUrl
}, undefined);
expect(result).toBe(_api2.getEncryptusBaseUrlResponse.data.link);
});
it('should throw invalid response if the response doesnt contain the link', async () => {
const isProduction = false;
const expectedUrl = _api.encryptusApiConfig.sandbox.getBaseUrl;
const invalidApiResponse = {
..._api2.getEncryptusBaseUrlResponse,
data: {}
};
const mockFetchDataResponse = {
tag: 'right',
value: {
data: invalidApiResponse,
status: 200
}
};
const mockFetchData = jest.fn().mockReturnValue(mockFetchDataResponse);
const getBaseUrl = (0, _api.encryptusApiGetBaseUrl)({
isProduction
}, {
request: mockFetchData
});
await expect(() => getBaseUrl()).rejects.toThrow(_types.Exchange.Errors.Validation);
expect(mockFetchData).toHaveBeenCalledWith({
url: expectedUrl
}, undefined);
});
it('should throw unknow error if throws an error', async () => {
const isProduction = false;
const expectedUrl = _api.encryptusApiConfig.sandbox.getBaseUrl;
const errorApiResponse = {
status: 404,
message: 'Mega Bad Error',
responseData: null
};
const mockFetchDataResponse = {
tag: 'left',
error: errorApiResponse
};
const mockFetchData = jest.fn().mockReturnValue(mockFetchDataResponse);
const getBaseUrl = (0, _api.encryptusApiGetBaseUrl)({
isProduction
}, {
request: mockFetchData
});
await expect(() => getBaseUrl()).rejects.toThrow(_types.Api.Errors.NotFound);
expect(mockFetchData).toHaveBeenCalledWith({
url: expectedUrl
}, undefined);
});
it('should build without dependencies (coverage only)', () => {
const getCryptoAddress = (0, _api.encryptusApiGetBaseUrl)({
isProduction: true
});
expect(getCryptoAddress).toBeDefined();
});
});
//# sourceMappingURL=api.test.js.map