Skip to content

Commit

Permalink
test: add unit test for CoinmarketcapClient
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Apr 25, 2024
1 parent e28dd3a commit 4bdac9f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
73 changes: 73 additions & 0 deletions src/clients/coinmarketcap-client.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Test, TestingModule } from '@nestjs/testing';

import { CoinmarketcapClientService } from '@/clients/coinmarketcap-client.service';
import { HttpService } from '@/clients/http.service';
import resetAllMocks = jest.resetAllMocks;
import { RateLimiter } from 'limiter';

import { coinmarketCapResponseAeUsdQuoteData } from '@/test/mock-data/pair-liquidity-info-history-mock-data';

const mockHttpService = {
get: jest.fn(),
};

describe('CoinmarketcapClientService', () => {
let service: CoinmarketcapClientService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CoinmarketcapClientService,
{ provide: HttpService, useValue: mockHttpService },
],
}).compile();
service = module.get<CoinmarketcapClientService>(
CoinmarketcapClientService,
);
resetAllMocks();
});

describe('getHistoricalPriceDataThrottled', () => {
it('should correctly calculate and fetch the latest 5 min interval for a given timestamp', async () => {
// Mock functions
mockHttpService.get.mockResolvedValue(
coinmarketCapResponseAeUsdQuoteData,
);
// Call function
await service.getHistoricalPriceDataThrottled(1704203935123);

// Assertions
expect(mockHttpService.get).toHaveBeenCalledWith(
'https://pro-api.coinmarketcap.com/v3/cryptocurrency/quotes/historical?id=1700&interval=24h&count=1&time_end=2024-01-02T13:55:00.000Z',
expect.anything(),
);

await service.getHistoricalPriceDataThrottled(1704203614123);

expect(mockHttpService.get).toHaveBeenCalledWith(
'https://pro-api.coinmarketcap.com/v3/cryptocurrency/quotes/historical?id=1700&interval=24h&count=1&time_end=2024-01-02T13:50:00.000Z',
expect.anything(),
);
});

it('should be throtteled to defined rate limit', async () => {
service['rateLimiter'] = new RateLimiter({
tokensPerInterval: 2,
interval: 'minute',
});

// Mock
mockHttpService.get.mockResolvedValue({});

// Call function
service.getHistoricalPriceDataThrottled(1704203935123);
service.getHistoricalPriceDataThrottled(1704203935123);
service.getHistoricalPriceDataThrottled(1704203935123);

await new Promise((res) => setTimeout(res, 200));

// Assertion
expect(mockHttpService.get).toHaveBeenCalledTimes(2);
});
});
});
6 changes: 3 additions & 3 deletions src/clients/coinmarketcap-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { HttpService } from '@/clients/http.service';
export class CoinmarketcapClientService {
constructor(private httpService: HttpService) {}

private readonly AE_CURRENCY_ID = 1700;
private readonly COUNT = 1;
private readonly INTERVAL = '24h';
private readonly AUTH_HEADER = {
'X-CMC_PRO_API_KEY': process.env.COIN_MARKET_CAP_API_KEY || '',
};
private readonly AE_CURRENCY_ID = 1700;
private readonly COUNT = 1;
private readonly INTERVAL = '24h';
private readonly CALLS_LIMIT = 28;
private readonly CALL_INTERVAL = 'minute';
private rateLimiter = new RateLimiter({
Expand Down

0 comments on commit 4bdac9f

Please sign in to comment.