-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[charts] Test pointer events #14042
Merged
Merged
[charts] Test pointer events #14042
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c632673
[charts] Test pointer events
alexfauquette bea8ef6
add tests
alexfauquette 911ee49
flavien feedback
alexfauquette 7828ec7
Extends test to other charts
alexfauquette a00a0f1
add pie chart
alexfauquette b39c7e9
Merge remote-tracking branch 'upstream/master' into pointer-test
alexfauquette 2b4b048
fix
alexfauquette 1bbc118
clean DOM after testing
alexfauquette c0987f2
test
alexfauquette eae6485
Merge remote-tracking branch 'upstream/master' into pointer-test
alexfauquette 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
106 changes: 106 additions & 0 deletions
106
packages/x-charts/src/BarChart/checkClickEvent.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,106 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { createRenderer, fireEvent } from '@mui/internal-test-utils'; | ||
import { spy } from 'sinon'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
function firePointerEvent( | ||
target: Element, | ||
type: 'pointerstart' | 'pointermove' | 'pointerend', | ||
options: Pick<PointerEventInit, 'clientX' | 'clientY'>, | ||
): void { | ||
const originalGetBoundingClientRect = target.getBoundingClientRect; | ||
target.getBoundingClientRect = () => ({ | ||
x: 0, | ||
y: 0, | ||
bottom: 0, | ||
height: 400, | ||
left: 0, | ||
right: 0, | ||
top: 0, | ||
width: 400, | ||
toJSON() { | ||
return { | ||
x: 0, | ||
y: 0, | ||
bottom: 0, | ||
height: 400, | ||
left: 0, | ||
right: 0, | ||
top: 0, | ||
width: 400, | ||
}; | ||
}, | ||
}); | ||
const event = new window.PointerEvent(type, { | ||
bubbles: true, | ||
cancelable: true, | ||
composed: true, | ||
isPrimary: true, | ||
...options, | ||
}); | ||
|
||
fireEvent(target, event); | ||
target.getBoundingClientRect = originalGetBoundingClientRect; | ||
} | ||
|
||
const config = { | ||
dataset: [ | ||
{ x: 'A', v1: 4, v2: 2 }, | ||
{ x: 'B', v1: 1, v2: 1 }, | ||
], | ||
margin: { top: 0, left: 0, bottom: 0, right: 0 }, | ||
width: 400, | ||
height: 400, | ||
}; | ||
|
||
// Plot as follow to simplify click position | ||
// | ||
// | X | ||
// | X | ||
// | X X | ||
// | X X X X | ||
// ---A---B- | ||
|
||
describe('BarChart - click event', () => { | ||
const { render } = createRenderer(); | ||
|
||
it('should provide the accurate context as second argument', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) { | ||
// can't do Pointer event with JSDom https://github.com/jsdom/jsdom/issues/2527 | ||
this.skip(); | ||
} | ||
const onAxisClick = spy(); | ||
render( | ||
<div | ||
style={{ | ||
width: 400, | ||
height: 400, | ||
}} | ||
> | ||
<BarChart | ||
{...config} | ||
series={[ | ||
{ dataKey: 'v1', id: 's1' }, | ||
{ dataKey: 'v2', id: 's2' }, | ||
]} | ||
xAxis={[{ scaleType: 'band', dataKey: 'x' }]} | ||
onAxisClick={onAxisClick} | ||
/> | ||
</div>, | ||
); | ||
const svg = document.querySelector<HTMLElement>('svg')!; | ||
|
||
firePointerEvent(svg, 'pointermove', { | ||
clientX: 201, | ||
clientY: 60, | ||
}); | ||
fireEvent.click(svg); | ||
|
||
expect(onAxisClick.lastCall.args[1]).to.deep.equal({ | ||
dataIndex: 0, | ||
axisValue: 'A', | ||
seriesValues: { s1: 4, s2: 2 }, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick, I don't think "accurate" is the right word here.