From 9d7f45ee0cce17620236de2e86505ed86c7fd022 Mon Sep 17 00:00:00 2001 From: okjodom Date: Sat, 2 Nov 2024 07:50:49 +0300 Subject: [PATCH] fix: fx test configuratin --- apps/swap/src/fx/fx.service.spec.ts | 23 +++++++++++++++++++---- apps/swap/src/fx/fx.service.ts | 8 +++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/apps/swap/src/fx/fx.service.spec.ts b/apps/swap/src/fx/fx.service.spec.ts index a27de6d..a3a4bfc 100644 --- a/apps/swap/src/fx/fx.service.spec.ts +++ b/apps/swap/src/fx/fx.service.spec.ts @@ -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'; @@ -10,7 +13,7 @@ const mock_rate = 8708520.117232416; describe('FxService', () => { let fxService: FxService; let mockCfg: { get: jest.Mock }; - let mockCacheManager: any; + let mockCacheManager: CustomStore; beforeEach(async () => { mockCfg = { @@ -18,9 +21,13 @@ describe('FxService', () => { }; 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()], @@ -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', ); @@ -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', ); diff --git a/apps/swap/src/fx/fx.service.ts b/apps/swap/src/fx/fx.service.ts index 93ec113..c19709a 100644 --- a/apps/swap/src/fx/fx.service.ts +++ b/apps/swap/src/fx/fx.service.ts @@ -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;