Skip to content

Commit

Permalink
feat: add unit test TickerService
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasrosa committed Nov 22, 2023
1 parent 4942cc8 commit 65efc9a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 22 deletions.
85 changes: 85 additions & 0 deletions src/services/TickerService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { describe, expect, test, vi } from 'vitest'
import { TickerService } from './TickerService'

const ticker = 'XPLG11'

describe('TickerService', () => {
test('should return result with success', async () => {
const service = new TickerService(ticker)
const result = await service.fetch()

expect(result).toBeDefined()
expect(result.ticker).toBe(ticker)
expect(result.pvp).toBe(1.05)
expect(result.dy12).toBe(8.21)
expect(result.price).toBe(107)
expect(result.dividend12).toBe(8.79)
expect(result.lastDividend).toBe(0.74)
expect(result.dividendsHistory.length).toBe(12)
expect(result.pricesHistory.length).toBeTruthy()
})

test('should return error when ticker is invalid', async () => {
await expect(async () => {
return new TickerService('invalid').fetch()
}).rejects.toThrowError('Ocorreu um erro ao buscar o ticker')
})

test('should return error when ticker is empty', async () => {
await expect(async () => {
return new TickerService('').fetch()
}).rejects.toThrowError('Ocorreu um erro ao buscar o ticker')
})

test('should return error when ticker is null', async () => {
await expect(async () => {
return new TickerService(null as any).fetch()
}).rejects.toThrowError('Ocorreu um erro ao buscar o ticker')
})

test('should return dividendsHistory with array of object', async () => {
const service = new TickerService(ticker)
const result = await service.fetch()

expect(result).toBeDefined()
expect(result.dividendsHistory.length).toBe(12)

expect(result.dividendsHistory[0]).toEqual({
kind: 'cash',
currency: 'brl',
isin_code: 'BRXPLGCTF002',
label: 'Rendimento',
amount: 0.78,
approved_in: '2023-10-31',
traded_until: '2023-10-31',
payment_date: '2023-11-16',
})

expect(result.dividendsHistory[11]).toEqual({
kind: 'cash',
currency: 'brl',
isin_code: 'BRXPLGCTF002',
label: 'Rendimento',
amount: 0.74,
approved_in: '2023-03-31',
traded_until: '2023-03-31',
payment_date: '2023-04-17',
})
})

test('should return pricesHistory with array of object', async () => {
const service = new TickerService(ticker)
const result = await service.fetch()

expect(result).toBeDefined()
expect(result.pricesHistory.length).toBeTruthy()

expect(result.pricesHistory[0]).toEqual({
timestamp: 1700535600000,
avg: 107.7,
min: 107,
max: 108.56,
date: '21/Nov',
})
})
})
51 changes: 29 additions & 22 deletions src/services/TickerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,28 @@ export class TickerService {
}

public async fetch(): Promise<TickerData> {
const { price, pvp } = await this.fetchTicker(this.ticker)
const pricesHistory = await this.fetchPricesHistory(this.ticker)
const dividendsHistory = await this.fetchDividendsHistory(this.ticker)

const { dividend12, dy12, lastDividend } = this.getDividendsByHistory(
dividendsHistory,
price,
)

return {
pvp,
dy12,
price,
dividend12,
lastDividend,
dividendsHistory,
pricesHistory,
ticker: this.ticker,
try {
const { price, pvp } = await this.fetchTicker(this.ticker)
const pricesHistory = await this.fetchPricesHistory(this.ticker)
const dividendsHistory = await this.fetchDividendsHistory(this.ticker)

const { dividend12, dy12, lastDividend } = this.getDividendsByHistory(
dividendsHistory,
price,
)

return {
pvp,
dy12,
price,
dividend12,
lastDividend,
dividendsHistory,
pricesHistory,
ticker: this.ticker,
}
} catch (_err) {
throw new Error('Ocorreu um erro ao buscar o ticker')
}
}

Expand Down Expand Up @@ -156,11 +160,14 @@ export class TickerService {
dividendsHistory: DividendsHistory[],
price: number,
) {
const lastDividend = last(dividendsHistory)?.amount || 0
const lastDividend = round(last(dividendsHistory)?.amount || 0, 2)

const dividend12 = dividendsHistory.reduce((acc, dividend) => {
return acc + dividend.amount
}, 0)
const dividend12 = round(
dividendsHistory.reduce((acc, dividend) => {
return acc + dividend.amount
}, 0),
2,
)

const dy12 = round((dividend12 / price) * 100, 2)

Expand Down

0 comments on commit 65efc9a

Please sign in to comment.