forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlameGraph.test.tsx
115 lines (100 loc) · 3.5 KB
/
FlameGraph.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { fireEvent, render, screen } from '@testing-library/react';
import * as React from 'react';
import { createDataFrame } from '@grafana/data';
import { ColorScheme, SelectedView } from '../types';
import FlameGraph from './FlameGraph';
import { FlameGraphDataContainer } from './dataTransform';
import { data } from './testData/dataNestedSet';
import 'jest-canvas-mock';
jest.mock('react-use', () => {
const reactUse = jest.requireActual('react-use');
return {
...reactUse,
useMeasure: () => {
const ref = React.useRef();
return [ref, { width: 1600 }];
},
};
});
describe('FlameGraph', () => {
function setup(props?: Partial<React.ComponentProps<typeof FlameGraph>>) {
const flameGraphData = createDataFrame(data);
const container = new FlameGraphDataContainer(flameGraphData, { collapsing: true });
const setRangeMin = jest.fn();
const setRangeMax = jest.fn();
const onItemFocused = jest.fn();
const onSandwich = jest.fn();
const onFocusPillClick = jest.fn();
const onSandwichPillClick = jest.fn();
const renderResult = render(
<FlameGraph
data={container}
rangeMin={0}
rangeMax={1}
setRangeMin={setRangeMin}
setRangeMax={setRangeMax}
onItemFocused={onItemFocused}
textAlign={'left'}
onSandwich={onSandwich}
onFocusPillClick={onFocusPillClick}
onSandwichPillClick={onSandwichPillClick}
colorScheme={ColorScheme.ValueBased}
selectedView={SelectedView.FlameGraph}
search={''}
collapsedMap={container.getCollapsedMap()}
setCollapsedMap={() => {}}
{...props}
/>
);
return {
renderResult,
mocks: {
setRangeMax,
setRangeMin,
onItemFocused,
onSandwich,
onFocusPillClick,
onSandwichPillClick,
},
};
}
it('should render without error', async () => {
setup();
});
it('should render correctly', async () => {
setup();
const canvas = screen.getByTestId('flameGraph') as HTMLCanvasElement;
const ctx = canvas!.getContext('2d');
const calls = ctx!.__getDrawCalls();
expect(calls).toMatchSnapshot();
});
it('should render metadata', async () => {
setup();
expect(screen.getByText('16.5 Bil | 16.5 Bil samples (Count)')).toBeDefined();
});
it('should render context menu + extra items', async () => {
const event = new MouseEvent('click', { bubbles: true });
Object.defineProperty(event, 'offsetX', { get: () => 10 });
Object.defineProperty(event, 'offsetY', { get: () => 10 });
Object.defineProperty(HTMLCanvasElement.prototype, 'clientWidth', { configurable: true, value: 500 });
setup({
getExtraContextMenuButtons: (clickedItemData, data, state) => {
expect(clickedItemData).toMatchObject({ posX: 0, posY: 0, label: 'total' });
expect(data.length).toEqual(1101);
expect(state).toEqual({
selectedView: SelectedView.FlameGraph,
isDiff: false,
search: '',
collapseConfig: undefined,
});
return [{ label: 'test extra item', icon: 'eye', onClick: () => {} }];
},
});
const canvas = screen.getByTestId('flameGraph') as HTMLCanvasElement;
expect(canvas).toBeInTheDocument();
expect(screen.queryByTestId('contextMenu')).not.toBeInTheDocument();
fireEvent(canvas, event);
expect(screen.getByTestId('contextMenu')).toBeInTheDocument();
expect(screen.getByText('test extra item')).toBeInTheDocument();
});
});