Skip to content

Commit

Permalink
fix: fx test configuratin
Browse files Browse the repository at this point in the history
  • Loading branch information
okjodom committed Nov 2, 2024
1 parent 791472f commit 9d7f45e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
23 changes: 19 additions & 4 deletions apps/swap/src/fx/fx.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { TestingModule } from '@nestjs/testing';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { createTestingModuleWithValidation } from '@bitsacco/common';
import {
createTestingModuleWithValidation,
CustomStore,
} from '@bitsacco/common';
import { CacheModule } from '@nestjs/cache-manager';
import { HttpModule } from '@nestjs/axios';
import { FxService } from './fx.service';
Expand All @@ -10,17 +13,21 @@ const mock_rate = 8708520.117232416;
describe('FxService', () => {
let fxService: FxService;
let mockCfg: { get: jest.Mock };
let mockCacheManager: any;
let mockCacheManager: CustomStore;

beforeEach(async () => {
mockCfg = {
get: jest.fn(),
};

mockCacheManager = {
get: jest.fn(),
get: jest.fn().mockImplementation((key: string) => {
return Promise.resolve({
btcToKesRate: mock_rate,
});
}),
set: jest.fn(),
};
} as unknown as CustomStore;

const module: TestingModule = await createTestingModuleWithValidation({
imports: [ConfigModule, HttpModule, CacheModule.register()],
Expand Down Expand Up @@ -69,6 +76,10 @@ describe('FxService', () => {
}
});

(mockCacheManager.get as jest.Mock).mockImplementation(() => {
return Promise.reject(new Error('cache miss'));
});

await expect(fxService.getBtcToKesRate()).rejects.toThrow(
'Either CURRENCY_API_KEY or MOCK_BTC_KES_RATE must be configured',
);
Expand Down Expand Up @@ -99,6 +110,10 @@ describe('FxService', () => {
}
});

(mockCacheManager.get as jest.Mock).mockImplementation(() => {
return Promise.reject(new Error('cache miss'));
});

await expect(fxService.getBtcToKesRate()).rejects.toThrow(
'CURRENCY_API_KEY not found',
);
Expand Down
8 changes: 5 additions & 3 deletions apps/swap/src/fx/fx.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ export class FxService {
}

private async getCurrencyApiRates() {
const cachedData = await this.cacheManager.get<{
btcToKesRate: string;
} | void>(this.CACHE_KEY);
const cachedData = await this.cacheManager
.get<{
btcToKesRate: string;
}>(this.CACHE_KEY)
.catch((_) => undefined);
if (cachedData) {
this.logger.log('Returning cached currency rates');
return cachedData;
Expand Down

0 comments on commit 9d7f45e

Please sign in to comment.