Skip to content

Commit

Permalink
test(dca): add jest config and first DCA calculations test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnacioPorte committed Feb 5, 2024
1 parent a6ebcb9 commit 71941cf
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 32 deletions.
1 change: 0 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{

"tabWidth": 2,
"semi": true,
"singleQuote": false,
Expand Down
84 changes: 54 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default {
// other rules...
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
}
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
# Buda DCA simulator

## Requisitos

Antes de comenzar, asegúrate de cumplir con uno de los siguientes requisitos:

- Bun
- Docker

## Configuración del Entorno

Para configurar el entorno, sigue estos pasos:

1. Clona el repositorio:
```bash
git clone https://github.com/IgnacioPorte/buda-dca.git
```
2. Navega al directorio del proyecto:
```bash
cd buda-dca
```
### Dockerizado:

3. 1. Buildea la imagen de Docker:
```bash
docker build . -t dca
```

4. 1. Corre el contenedor de Docker:
```bash
docker run -p 8080:8080 dca
```
### Local:

3. 2. Instala las dependencias:
```bash
bun install
```
4. 2. Corre el proyecto:
```bash
bun run dev
```
5. Abre tu navegador y navega a `http://localhost:8080`.

## Tests

- Para correr los tests, ejecuta el siguiente comando:
```bash
bun test
```

## Consideraciones
- La aplicación tiene todos los requerimientos y deseables específicados
- Los tests se corren con github actions
Binary file modified bun.lockb
Binary file not shown.
12 changes: 12 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
preset: 'ts-jest',
testEnvironment: 'jest-environment-jsdom',
transform: {
"^.+\\.tsx?$": "ts-jest"
// process `*.tsx` files with `ts-jest`
},
moduleNameMapper: {
'\\.(gif|ttf|eot|svg|png)$': '<rootDir>/test/__ mocks __/fileMock.js',
'^@/(.*)$': '<rootDir>/src/$1',
},
}
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,35 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --fix --ext ts,tsx --report-unused-disable-directives --max-warnings 0 && prettier --write ./src",
"test": "jest",
"preview": "vite preview"
},
"dependencies": {
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
"@testing-library/react": "^14.2.1",
"@types/jest": "^29.5.12",
"@types/testing-library__jest-dom": "^6.0.0",
"axios": "^1.6.7",
"axios-rate-limit": "^1.3.0",
"chart.js": "^4.4.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"date-fns": "^3.3.1",
"identity-obj-proxy": "^3.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.2.5",
"react": "^18.2.0",
"react-chartjs-2": "^5.2.0",
"react-day-picker": "^8.10.0",
"react-dom": "^18.2.0",
"tailwind-merge": "^2.2.1",
"tailwindcss-animate": "^1.0.7"
"tailwindcss-animate": "^1.0.7",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2"
},
"devDependencies": {
"@types/node": "^20.11.16",
Expand Down
4 changes: 4 additions & 0 deletions src/test/__mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
__esModule: true,
default: 'test-file-stub',
};
136 changes: 136 additions & 0 deletions src/test/__tests__/calculateDCA.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { calculateDCAInvestment } from "@/utils/dca";
import { HistoricalPrice, MonthlyDCAInvestment } from "@/types";

describe("calculateDCAInvestment", () => {
const quantity = 20;
const data: HistoricalPrice[] = [
{
date: "2021-01-01",
price: 100,
},
{
date: "2021-01-02",
price: 200,
},
{
date: "2021-01-03",
price: 400,
},
];

const expectedDCA: MonthlyDCAInvestment[] = [
{
cryptoPortfolio: 0.2,
date: "2021-01-01",
fiatInvestment: 20,
gain: 0,
gainPercentage: 0,
portfolioFiatValue: 20,
price: 100,
},
{
cryptoPortfolio: 0.30000000000000004,
date: "2021-01-02",
fiatInvestment: 40,
gain: 20.000000000000007,
gainPercentage: 50.00000000000002,
portfolioFiatValue: 60.00000000000001,
price: 200,
},
{
cryptoPortfolio: 0.35000000000000003,
date: "2021-01-03",
fiatInvestment: 60,
gain: 80,
gainPercentage: 133.33333333333331,
portfolioFiatValue: 140,
price: 400,
},
];

const data2: HistoricalPrice[] = [
{
date: "2021-01-01",
price: 100,
},
{
date: "2021-01-02",
price: 120,
},
{
date: "2021-01-03",
price: 140,
},
{
date: "2021-01-04",
price: 160,
},
{
date: "2021-01-05",
price: 180,
},
];

const expectedDCA2: MonthlyDCAInvestment[] = [
{
cryptoPortfolio: 0.2,
date: "2021-01-01",
fiatInvestment: 20,
gain: 0,
gainPercentage: 0,
portfolioFiatValue: 20,
price: 100,
},
{
cryptoPortfolio: 0.3666666666666667,
date: "2021-01-02",
fiatInvestment: 40,
gain: 4,
gainPercentage: 10,
portfolioFiatValue: 44,
price: 120,
},
{
cryptoPortfolio: 0.5095238095238095,
date: "2021-01-03",
fiatInvestment: 60,
gain: 11.333333333333329,
gainPercentage: 18.88888888888888,
portfolioFiatValue: 71.33333333333333,
price: 140,
},
{
cryptoPortfolio: 0.6345238095238095,
date: "2021-01-04",
fiatInvestment: 80,
gain: 21.52380952380952,
gainPercentage: 26.904761904761898,
portfolioFiatValue: 101.52380952380952,
price: 160,
},
{
cryptoPortfolio: 0.7456349206349207,
date: "2021-01-05",
fiatInvestment: 100,
gain: 34.21428571428572,
gainPercentage: 34.21428571428572,
portfolioFiatValue: 134.21428571428572,
price: 180,
},
];

it("should return empty array if data is empty", () => {
const investment = calculateDCAInvestment([], quantity);
expect(investment).toStrictEqual([]);
});

it("Calculate simple DCA", () => {
const investment = calculateDCAInvestment(data, quantity);
expect(investment).toStrictEqual(expectedDCA);
});

it("Caculate DCA complex", () => {
const investment = calculateDCAInvestment(data2, quantity);
expect(investment).toStrictEqual(expectedDCA2);
});
});
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"@/*": ["./src/*"]
},

"esModuleInterop": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
Expand Down

0 comments on commit 71941cf

Please sign in to comment.