-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59e2f9c
commit eff658d
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/3-adapters/controller/exchangeRate/updateExchangeRateController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IExchangeRateRepository } from '../../../2-business/repositories/iExchangeRateRepository' | ||
import { IExchangeRateService } from '../../../2-business/services/iExchangeRateService' | ||
import { UpdateExchangeRateUseCase } from '../../../2-business/useCases/exchangeRate/updateExchangeRateUseCase' | ||
import { ControllerBase } from '../../controllerBase' | ||
import { Output } from '../../../2-business/dto/output' | ||
|
||
export class UpdateExchangeRateController extends ControllerBase<void, void> { | ||
constructor ( | ||
private readonly exchangeRateRepository: IExchangeRateRepository, | ||
private readonly exchangeRateService: IExchangeRateService | ||
) { super() } | ||
|
||
async run (): Promise<Output<void>> { | ||
const logPrefix = 'UpdateExchangeRateController' | ||
console.log(`${logPrefix} :: start`) | ||
|
||
try { | ||
const updateExchangeRateUseCase = new UpdateExchangeRateUseCase( | ||
this.exchangeRateRepository, | ||
this.exchangeRateService | ||
) | ||
|
||
await updateExchangeRateUseCase.execute() | ||
|
||
console.log(`${logPrefix} :: end`) | ||
|
||
return { status: 'success' } | ||
} catch (error) { | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Output } from '../2-business/dto/output' | ||
|
||
export abstract class ControllerBase<I, O> { | ||
abstract run (input: I): Promise<Output<O>> | ||
} |
56 changes: 56 additions & 0 deletions
56
test/unit/3-adpters/exchangeRate/updateExchangeRateController.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { UpdateExchangeRateController } from '../../../../src/3-adapters/controller/exchangeRate/updateExchangeRateController' | ||
import { IExchangeRateRepository } from '../../../../src/2-business/repositories/iExchangeRateRepository' | ||
import { IExchangeRateService } from '../../../../src/2-business/services/iExchangeRateService' | ||
import { ILatestRatesResponse } from '../../../../src/1-domain/models/iExchangeRateResponse' | ||
|
||
jest.mock('../../../../src/2-business/useCases/exchangeRate/updateExchangeRateUseCase') | ||
|
||
describe('UpdateExchangeRateController', () => { | ||
const exchangeRateResponseMock: ILatestRatesResponse = { | ||
success: true, | ||
timestamp: 1519296206, | ||
base: 'BRL', | ||
date: '2021-03-17', | ||
rates: { | ||
AUD: 1.566015, | ||
CAD: 1.560132, | ||
CHF: 1.154727, | ||
CNY: 7.827874, | ||
GBP: 0.882047, | ||
JPY: 132.360679, | ||
USD: 1.23396 | ||
} | ||
} | ||
|
||
let exchangeRateRepository: IExchangeRateRepository | ||
let exchangeRateService: IExchangeRateService | ||
let UpdateExchangeRateUseCase = jest.fn() | ||
|
||
const setMocks = () => { | ||
exchangeRateRepository = { | ||
upsert: jest.fn().mockResolvedValue(null) | ||
} | ||
exchangeRateService = { | ||
getLatestRates: jest.fn().mockResolvedValue(exchangeRateResponseMock) | ||
} | ||
UpdateExchangeRateUseCase.mockClear() | ||
} | ||
|
||
beforeEach(() => { | ||
setMocks() | ||
}) | ||
|
||
test('Success::should update the exchange rates successfully', async () => { | ||
UpdateExchangeRateUseCase.mockImplementation(() => { | ||
return { | ||
execute: jest.fn() | ||
} | ||
}) | ||
const controller = new UpdateExchangeRateController( | ||
exchangeRateRepository, | ||
exchangeRateService | ||
) | ||
|
||
await expect(controller.run()).resolves.not.toThrow() | ||
}) | ||
}) |