Skip to content

Commit 3c1a840

Browse files
authored
Added Unit tests (#5)
1 parent 2ceb925 commit 3c1a840

File tree

13 files changed

+261
-81
lines changed

13 files changed

+261
-81
lines changed

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ jobs:
2929
- name: Install dependencies
3030
run: npm ci
3131

32-
# - name: Run tests
33-
# run: npm run test
32+
- name: Run tests
33+
run: npm run test
3434

3535
- name: 'Set release type : ${{ inputs.ReleaseType }}'
3636
id: release_type

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
2-
3-
4-
51
# React Waterfall Chart
62

73

84

9-
<a href="https://www.npmjs.com/package/react-waterfall-chart"><img src="https://badgen.net/npm/v/react-waterfall-chart?color=blue" alt="npm version"></a> <a href="https://www.npmjs.com/package/react-waterfall-chart" ><img src="https://img.shields.io/npm/dw/react-waterfall-chart?label=Downloads" /></a> <a href="https://github.com/KeyValueSoftwareSystems/react-waterfall-chart"><img src="https://github.com/KeyValueSoftwareSystems/react-waterfall-chart/actions/workflows/deploy.yml/badge.svg" alt="" /></a>
5+
<a href="https://www.npmjs.com/package/@keyvaluesystems/react-waterfall-chart"><img src="https://badgen.net/npm/v/@keyvaluesystems/react-waterfall-chart?color=blue" alt="npm version"></a> <a href="https://www.npmjs.com/package/@keyvaluesystems/react-waterfall-chart" ><img src="https://img.shields.io/npm/dw/@keyvaluesystems/react-waterfall-chart?label=Downloads" /></a> <a href="https://github.com/KeyValueSoftwareSystems/react-waterfall-chart"><img src="https://github.com/KeyValueSoftwareSystems/react-waterfall-chart/actions/workflows/deploy.yml/badge.svg" alt="" /></a>
106

117

128

139
<div align="center">
14-
<img src="src/assets/waterfall-chart-example.png" alt="" width="735" height="401"/>
10+
<img src="src/assets/waterfall-chart-example.png" alt="" width="784" height="414"/>
1511
</div>
1612

1713

jest.config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

jest.config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type {Config} from 'jest';
2+
3+
const config :Config = {
4+
// Other configuration above...
5+
6+
// Add the next three options if using TypeScript
7+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
8+
preset: "ts-jest",
9+
roots: ["<rootDir>/src"],
10+
transform: {
11+
"^.+\\.[t|j]sx?$": "ts-jest",
12+
"^.+\\.svg?$": "<rootDir>/transform.js",
13+
"^.+\\.scss?$": "<rootDir>/transform.js"
14+
},
15+
"testEnvironment": "jsdom",
16+
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
17+
moduleNameMapper: {
18+
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/scripts/testMock.js",
19+
"\\.(css|less)$": "<rootDir>/scripts/testMock.js"
20+
}
21+
};
22+
23+
export default config;

package-lock.json

Lines changed: 111 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"start": "webpack watch && tsc",
1010
"build": "webpack && tsc",
1111
"eslint": "eslint src",
12-
"test": "jest --config ./jest.config.js",
12+
"test": "jest --config ./jest.config.ts",
1313
"coverage": "npm run test -- --coverage",
1414
"trypublish": "npm publish || true",
1515
"storybook": "start-storybook -p 6006",
@@ -73,6 +73,7 @@
7373
"terser-webpack-plugin": "^5.3.5",
7474
"ts-jest": "^29.0.5",
7575
"ts-loader": "^9.4.2",
76+
"ts-node": "^10.9.1",
7677
"typescript": "^4.9.5",
7778
"url-loader": "^4.1.1",
7879
"webpack": "^5.75.0",
@@ -88,24 +89,6 @@
8889
"files": [
8990
"build"
9091
],
91-
"jest": {
92-
"moduleNameMapper": {
93-
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/scripts/testMock.js",
94-
"\\.(css|less)$": "<rootDir>/scripts/testMock.js"
95-
},
96-
"moduleFileExtensions": [
97-
"web.js",
98-
"js",
99-
"web.ts",
100-
"ts",
101-
"web.tsx",
102-
"tsx",
103-
"json",
104-
"web.jsx",
105-
"jsx",
106-
"node"
107-
]
108-
},
10992
"bugs": {
11093
"url": "https://github.com/KeyValueSoftwareSystems/react-vertical-stepper/issues"
11194
},
-868 Bytes
Loading

src/stories/Component.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ WaterFallChart.args = {
4747
label: 'Acquisitions',
4848
value: 80
4949
}
50-
]
50+
],
51+
onChartClick: (e: any) => console.log(e)
5152
};

