@yoroi/portfolio
Version:
The Portfolio package of Yoroi SDK
97 lines • 4.52 kB
JavaScript
import { toDullahanRequest, toProcessedMediaRequest, toSecondaryTokenInfos, toTokenActivity, toTokenHistory } from './transformers';
import { Api } from '@yoroi/types';
import { tokenMocks } from '../token.mocks';
import { tokenActivityMocks } from '../token-activity.mocks';
import { duallahanTokenActivityMocks } from './token-activity.mocks';
import { tokenHistoryMocks } from '../token-history.mocks';
describe('transformers', () => {
describe('toSecondaryTokenInfos', () => {
it('should return an empty object if apiTokenInfosResponse is empty', () => {
const apiTokenInfosResponse = {};
const result = toSecondaryTokenInfos(apiTokenInfosResponse);
expect(result).toEqual({});
});
it('should return the updated token info if the status code is not HttpStatusCode.NotModified', () => {
const apiTokenInfosResponse = {
[tokenMocks.primaryETH.info.id]: [Api.HttpStatusCode.NotModified, 123456]
};
const result = toSecondaryTokenInfos(apiTokenInfosResponse);
expect(result).toEqual(apiTokenInfosResponse);
});
it('should return the original token info if the status code is HttpStatusCode.NotModified', () => {
const apiTokenInfosResponse = {
[tokenMocks.rnftWhatever.info.id]: [Api.HttpStatusCode.NotModified, 123456],
[tokenMocks.nftCryptoKitty.info.id]: [Api.HttpStatusCode.Ok, tokenMocks.nftCryptoKitty.info, 'etag', 0]
};
const result = toSecondaryTokenInfos(apiTokenInfosResponse);
expect(result).toEqual({
[tokenMocks.rnftWhatever.info.id]: [Api.HttpStatusCode.NotModified, 123456],
[tokenMocks.nftCryptoKitty.info.id]: [200, tokenMocks.nftCryptoKitty.info, 'etag', 0]
});
});
it('should return drop the record if unable to parse the token-info response', () => {
const apiTokenInfosResponse = {
[tokenMocks.primaryETH.info.id]: [2000, tokenMocks.primaryETH.info, 'etag', 0],
[tokenMocks.nftCryptoKitty.info.id]: [Api.HttpStatusCode.Ok, tokenMocks.nftCryptoKitty.info, 'etag', 0]
};
const result = toSecondaryTokenInfos(apiTokenInfosResponse);
expect(result).toEqual({
[tokenMocks.nftCryptoKitty.info.id]: [200, tokenMocks.nftCryptoKitty.info, 'etag', 0]
});
});
it('should return drop the record when the status code is HttpStatusCode.InternalServerError', () => {
const apiTokenInfosResponse = {
[tokenMocks.rnftWhatever.info.id]: [Api.HttpStatusCode.InternalServerError, 'Not found', 3600],
[tokenMocks.nftCryptoKitty.info.id]: [Api.HttpStatusCode.Ok, tokenMocks.nftCryptoKitty.info, 'etag', 0]
};
const result = toSecondaryTokenInfos(apiTokenInfosResponse);
expect(result).toEqual({
[tokenMocks.nftCryptoKitty.info.id]: [200, tokenMocks.nftCryptoKitty.info, 'etag', 0]
});
});
});
describe('toTokenActivity', () => {
it('should return an empty object if apiTokenActivity response is empty', () => {
const apiTokenInfosResponse = {};
expect(toTokenActivity(apiTokenInfosResponse)).toEqual({});
});
it('should return the data and deal with empty records', () => {
const responseWithEmptyRecords = {
...duallahanTokenActivityMocks.api.responseSuccessDataOnly,
'token.4': undefined,
'token.5': [Api.HttpStatusCode.InternalServerError, 'Not found']
};
const result = toTokenActivity(responseWithEmptyRecords);
expect(result).toEqual(tokenActivityMocks.api.responseDataOnly);
});
});
describe('toTokenHistory', () => {
it('should return undefined if apiTokenHistory response is malformed', () => {
expect(toTokenHistory({
whatever: false
})).toEqual(undefined);
});
it('should return the data', () => {
const result = toTokenHistory(tokenHistoryMocks.ftNamelessRaw);
expect(result).toEqual(tokenHistoryMocks.api.responseDataOnly);
});
});
});
describe('toDullahanRequest', () => {
it('success', () => {
const request = [['token.1', 'hash1'], ['token.2', 'hash2'], ['token.3', 'hash3']];
const result = toDullahanRequest(request);
expect(result).toEqual(['token.1:hash1', 'token.2:hash2', 'token.3:hash3']);
});
});
describe('toProcessedMediaRequest', () => {
it('success', () => {
const request = 'token.1';
const result = toProcessedMediaRequest(request);
expect(result).toEqual({
policy: 'token',
name: '1'
});
});
});
//# sourceMappingURL=transformers.test.js.map