Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Setup jest #154

Merged
merged 3 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions apps/vaults/components/details/tabs/VaultDetailsStrategies.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useMemo, useState} from 'react';
import dynamic from 'next/dynamic';
import useSWR from 'swr';
import {findLatestApr} from '@vaults/components/details/tabs/findLatestApr';
import {useSettings} from '@yearn-finance/web-lib/contexts/useSettings';
import {useChainID} from '@yearn-finance/web-lib/hooks/useChainID';
import IconCopy from '@yearn-finance/web-lib/icons/IconCopy';
Expand All @@ -17,7 +18,7 @@ import IconChevron from '@common/icons/IconChevron';
import type {LoaderComponent} from 'next/dynamic';
import type {ReactElement} from 'react';
import type {SWRResponse} from 'swr';
import type {TYDaemonReports, TYearnVault, TYearnVaultStrategy} from '@common/types/yearn';
import type {TYearnVault, TYearnVaultStrategy} from '@common/types/yearn';
import type {TGraphForStrategyReportsProps} from '@vaults/components/graphs/GraphForStrategyReports';

const GraphForStrategyReports = dynamic<TGraphForStrategyReportsProps>(async (): LoaderComponent<TGraphForStrategyReportsProps> => import('@vaults/components/graphs/GraphForStrategyReports'), {ssr: false});
Expand Down Expand Up @@ -225,17 +226,4 @@ function VaultDetailsStrategies({currentVault}: {currentVault: TYearnVault}): Re
);
}

function findLatestApr(reports?: TYDaemonReports[]): number {
if (!reports) {
return 0;
}

const latestReport = reports.reduce((prev, curr): TYDaemonReports => {
return parseInt(prev.timestamp) > parseInt(curr.timestamp)? prev : curr;
});

return Number(latestReport.results[0].APR) * 100;
}


export {VaultDetailsStrategies};
87 changes: 87 additions & 0 deletions apps/vaults/components/details/tabs/findLatestApr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {findLatestApr} from './findLatestApr';

describe('findLatestApr', (): void => {
it('should return 0 when no reports are provided', (): void => {
expect(findLatestApr()).toBe(0);
});

it('should return 0 when an empty array is provided', (): void => {
expect(findLatestApr([])).toBe(0);
});

it('should return 0 if a result item has no APR', (): void => {
const reports = [
{
timestamp: '1000',
results: [
{
APR: ''
}
]
}
];

expect(findLatestApr(reports)).toBe(0);
});

it('should return 0 if a result item has an invalid APR', (): void => {
const reports = [
{
timestamp: '1000',
results: [
{
APR: 'foo'
}
]
}
];

expect(findLatestApr(reports)).toBe(0);
});

it('should return the correct APR for a single report', (): void => {
const reports = [
{
timestamp: '1000',
results: [
{
APR: '0.05'
}
]
}
];

expect(findLatestApr(reports)).toBe(5);
});

it('should return the correct APR for multiple reports', (): void => {
const reports = [
{
timestamp: '1000',
results: [
{
APR: '0.05'
}
]
},
{
timestamp: '3000',
results: [
{
APR: '0.1'
}
]
},
{
timestamp: '2000',
results: [
{
APR: '0.07'
}
]
}
];

expect(findLatestApr(reports)).toBe(10);
});
});
23 changes: 23 additions & 0 deletions apps/vaults/components/details/tabs/findLatestApr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {TYDaemonReports} from '@common/types/yearn';

export type TYDaemonPartialReports = Pick<TYDaemonReports, 'timestamp'> & {
results: Pick<TYDaemonReports['results'][0], 'APR'>[];
};

export function findLatestApr(reports?: TYDaemonPartialReports[]): number {
if (!reports?.length) {
return 0;
}

const latestReport = reports.reduce(
(prev, curr): TYDaemonPartialReports => {
return parseInt(prev.timestamp) > parseInt(curr.timestamp)
? prev
: curr;
}
);

const apr = Number(latestReport.results[0]?.APR);

return isNaN(apr) ? 0 : apr * 100;
}
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node'
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "tsc && next build",
"export": "tsc && next build && next export -o ipfs",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"bump": "bump"
"bump": "bump",
"test": "jest"
},
"dependencies": {
"@gnosis.pm/gp-v2-contracts": "^1.1.2",
Expand All @@ -27,6 +28,7 @@
"graphql": "^16.6.0",
"graphql-request": "^5.1.0",
"html2canvas": "^1.4.1",
"jest": "^29.5.0",
"next": "^13.1.1",
"next-plausible": "^3.7.2",
"next-pwa": "^5.6.0",
Expand All @@ -44,6 +46,7 @@
"wido": "^0.2.0"
},
"devDependencies": {
"@types/jest": "^29.5.1",
"@types/node": "^18.11.18",
"@types/nprogress": "^0.2.0",
"@types/react": "^18.0.26",
Expand All @@ -69,6 +72,7 @@
"postcss-nesting": "^10.2.0",
"sass": "^1.57.1",
"sharp": "^0.31.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.2",
"typescript": "^5.0.4"
},
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@
"next.config.js",
"theme.config.js",
".eslintrc.js",
".next/types/**/*.ts"]
".next/types/**/*.ts",
"jest.config.js"
]
}
Loading