Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fx test configuration #9

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading