Skip to content

Commit

Permalink
feature #3 - Add controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeniosales committed Oct 15, 2022
1 parent 59e2f9c commit eff658d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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
}
}
}
5 changes: 5 additions & 0 deletions src/3-adapters/controllerBase.ts
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>>
}
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()
})
})

0 comments on commit eff658d

Please sign in to comment.