src/tests/waterfallChart.test.tsx

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import React from 'react';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom/extend-expect';
4+
import WaterFallChart from '../waterfall-chart';
5+
6+
// Mock data for transactions
7+
const transactions = [
8+
{ label: 'Transaction 1', value: 100 },
9+
{ label: 'Transaction 2', value: -50 },
10+
{ label: 'Transaction 3', value: 200 }
11+
];
12+
13+
describe('WaterFallChart component', () => {
14+
it('renders the chart with correct bars and labels', () => {
15+
// Render the WaterFallChart component with transactions as props
16+
const { container, getByText } = render(<WaterFallChart transactions={transactions} />);
17+
18+
// Assert that the chart bars are rendered with correct heights
19+
const positiveBar = container.querySelector('#chartBar-0');
20+
const negativeBar = container.querySelector('#chartBar-1');
21+
const summaryBar = container.querySelector('#summaryBar');
22+
if (positiveBar) expect(positiveBar).toHaveAttribute('height', '50');
23+
if (negativeBar) expect(negativeBar).toHaveAttribute('height', '25');
24+
if (summaryBar) expect(summaryBar).toHaveAttribute('height', '75');
25+
26+
// Assert that the x-axis and y-axis lines are rendered
27+
const xAxisLine = container.querySelector('#xAxisLine');
28+
const yAxisLine = container.querySelector('#yAxisLine');
29+
expect(xAxisLine).toBeInTheDocument();
30+
expect(yAxisLine).toBeInTheDocument();
31+
32+
// Assert that the summary label is rendered
33+
const summaryLabel = getByText('Total');
34+
expect(summaryLabel).toBeInTheDocument();
35+
});
36+
37+
it('calls onChartClick callback when a bar is clicked', () => {
38+
// Mock callback function
39+
const mockOnClick = jest.fn();
40+
41+
// Render the WaterFallChart component with transactions and onChartClick callback as props
42+
const { container } = render(
43+
<WaterFallChart transactions={transactions} onChartClick={(e: any) => mockOnClick(e)} />
44+
);
45+
46+
// Click on a chart bar
47+
const barZero = container.querySelector('#chartBar-0');
48+
if (barZero) {
49+
fireEvent.click(barZero);
50+
// Assert that the mock callback function is called with the correct chart element
51+
expect(mockOnClick).toHaveBeenCalledTimes(1);
52+
}
53+
});
54+
55+
it('does not render bridge lines when showBridgeLines prop is set to false', () => {
56+
// Render the WaterFallChart component with showBridgeLines prop set to false
57+
const { container } = render(<WaterFallChart transactions={transactions} showBridgeLines={false} />);
58+
59+
// Assert that the bridge lines are not rendered
60+
const bridgeLine = container.querySelector('#chartBarBridgeLine-0');
61+
expect(bridgeLine).toBeNull();
62+
});
63+
64+
it('renders y-axis scale lines when showYAxisScaleLines prop is set to true', () => {
65+
// Render the WaterFallChart component with showYAxisScaleLines prop set to true
66+
const { container } = render(<WaterFallChart transactions={transactions} showYAxisScaleLines={true} />);
67+
68+
// Assert that y-axis scale lines are rendered
69+
const yAxisScaleLines = container.querySelectorAll('[id^="yAxisScaleLine-"]');
70+
if (yAxisScaleLines?.length > 0) expect(yAxisScaleLines.length).toBeGreaterThan(0);
71+
});
72+
});

src/types/types.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ export type IWaterfallGraphProps = {
88
yAxisPixelsPerUnit?: number;
99
showFinalSummary?: boolean;
1010
summaryXLabel?: string;
11-
summaryBarStyles?: CSSProperties;
12-
positiveBarStyles?: CSSProperties;
13-
negativeBarStyles?: CSSProperties;
11+
styles?: ICustomizationStyles;
1412
onChartClick?: IOnChartClick;
1513
};
1614

15+
export type ICustomizationStyles = {
16+
summaryBar?: CSSProperties;
17+
positiveBar?: CSSProperties;
18+
negativeBar?: CSSProperties;
19+
};
20+
1721
export type ITransaction = {
1822
label: string;
1923
value: number;

0 commit comments

Comments
 (0)