-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from adobe/an-334305/highlightBy
An 334305/highlight by to bar
- Loading branch information
Showing
6 changed files
with
216 additions
and
41 deletions.
There are no files selected for viewing
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
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
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
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
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,83 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
import { ReactElement } from 'react'; | ||
|
||
import { ChartTooltip } from '@components/ChartTooltip/ChartTooltip'; | ||
import useChartProps from '@hooks/useChartProps'; | ||
import { Bar, Chart, Datum } from '@rsc'; | ||
import { browserData } from '@stories/data/data'; | ||
import { StoryFn } from '@storybook/react'; | ||
import { bindWithProps } from '@test-utils'; | ||
|
||
export default { | ||
title: 'RSC/ChartTooltip/HighlightBy', | ||
component: ChartTooltip, | ||
argTypes: { | ||
children: { | ||
description: '`(datum) => React.ReactElement`', | ||
control: { | ||
type: null, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
interface LineData extends Datum { | ||
value?: number; | ||
series?: string; | ||
category?: string; | ||
} | ||
|
||
const StackedBarTooltipStory: StoryFn<typeof ChartTooltip> = (args): ReactElement => { | ||
const chartProps = useChartProps({ data: browserData, width: 600 }); | ||
return ( | ||
<Chart {...chartProps}> | ||
<Bar color="series"> | ||
<ChartTooltip {...args} /> | ||
</Bar> | ||
</Chart> | ||
); | ||
}; | ||
|
||
const dialogCallback = (datum: LineData) => ( | ||
<div className="bar-tooltip"> | ||
<div>Operating system: {datum.series}</div> | ||
<div>Browser: {datum.category}</div> | ||
<div>Users: {datum.value}</div> | ||
</div> | ||
); | ||
|
||
const Basic = bindWithProps(StackedBarTooltipStory); | ||
Basic.args = { | ||
highlightBy: 'item', | ||
children: dialogCallback, | ||
}; | ||
|
||
const Dimension = bindWithProps(StackedBarTooltipStory); | ||
Dimension.args = { | ||
highlightBy: 'dimension', | ||
children: dialogCallback, | ||
}; | ||
|
||
const Series = bindWithProps(StackedBarTooltipStory); | ||
Series.args = { | ||
highlightBy: 'series', | ||
children: dialogCallback, | ||
}; | ||
|
||
const Keys = bindWithProps(StackedBarTooltipStory); | ||
Keys.args = { | ||
highlightBy: ['series'], | ||
children: dialogCallback, | ||
}; | ||
|
||
export { Basic, Dimension, Series, Keys }; |
100 changes: 100 additions & 0 deletions
100
src/stories/components/ChartTooltip/HighlightBy.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,100 @@ | ||
import { HIGHLIGHT_CONTRAST_RATIO } from '@constants'; | ||
import { | ||
allElementsHaveAttributeValue, | ||
findAllMarksByGroupName, | ||
findChart, | ||
hoverNthElement, | ||
render, | ||
} from '@test-utils'; | ||
|
||
import { Basic, Dimension, Keys, Series } from './HighlightBy.story'; | ||
|
||
describe('Basic', () => { | ||
test('Only the hovered element should be highlighted', async () => { | ||
render(<Basic {...Basic.args} />); | ||
|
||
const chart = await findChart(); | ||
expect(chart).toBeInTheDocument(); | ||
|
||
const bars = await findAllMarksByGroupName(chart, 'bar0'); | ||
expect(bars).toHaveLength(9); | ||
|
||
await hoverNthElement(bars, 2); | ||
// highlighed bar | ||
expect(bars[2]).toHaveAttribute('opacity', '1'); | ||
// all other bars | ||
expect( | ||
allElementsHaveAttributeValue( | ||
[...bars.slice(0, 2), ...bars.slice(3)], | ||
'opacity', | ||
1 / HIGHLIGHT_CONTRAST_RATIO | ||
) | ||
).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Dimension', () => { | ||
test('All the bars with the same dimension should be highlighted', async () => { | ||
render(<Dimension {...Dimension.args} />); | ||
|
||
const chart = await findChart(); | ||
expect(chart).toBeInTheDocument(); | ||
|
||
const bars = await findAllMarksByGroupName(chart, 'bar0'); | ||
expect(bars).toHaveLength(9); | ||
|
||
await hoverNthElement(bars, 2); | ||
// first three bars (same dimension) | ||
expect(allElementsHaveAttributeValue(bars.slice(0, 2), 'opacity', '1')).toBe(true); | ||
// all other bars | ||
expect(allElementsHaveAttributeValue(bars.slice(3), 'opacity', 1 / HIGHLIGHT_CONTRAST_RATIO)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Series', () => { | ||
test('All the bars with the same series should be highlighted', async () => { | ||
render(<Series {...Series.args} />); | ||
|
||
const chart = await findChart(); | ||
expect(chart).toBeInTheDocument(); | ||
|
||
const bars = await findAllMarksByGroupName(chart, 'bar0'); | ||
expect(bars).toHaveLength(9); | ||
|
||
await hoverNthElement(bars, 2); | ||
// bars 2, 5, and 8 (same series) | ||
expect(allElementsHaveAttributeValue([bars[2], bars[5], bars[8]], 'opacity', '1')).toBe(true); | ||
// all other bars | ||
expect( | ||
allElementsHaveAttributeValue( | ||
[...bars.slice(0, 1), ...bars.slice(3, 4), ...bars.slice(6, 7)], | ||
'opacity', | ||
1 / HIGHLIGHT_CONTRAST_RATIO | ||
) | ||
).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Keys', () => { | ||
test('All the bars with the same keys should be highlighted', async () => { | ||
render(<Keys {...Keys.args} />); | ||
|
||
const chart = await findChart(); | ||
expect(chart).toBeInTheDocument(); | ||
|
||
const bars = await findAllMarksByGroupName(chart, 'bar0'); | ||
expect(bars).toHaveLength(9); | ||
|
||
await hoverNthElement(bars, 2); | ||
// bars 2, 5, and 8 (same series) | ||
expect(allElementsHaveAttributeValue([bars[2], bars[5], bars[8]], 'opacity', '1')).toBe(true); | ||
// all other bars | ||
expect( | ||
allElementsHaveAttributeValue( | ||
[...bars.slice(0, 1), ...bars.slice(3, 4), ...bars.slice(6, 7)], | ||
'opacity', | ||
1 / HIGHLIGHT_CONTRAST_RATIO | ||
) | ||
).toBe(true); | ||
}); | ||
}); |