forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrackVisualProgress.test.js
147 lines (128 loc) · 4.65 KB
/
TrackVisualProgress.test.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import type { IndexIntoSamplesTable, CssPixels } from 'firefox-profiler/types';
import * as React from 'react';
import { Provider } from 'react-redux';
import { fireEvent } from '@testing-library/react';
import { render } from 'firefox-profiler/test/fixtures/testing-library';
import { TrackVisualProgress } from '../../components/timeline/TrackVisualProgress';
import { ensureExists } from '../../utils/flow';
import {
autoMockCanvasContext,
flushDrawLog,
} from '../fixtures/mocks/canvas-context';
import { autoMockElementSize } from '../fixtures/mocks/element-size';
import { mockRaf } from '../fixtures/mocks/request-animation-frame';
import { storeWithProfile } from '../fixtures/stores';
import {
addRootOverlayElement,
removeRootOverlayElement,
getMouseEvent,
} from '../fixtures/utils';
import { getVisualProgressTrackProfile } from '../fixtures/profiles/processed-profile';
// The following constants determine the size of the drawn graph.
const SAMPLE_COUNT = 7;
const PIXELS_PER_SAMPLE = 10;
const GRAPH_WIDTH = PIXELS_PER_SAMPLE * SAMPLE_COUNT;
const GRAPH_HEIGHT = 10;
function getSamplesPixelPosition(
sampleIndex: IndexIntoSamplesTable
): CssPixels {
// Compute the pixel position of the center of a given sample.
return sampleIndex * PIXELS_PER_SAMPLE + PIXELS_PER_SAMPLE * 0.5;
}
/**
* This test verifies that the memory track can draw a graph of the memory.
*/
describe('TrackVisualProgress', function () {
function setup() {
const profile = getVisualProgressTrackProfile(
Array(SAMPLE_COUNT).fill('A').join(' ')
);
const {
meta: { visualMetrics },
} = profile;
if (!visualMetrics) {
throw new Error('This profile does not contain visual Metrics');
}
const { VisualProgress } = visualMetrics;
const store = storeWithProfile(profile);
const { getState, dispatch } = store;
const flushRafCalls = mockRaf();
const renderResult = render(
<Provider store={store}>
<TrackVisualProgress
progressGraphData={VisualProgress}
graphDotTooltipText=" visual completeness at this time"
/>
</Provider>
);
const { container } = renderResult;
// WithSize uses requestAnimationFrame
flushRafCalls();
const canvas = ensureExists(
container.querySelector('.timelineTrackVisualProgressCanvas'),
`Couldn't find the memory canvas, with selector .timelineTrackVisualProgressCanvas`
);
const getTooltipContents = () =>
document.querySelector('.timelineTrackVisualProgressTooltip');
const getVisualProgressDot = () =>
container.querySelector('.timelineTrackVisualProgressGraphDot');
const moveMouseAtCounter = (index) =>
fireEvent(
canvas,
getMouseEvent('mousemove', { pageX: getSamplesPixelPosition(index) })
);
return {
...renderResult,
dispatch,
getState,
profile,
store,
canvas,
getTooltipContents,
moveMouseAtCounter,
flushRafCalls,
getVisualProgressDot,
};
}
autoMockCanvasContext();
autoMockElementSize({ width: GRAPH_WIDTH, height: GRAPH_HEIGHT });
beforeEach(addRootOverlayElement);
afterEach(removeRootOverlayElement);
it('matches the component snapshot', () => {
const { container } = setup();
expect(container.firstChild).toMatchSnapshot();
});
it('matches the 2d canvas draw snapshot', () => {
const { flushRafCalls } = setup();
flushRafCalls();
expect(flushDrawLog()).toMatchSnapshot();
});
it('can create a tooltip', function () {
const { moveMouseAtCounter, getTooltipContents, canvas } = setup();
expect(getTooltipContents()).toBeFalsy();
moveMouseAtCounter(5000);
expect(getTooltipContents()).toBeTruthy();
fireEvent.mouseLeave(canvas);
expect(getTooltipContents()).toBeFalsy();
});
it('has a tooltip that matches the snapshot', function () {
const { moveMouseAtCounter, getTooltipContents } = setup();
moveMouseAtCounter(5000);
expect(getTooltipContents()).toMatchSnapshot();
});
it('draws a dot on the graph', function () {
const { moveMouseAtCounter, getVisualProgressDot } = setup();
expect(getVisualProgressDot()).toBeFalsy();
moveMouseAtCounter(5000);
expect(getVisualProgressDot()).toBeTruthy();
});
it('draws a dot that matches the snapshot', function () {
const { moveMouseAtCounter, getVisualProgressDot } = setup();
moveMouseAtCounter(5000);
expect(getVisualProgressDot()).toMatchSnapshot();
});
});