From 586d6eb728915cc3ddda8c86102250ef08cf990e Mon Sep 17 00:00:00 2001 From: Elias da Rosa Date: Wed, 22 Nov 2023 15:10:34 -0300 Subject: [PATCH] feat: add unit tests --- src/actions/portfoliosActions.test.ts | 48 +++++++++++ src/actions/tickersActions.test.ts | 68 +++++++++++++++ src/components/common/Title.tsx | 2 +- src/components/layout/Main.tsx | 2 +- src/components/layout/Sidebar.tsx | 4 +- .../pages/home/history-chart/index.tsx | 8 +- .../pages/home/stocks-cards/StockCard.tsx | 2 +- src/config.test.ts | 73 ++++++++++++++++ src/config.ts | 4 +- src/helpers/cn.test.ts | 9 ++ src/helpers/cn.ts | 6 ++ src/helpers/currency.test.ts | 12 +++ src/helpers/fetchStocks.ts | 26 ------ src/helpers/getLastDays.test.ts | 14 +++ src/helpers/getLastDays.ts | 17 ++++ src/helpers/getLastMonths.test.ts | 14 +++ src/helpers/getLastMonths.ts | 14 +++ src/helpers/mergeArrayOfObjects.test.ts | 10 +++ src/helpers/mergeArrayOfObjects.ts | 5 ++ src/helpers/utils.ts | 42 --------- src/services/TickerService.test.ts | 85 +++++++++++++++++++ src/services/TickerService.ts | 51 ++++++----- 22 files changed, 414 insertions(+), 102 deletions(-) create mode 100644 src/actions/portfoliosActions.test.ts create mode 100644 src/actions/tickersActions.test.ts create mode 100644 src/config.test.ts create mode 100644 src/helpers/cn.test.ts create mode 100644 src/helpers/cn.ts create mode 100644 src/helpers/currency.test.ts delete mode 100644 src/helpers/fetchStocks.ts create mode 100644 src/helpers/getLastDays.test.ts create mode 100644 src/helpers/getLastDays.ts create mode 100644 src/helpers/getLastMonths.test.ts create mode 100644 src/helpers/getLastMonths.ts create mode 100644 src/helpers/mergeArrayOfObjects.test.ts create mode 100644 src/helpers/mergeArrayOfObjects.ts delete mode 100644 src/helpers/utils.ts create mode 100644 src/services/TickerService.test.ts diff --git a/src/actions/portfoliosActions.test.ts b/src/actions/portfoliosActions.test.ts new file mode 100644 index 0000000..9ed7300 --- /dev/null +++ b/src/actions/portfoliosActions.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, test } from 'vitest' +import { portfoliosActions } from './portfoliosActions' +import { Portfolio } from '@/@types/PortfoliosTypes' + +describe('portfoliosActions', () => { + const initialState: Portfolio[] = [ + { id: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', name: 'Portfolio 1' }, + { id: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', name: 'Portfolio 2' }, + { id: 'b33c91c4-2661-462c-8cbd-a4501f629351', name: 'Portfolio 3' }, + ] + + test('should update the portfolio with the given payload', () => { + const newState = portfoliosActions.update(initialState, { + id: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', + name: 'Portfolio Edited', + }) + + expect(newState).toEqual([ + { id: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', name: 'Portfolio 1' }, + { id: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', name: 'Portfolio Edited' }, + { id: 'b33c91c4-2661-462c-8cbd-a4501f629351', name: 'Portfolio 3' }, + ]) + }) + + test('should insert a new portfolio into the state', () => { + const newState = portfoliosActions.insert(initialState, { + name: 'Portfolio New', + }) + + expect(newState).toEqual([ + { id: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', name: 'Portfolio 1' }, + { id: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', name: 'Portfolio 2' }, + { id: 'b33c91c4-2661-462c-8cbd-a4501f629351', name: 'Portfolio 3' }, + { id: expect.any(String), name: 'Portfolio New' }, + ]) + }) + + test('should remove the specified portfolio from the state', () => { + const newState = portfoliosActions.remove(initialState, { + id: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', + }) + + expect(newState).toEqual([ + { id: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', name: 'Portfolio 1' }, + { id: 'b33c91c4-2661-462c-8cbd-a4501f629351', name: 'Portfolio 3' }, + ]) + }) +}) diff --git a/src/actions/tickersActions.test.ts b/src/actions/tickersActions.test.ts new file mode 100644 index 0000000..1f1eb8b --- /dev/null +++ b/src/actions/tickersActions.test.ts @@ -0,0 +1,68 @@ +/* eslint-disable prettier/prettier */ +import { describe, expect, test } from 'vitest' +import { Ticker } from '@/@types/TickersTypes' +import { tickersActions } from './tickersActions' + +describe('tickersActions', () => { + const initialState: Ticker[] = [ + { ticker: 'AABB11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + { ticker: 'AABB11', portfolioId: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', isHidden: false, quantity: 1 }, + { ticker: 'AAXX11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + ] + + describe('insert', () => { + test('should insert new tickers into the state', () => { + const tickersList = ['AABB11', 'AAXX11'] + const portfoliosList = [ + 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', + 'bdcfe013-ae51-4f79-b8a1-791f745e9116', + ] + + const newState = tickersActions.insert(initialState, { + tickersList, + portfoliosList, + }) + + expect(newState).toEqual([ + { ticker: 'AABB11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + { ticker: 'AABB11', portfolioId: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', isHidden: false, quantity: 1 }, + { ticker: 'AAXX11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + { ticker: 'AAXX11', portfolioId: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', isHidden: false, quantity: 1 }, + ]) + }) + }) + + describe('remove', () => { + test('should remove the specified ticker from the state', () => { + const ticker = 'AAXX11' + const portfolioId = 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b' + + const newState = tickersActions.remove(initialState, { + ticker, + portfolioId, + }) + + expect(newState).toEqual([ + { ticker: 'AABB11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + { ticker: 'AABB11', portfolioId: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', isHidden: false, quantity: 1 }, + ]) + }) + }) + + describe('update', () => { + test('should update the specified ticker in the state', () => { + const newState = tickersActions.update(initialState, { + ticker: 'AABB11', + portfolioId: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', + isHidden: true, + quantity: 5, + }) + + expect(newState).toEqual([ + { ticker: 'AABB11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + { ticker: 'AABB11', portfolioId: 'bdcfe013-ae51-4f79-b8a1-791f745e9116', isHidden: true, quantity: 5 }, + { ticker: 'AAXX11', portfolioId: 'e5d46968-3a05-46d1-b5ec-a003c2a38a3b', isHidden: false, quantity: 1 }, + ]) + }) + }) +}) diff --git a/src/components/common/Title.tsx b/src/components/common/Title.tsx index 2d41682..cb82f74 100644 --- a/src/components/common/Title.tsx +++ b/src/components/common/Title.tsx @@ -1,6 +1,6 @@ 'use client' -import { cn } from '@/helpers/utils' +import { cn } from '@/helpers/cn' import { TitleProps, Title as TitleTremor } from '@tremor/react' export function Title({ children, ...props }: TitleProps) { diff --git a/src/components/layout/Main.tsx b/src/components/layout/Main.tsx index 253c2c3..9ca3b3b 100644 --- a/src/components/layout/Main.tsx +++ b/src/components/layout/Main.tsx @@ -1,6 +1,6 @@ 'use client' -import { cn } from '@/helpers/utils' +import { cn } from '@/helpers/cn' import { useSidebar } from '@/hooks/useSidebar' import { ReactNode } from 'react' diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index 72b38e4..d1ccca3 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -1,10 +1,10 @@ 'use client' -import { Calculator, Question, Wallet } from '@phosphor-icons/react' +import { Calculator, Wallet } from '@phosphor-icons/react' import { SidebarMenuItem } from './SidebarMenu' import { Logo } from '../common/Logo' import { useSidebar } from '@/hooks/useSidebar' -import { cn } from '@/helpers/utils' +import { cn } from '@/helpers/cn' export function Sidebar() { const { isOpen } = useSidebar() diff --git a/src/components/pages/home/history-chart/index.tsx b/src/components/pages/home/history-chart/index.tsx index e6bbfe6..d03767b 100644 --- a/src/components/pages/home/history-chart/index.tsx +++ b/src/components/pages/home/history-chart/index.tsx @@ -5,11 +5,9 @@ import { usePortfolios } from '@/hooks/usePortfolios' import { Card } from '../../../common/Card' import { TickerData } from '@/@types/TickersTypes' import { Chart } from './Chart' -import { - getLastDays, - getLastMonths, - mergeArrayOfObjects, -} from '@/helpers/utils' +import { getLastDays } from '@/helpers/getLastDays' +import { getLastMonths } from '@/helpers/getLastMonths' +import { mergeArrayOfObjects } from '@/helpers/mergeArrayOfObjects' type Report = 'pricesHistory' | 'dividendsHistory' diff --git a/src/components/pages/home/stocks-cards/StockCard.tsx b/src/components/pages/home/stocks-cards/StockCard.tsx index 533ce8f..6d8a60e 100644 --- a/src/components/pages/home/stocks-cards/StockCard.tsx +++ b/src/components/pages/home/stocks-cards/StockCard.tsx @@ -1,4 +1,4 @@ -import { cn } from '@/helpers/utils' +import { cn } from '@/helpers/cn' import { Card, Text, BadgeDelta, Metric, CardProps } from '@tremor/react' interface Props extends CardProps { diff --git a/src/config.test.ts b/src/config.test.ts new file mode 100644 index 0000000..a8fe99d --- /dev/null +++ b/src/config.test.ts @@ -0,0 +1,73 @@ +import { afterEach, beforeEach, describe, expect, test } from 'vitest' +import { getConfigAPI } from './config' + +describe('getConfigAPI', () => { + beforeEach(() => { + process.env.NEXT_PUBLIC_API_KEY = 'test-key' + process.env.NEXT_PUBLIC_API_URL = 'https://api.example.com' + process.env.NEXT_PUBLIC_API_CACHE = 'no-cache' + process.env.NEXT_PUBLIC_API_REVALIDATE = '3600' + }) + + afterEach(() => { + delete process.env.NEXT_PUBLIC_API_KEY + delete process.env.NEXT_PUBLIC_API_URL + delete process.env.NEXT_PUBLIC_API_CACHE + delete process.env.NEXT_PUBLIC_API_REVALIDATE + }) + + test('should return config object with valid environment variables', () => { + const expectedConfig = { + key: 'test-key', + url: 'https://api.example.com', + cache: 'no-cache', + revalidate: 3600, + } + + const config = getConfigAPI() + + expect(config).toEqual(expectedConfig) + }) + + test('should throw an error if NEXT_PUBLIC_API_KEY is not found', () => { + delete process.env.NEXT_PUBLIC_API_KEY + + expect(getConfigAPI).toThrowError('NEXT_PUBLIC_API_KEY not found') + }) + + test('should throw an error if NEXT_PUBLIC_API_URL is not found', () => { + delete process.env.NEXT_PUBLIC_API_URL + + expect(getConfigAPI).toThrowError('NEXT_PUBLIC_API_URL not found') + }) + + test('should return default values if NEXT_PUBLIC_API_CACHE is not found', () => { + delete process.env.NEXT_PUBLIC_API_CACHE + + const expectedConfig = { + key: 'test-key', + url: 'https://api.example.com', + cache: 'force-cache', + revalidate: 3600, + } + + const config = getConfigAPI() + + expect(config).toEqual(expectedConfig) + }) + + test('should return default values if NEXT_PUBLIC_API_REVALIDATE is not found', () => { + delete process.env.NEXT_PUBLIC_API_REVALIDATE + + const expectedConfig = { + key: 'test-key', + url: 'https://api.example.com', + cache: 'no-cache', + revalidate: 3600, + } + + const config = getConfigAPI() + + expect(config).toEqual(expectedConfig) + }) +}) diff --git a/src/config.ts b/src/config.ts index 63f0a47..979f4f3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,4 @@ -const getConfigAPI = () => { +export const getConfigAPI = () => { if (!process.env.NEXT_PUBLIC_API_KEY) { throw new Error('NEXT_PUBLIC_API_KEY not found') } @@ -10,7 +10,7 @@ const getConfigAPI = () => { return { key: process.env.NEXT_PUBLIC_API_KEY, url: process.env.NEXT_PUBLIC_API_URL, - cache: process.env.NEXT_PUBLIC_API_CACHE as RequestCache, + cache: (process.env.NEXT_PUBLIC_API_CACHE || 'force-cache') as RequestCache, revalidate: parseInt(process.env.NEXT_PUBLIC_API_REVALIDATE || '3600'), } } diff --git a/src/helpers/cn.test.ts b/src/helpers/cn.test.ts new file mode 100644 index 0000000..f8ace2b --- /dev/null +++ b/src/helpers/cn.test.ts @@ -0,0 +1,9 @@ +import { describe, expect, test } from 'vitest' +import { cn } from './cn' + +describe('cn', () => { + test('should return string with merged values', () => { + const result = cn('a', 'b', 'c') + expect(result).toBe('a b c') + }) +}) diff --git a/src/helpers/cn.ts b/src/helpers/cn.ts new file mode 100644 index 0000000..d32b0fe --- /dev/null +++ b/src/helpers/cn.ts @@ -0,0 +1,6 @@ +import { type ClassValue, clsx } from 'clsx' +import { twMerge } from 'tailwind-merge' + +export function cn(...inputs: ClassValue[]) { + return twMerge(clsx(inputs)) +} diff --git a/src/helpers/currency.test.ts b/src/helpers/currency.test.ts new file mode 100644 index 0000000..1446f2a --- /dev/null +++ b/src/helpers/currency.test.ts @@ -0,0 +1,12 @@ +import { describe, test, expect } from 'vitest' +import { toCurrency } from './currency' + +describe('toCurrency', () => { + test('should return the correct currency format for a given number', () => { + const number = 1234.56 + const expected = 'R$ 1.234,56' + + const result = toCurrency(number) + expect(result).toBe(expected) + }) +}) diff --git a/src/helpers/fetchStocks.ts b/src/helpers/fetchStocks.ts deleted file mode 100644 index a7bcb2b..0000000 --- a/src/helpers/fetchStocks.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { StockResult } from '@/@types/StocksTypes' -import { config } from '@/config' - -export const fetchStocks = async (): Promise => { - const { key, url } = config.api - - const response = await fetch(`${url}/finance?key=${key}&format=json-cors`, { - method: 'GET', - cache: 'force-cache', - next: { - revalidate: 3600, - }, - }) - - if (!response.ok) { - throw new Error('Something went wrong') - } - - const { results } = await response.json() - - if (!results) { - throw new Error('No results found') - } - - return results.stocks as StockResult -} diff --git a/src/helpers/getLastDays.test.ts b/src/helpers/getLastDays.test.ts new file mode 100644 index 0000000..20ad4da --- /dev/null +++ b/src/helpers/getLastDays.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, test } from 'vitest' +import { getLastDays } from './getLastDays' + +describe('getLastDays', () => { + test('should return array with last 20 days', () => { + const days = getLastDays() + expect(days.length).toBe(20) + }) + + test('should return array with custom days', () => { + const days = getLastDays(10) + expect(days.length).toBe(10) + }) +}) diff --git a/src/helpers/getLastDays.ts b/src/helpers/getLastDays.ts new file mode 100644 index 0000000..d960149 --- /dev/null +++ b/src/helpers/getLastDays.ts @@ -0,0 +1,17 @@ +import moment from 'moment' + +export function getLastDays(count = 20): string[] { + const today = moment() + const days = [] + + for (let i = 0; days.length < count; i++) { + const currentDay = today.clone().subtract(i, 'days') + + if (currentDay.isoWeekday() !== 6 && currentDay.isoWeekday() !== 7) { + const dayStr = currentDay.format('DD/MMM') + days.push(dayStr) + } + } + + return days +} diff --git a/src/helpers/getLastMonths.test.ts b/src/helpers/getLastMonths.test.ts new file mode 100644 index 0000000..e9f02a8 --- /dev/null +++ b/src/helpers/getLastMonths.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, test } from 'vitest' +import { getLastMonths } from './getLastMonths' + +describe('getLastMonths', () => { + test('should return array with last 12 months', () => { + const months = getLastMonths() + expect(months.length).toBe(12) + }) + + test('should return array with custom months', () => { + const months = getLastMonths(10) + expect(months.length).toBe(10) + }) +}) diff --git a/src/helpers/getLastMonths.ts b/src/helpers/getLastMonths.ts new file mode 100644 index 0000000..8cd85af --- /dev/null +++ b/src/helpers/getLastMonths.ts @@ -0,0 +1,14 @@ +import moment from 'moment' + +export function getLastMonths(count = 12): string[] { + const today = moment() + const months = [] + + for (let i = 0; i < count; i++) { + const currentMonth = today.clone().subtract(i, 'months') + const monthStr = currentMonth.format('MMM/YY') + months.push(monthStr) + } + + return months +} diff --git a/src/helpers/mergeArrayOfObjects.test.ts b/src/helpers/mergeArrayOfObjects.test.ts new file mode 100644 index 0000000..7218c91 --- /dev/null +++ b/src/helpers/mergeArrayOfObjects.test.ts @@ -0,0 +1,10 @@ +import { describe, expect, test } from 'vitest' +import { mergeArrayOfObjects } from './mergeArrayOfObjects' + +describe('mergeArrayOfObjects', () => { + test('should return object with merged values', () => { + const arr = [{ a: 1 }, { b: 2 }, { c: 3 }, { a: 4 }] + const result = mergeArrayOfObjects(arr) + expect(result).toEqual({ a: 4, b: 2, c: 3 }) + }) +}) diff --git a/src/helpers/mergeArrayOfObjects.ts b/src/helpers/mergeArrayOfObjects.ts new file mode 100644 index 0000000..5caf9ba --- /dev/null +++ b/src/helpers/mergeArrayOfObjects.ts @@ -0,0 +1,5 @@ +export function mergeArrayOfObjects(arr: object[]): object { + return arr.reduce((mergedObj, currentObj) => { + return { ...mergedObj, ...currentObj } + }, {}) +} diff --git a/src/helpers/utils.ts b/src/helpers/utils.ts deleted file mode 100644 index b711b1a..0000000 --- a/src/helpers/utils.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { type ClassValue, clsx } from 'clsx' -import { twMerge } from 'tailwind-merge' -import moment from 'moment' - -export function cn(...inputs: ClassValue[]) { - return twMerge(clsx(inputs)) -} - -export function getLastMonths(count = 12): string[] { - const today = moment() - const months = [] - - for (let i = 0; i < count; i++) { - const currentMonth = today.clone().subtract(i, 'months') - const monthStr = currentMonth.format('MMM/YY') - months.push(monthStr) - } - - return months -} - -export function getLastDays(count = 20): string[] { - const today = moment() - const days = [] - - for (let i = 0; days.length < count; i++) { - const currentDay = today.clone().subtract(i, 'days') - - if (currentDay.isoWeekday() !== 6 && currentDay.isoWeekday() !== 7) { - const dayStr = currentDay.format('DD/MMM') - days.push(dayStr) - } - } - - return days -} - -export function mergeArrayOfObjects(arr: object[]): object { - return arr.reduce((mergedObj, currentObj) => { - return { ...mergedObj, ...currentObj } - }, {}) -} diff --git a/src/services/TickerService.test.ts b/src/services/TickerService.test.ts new file mode 100644 index 0000000..d1bf2d1 --- /dev/null +++ b/src/services/TickerService.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, test } 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', + }) + }) +}) diff --git a/src/services/TickerService.ts b/src/services/TickerService.ts index bd9c7b5..f8023b1 100644 --- a/src/services/TickerService.ts +++ b/src/services/TickerService.ts @@ -50,24 +50,28 @@ export class TickerService { } public async fetch(): Promise { - 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') } } @@ -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)