-
Notifications
You must be signed in to change notification settings - Fork 822
Chart Library: add bar chart component tests + improve data validation #41296
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8d591a6
add bar chart tests
annacmc 70129d5
improve error handling + data validation
annacmc 5702338
add error state story
annacmc 68131f4
changelog
annacmc 07bd1c8
remove extra code not needed
annacmc 3864d09
Remove useCallback since it's not providing any benefit
annacmc 992f10f
destructure BarChart props in function parameters
annacmc 90dd2be
add back in string class for debugging and external changes
annacmc 7e1ce5d
remove unnecessary wrapper of a function
kangzj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/js-packages/charts/changelog/add-chart-library-test-bar-chart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Charts: adds tests and fixes to bar chart component |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
projects/js-packages/charts/src/components/bar-chart/test/bar-chart.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import { ThemeProvider } from '../../../providers/theme'; | ||
import BarChart from '../bar-chart'; | ||
|
||
describe( 'BarChart', () => { | ||
const defaultProps = { | ||
width: 500, | ||
height: 300, | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ | ||
{ date: new Date( '2024-01-01' ), value: 10, label: 'Jan 1' }, | ||
{ date: new Date( '2024-01-02' ), value: 20, label: 'Jan 2' }, | ||
], | ||
options: {}, | ||
}, | ||
], | ||
}; | ||
|
||
const renderWithTheme = ( props = {} ) => { | ||
return render( | ||
<ThemeProvider> | ||
<BarChart { ...defaultProps } { ...props } /> | ||
</ThemeProvider> | ||
); | ||
}; | ||
|
||
describe( 'Data Validation', () => { | ||
test( 'handles empty data array', () => { | ||
renderWithTheme( { data: [] } ); | ||
expect( screen.getByText( /no data available/i ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'handles single data point', () => { | ||
renderWithTheme( { | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ { date: new Date( '2024-01-01' ), value: 10, label: 'Jan 1' } ], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.getByRole( 'img', { name: /bar chart/i } ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'handles negative values', () => { | ||
renderWithTheme( { | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ | ||
{ date: new Date( '2024-01-01' ), value: -10, label: 'Jan 1' }, | ||
{ date: new Date( '2024-01-02' ), value: -20, label: 'Jan 2' }, | ||
], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.getByRole( 'img', { name: /bar chart/i } ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'handles null or undefined values', () => { | ||
renderWithTheme( { | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ | ||
{ date: new Date( '2024-01-01' ), value: null as number | null, label: 'Jan 1' }, | ||
{ | ||
date: new Date( '2024-01-02' ), | ||
value: undefined as number | undefined, | ||
label: 'Jan 2', | ||
}, | ||
], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.getByText( /invalid data/i ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'handles invalid date values', () => { | ||
renderWithTheme( { | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ | ||
{ date: new Date( 'invalid' ), value: 10, label: 'Jan 1' }, | ||
{ date: new Date( '2024-01-02' ), value: 20, label: 'Jan 2' }, | ||
], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.getByText( /invalid data/i ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'handles invalid label values', () => { | ||
renderWithTheme( { | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ | ||
{ label: '', value: 10 }, // Empty label | ||
{ label: 'Label 2', value: 20 }, | ||
], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.getByText( /invalid data/i ) ).toBeInTheDocument(); | ||
} ); | ||
} ); | ||
|
||
describe( 'Legend', () => { | ||
test( 'shows legend when showLegend is true', () => { | ||
renderWithTheme( { | ||
showLegend: true, | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ { date: new Date( '2024-01-01' ), value: 10, label: 'Jan 1' } ], | ||
options: {}, | ||
}, | ||
{ | ||
label: 'Series B', | ||
data: [ { date: new Date( '2024-01-01' ), value: 20, label: 'Jan 1' } ], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Series B' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'hides legend when showLegend is false', () => { | ||
renderWithTheme( { | ||
showLegend: false, | ||
data: [ | ||
{ | ||
label: 'Series A', | ||
data: [ { date: new Date( '2024-01-01' ), value: 10, label: 'Jan 1' } ], | ||
options: {}, | ||
}, | ||
], | ||
} ); | ||
expect( screen.queryByText( 'Series A' ) ).not.toBeInTheDocument(); | ||
} ); | ||
} ); | ||
|
||
describe( 'Grid Visibility', () => { | ||
test( 'renders with different grid visibility options', () => { | ||
const { rerender } = renderWithTheme( { gridVisibility: 'x' } ); | ||
expect( screen.getByRole( 'img', { name: /bar chart/i } ) ).toBeInTheDocument(); | ||
|
||
rerender( | ||
<ThemeProvider> | ||
<BarChart { ...defaultProps } gridVisibility="y" /> | ||
</ThemeProvider> | ||
); | ||
expect( screen.getByRole( 'img', { name: /bar chart/i } ) ).toBeInTheDocument(); | ||
|
||
rerender( | ||
<ThemeProvider> | ||
<BarChart { ...defaultProps } gridVisibility="xy" /> | ||
</ThemeProvider> | ||
); | ||
expect( screen.getByRole( 'img', { name: /bar chart/i } ) ).toBeInTheDocument(); | ||
} ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.