|
1 | | -import { createRenderer, screen } from '@mui/internal-test-utils'; |
| 1 | +import { page, userEvent } from 'vitest/browser'; |
| 2 | +import { act, createRenderer, fireEvent, screen } from '@mui/internal-test-utils'; |
2 | 3 | import { clearLicenseStatusCache, LicenseInfo } from '@mui/x-license'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | +import { isJSDOM } from 'test/utils/skipIf'; |
3 | 6 | import { Heatmap } from './Heatmap'; |
| 7 | +import { heatmapClasses } from './heatmapClasses'; |
4 | 8 |
|
5 | 9 | describe('<Heatmap /> - License', () => { |
6 | 10 | const { render } = createRenderer(); |
@@ -69,3 +73,48 @@ describe('<Heatmap /> - License', () => { |
69 | 73 | }); |
70 | 74 | }); |
71 | 75 | }); |
| 76 | + |
| 77 | +describe('Heatmap - onItemClick', () => { |
| 78 | + const { render } = createRenderer(); |
| 79 | + |
| 80 | + const config = { |
| 81 | + series: [ |
| 82 | + { |
| 83 | + id: '0', |
| 84 | + data: [ |
| 85 | + [0, 0, 1], |
| 86 | + [0, 1, 2], |
| 87 | + [0, 2, 3], |
| 88 | + [1, 0, 4], |
| 89 | + [1, 1, 5], |
| 90 | + [1, 2, 6], |
| 91 | + ], |
| 92 | + }, |
| 93 | + ], |
| 94 | + xAxis: [{ position: 'none' }], |
| 95 | + yAxis: [{ position: 'none' }], |
| 96 | + width: 300, |
| 97 | + height: 300, |
| 98 | + margin: { top: 0, left: 0, bottom: 0, right: 0 }, |
| 99 | + } as const; |
| 100 | + |
| 101 | + it.skipIf(isJSDOM)('should provide the right context as second argument', async () => { |
| 102 | + const onItemClick = vi.fn(); |
| 103 | + const { container } = render(<Heatmap {...config} onItemClick={onItemClick} />); |
| 104 | + |
| 105 | + const cells = container.querySelectorAll<HTMLElement>(`.${heatmapClasses.cell}`); |
| 106 | + |
| 107 | + // The userEvent is not from React Testing Library, it's from Vitest, so we need to wrap it in act |
| 108 | + // I tried using React Testing Library's userEvent, but it didn't work because it doesn't set the clientX/Y properties |
| 109 | + // eslint-disable-next-line testing-library/no-unnecessary-act |
| 110 | + await act(async () => { |
| 111 | + await userEvent.click(cells[5]); |
| 112 | + }); |
| 113 | + |
| 114 | + expect(onItemClick).toHaveBeenLastCalledWith(expect.anything(), { |
| 115 | + type: 'heatmap', |
| 116 | + seriesId: '0', |
| 117 | + dataIndex: 5, |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments