-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chart library: Improve overall test coverage (#41449)
* add tooltip tests * add legend testing * changelog * add additional legend tests
- Loading branch information
Showing
4 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/js-packages/charts/changelog/add-chart-library-improve-overall-test-coverage
This file contains 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: add additional testing for library |
This file contains 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
76 changes: 76 additions & 0 deletions
76
projects/js-packages/charts/src/components/legend/legend.test.tsx
This file contains 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,76 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { BaseLegend } from './base-legend'; | ||
|
||
describe( 'BaseLegend', () => { | ||
const defaultItems = [ | ||
{ label: 'Item 1', value: '50%', color: '#ff0000' }, | ||
{ label: 'Item 2', value: '30%', color: '#00ff00' }, | ||
]; | ||
|
||
test( 'renders horizontal legend items', () => { | ||
render( <BaseLegend items={ defaultItems } orientation="horizontal" /> ); | ||
expect( screen.getByText( 'Item 1' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Item 2' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( '50%' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( '30%' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'renders vertical legend items', () => { | ||
render( <BaseLegend items={ defaultItems } orientation="vertical" /> ); | ||
const items = screen.getAllByText( /Item \d/ ); | ||
expect( items ).toHaveLength( 2 ); | ||
} ); | ||
|
||
test( 'applies color styles to legend markers', () => { | ||
render( <BaseLegend items={ defaultItems } orientation="horizontal" /> ); | ||
const markers = screen.getAllByTestId( 'legend-marker' ); | ||
expect( markers[ 0 ] ).toHaveAttribute( 'fill', '#ff0000' ); | ||
expect( markers[ 1 ] ).toHaveAttribute( 'fill', '#00ff00' ); | ||
} ); | ||
|
||
test( 'handles empty items array', () => { | ||
render( <BaseLegend items={ [] } orientation="horizontal" /> ); | ||
const legendItems = screen.queryAllByRole( 'listitem' ); | ||
expect( legendItems ).toHaveLength( 0 ); | ||
} ); | ||
|
||
test( 'handles missing values', () => { | ||
const itemsWithoutValues = [ | ||
{ label: 'Item 1', color: '#ff0000', value: undefined }, | ||
{ label: 'Item 2', color: '#00ff00', value: undefined }, | ||
]; | ||
render( <BaseLegend items={ itemsWithoutValues } orientation="horizontal" /> ); | ||
expect( screen.getByText( 'Item 1' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Item 2' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'applies custom className', () => { | ||
render( | ||
<BaseLegend items={ defaultItems } className="custom-legend" orientation="horizontal" /> | ||
); | ||
expect( screen.getByRole( 'list' ) ).toHaveClass( 'custom-legend' ); | ||
} ); | ||
|
||
test( 'renders with correct orientation styles', () => { | ||
const { rerender } = render( <BaseLegend items={ defaultItems } orientation="horizontal" /> ); | ||
expect( screen.getByTestId( 'legend-horizontal' ) ).toBeInTheDocument(); | ||
|
||
rerender( <BaseLegend items={ defaultItems } orientation="vertical" /> ); | ||
expect( screen.getByTestId( 'legend-vertical' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'renders legend items with correct spacing', () => { | ||
render( <BaseLegend items={ defaultItems } orientation="horizontal" /> ); | ||
const items = screen.getAllByTestId( 'legend-item' ); | ||
expect( items ).toHaveLength( 2 ); | ||
} ); | ||
|
||
test( 'handles items with long labels', () => { | ||
const itemsWithLongLabels = [ | ||
{ label: 'Very Long Label That Should Still Display', value: '50%', color: '#ff0000' }, | ||
{ label: 'Another Long Label for Testing', value: '30%', color: '#00ff00' }, | ||
]; | ||
render( <BaseLegend items={ itemsWithLongLabels } orientation="horizontal" /> ); | ||
expect( screen.getByText( 'Very Long Label That Should Still Display' ) ).toBeInTheDocument(); | ||
} ); | ||
} ); |
50 changes: 50 additions & 0 deletions
50
projects/js-packages/charts/src/components/tooltip/test/tool-tip.test.tsx
This file contains 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,50 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { BaseTooltip } from '../base-tooltip'; | ||
|
||
describe( 'BaseTooltip', () => { | ||
const defaultProps = { | ||
data: { | ||
label: 'Test Label', | ||
value: 100, | ||
valueDisplay: '100%', | ||
}, | ||
top: 100, | ||
left: 200, | ||
}; | ||
|
||
test( 'renders default tooltip content', () => { | ||
render( <BaseTooltip { ...defaultProps } /> ); | ||
expect( screen.getByText( 'Test Label: 100%' ) ).toBeInTheDocument(); | ||
expect( screen.getByRole( 'tooltip' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'renders children instead of data when provided', () => { | ||
render( | ||
<BaseTooltip top={ 100 } left={ 200 }> | ||
<div>Custom Child Content</div> | ||
</BaseTooltip> | ||
); | ||
expect( screen.getByText( 'Custom Child Content' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'applies correct positioning styles', () => { | ||
render( <BaseTooltip { ...defaultProps } /> ); | ||
const tooltip = screen.getByRole( 'tooltip' ); | ||
expect( tooltip ).toHaveStyle( { | ||
top: '100px', | ||
left: '200px', | ||
} ); | ||
} ); | ||
|
||
test( 'handles missing valueDisplay', () => { | ||
const propsWithoutDisplay = { | ||
...defaultProps, | ||
data: { | ||
label: 'Test', | ||
value: 50, | ||
}, | ||
}; | ||
render( <BaseTooltip { ...propsWithoutDisplay } /> ); | ||
expect( screen.getByText( 'Test: 50' ) ).toBeInTheDocument(); | ||
} ); | ||
} ); |