@atlaskit/editor-plugin-table 2.7.2 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,39 +4,20 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
4
4
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
5
5
  import React, { useCallback, useMemo, useRef, useState } from 'react';
6
6
  import rafSchd from 'raf-schd';
7
- import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics';
8
7
  import { getGuidelinesWithHighlights } from '@atlaskit/editor-common/guideline';
9
8
  import { ResizerNext } from '@atlaskit/editor-common/resizer';
10
9
  import { resizerHandleShadowClassName } from '@atlaskit/editor-common/styles';
11
- import { TableMap } from '@atlaskit/editor-tables';
12
10
  import { findTable } from '@atlaskit/editor-tables/utils';
13
- import { COLUMN_MIN_WIDTH, getColgroupChildrenLength, hasTableBeenResized, previewScaleTable, scaleTable } from '../pm-plugins/table-resizing/utils';
11
+ import { COLUMN_MIN_WIDTH, getColgroupChildrenLength, previewScaleTable, scaleTable } from '../pm-plugins/table-resizing/utils';
14
12
  import { pluginKey as tableWidthPluginKey } from '../pm-plugins/table-width';
15
13
  import { TABLE_HIGHLIGHT_GAP, TABLE_SNAP_GAP } from '../ui/consts';
16
- import { getTableWidth } from '../utils';
14
+ import { generateResizedPayload, generateResizeFrameRatePayloads, useMeasureFramerate } from '../utils/analytics';
17
15
  import { defaultGuidelines, defaultGuidelineWidths } from '../utils/guidelines';
18
16
  import { findClosestSnap } from '../utils/snapping';
19
17
  var handles = {
20
18
  right: true
21
19
  };
22
20
  var tableHandleMarginTop = 12;
23
- var generateResizedPayload = function generateResizedPayload(props) {
24
- var _props$originalNode$a;
25
- var tableMap = TableMap.get(props.resizedNode);
26
- return {
27
- action: TABLE_ACTION.RESIZED,
28
- actionSubject: ACTION_SUBJECT.TABLE,
29
- eventType: EVENT_TYPE.TRACK,
30
- attributes: {
31
- newWidth: props.resizedNode.attrs.width,
32
- prevWidth: (_props$originalNode$a = props.originalNode.attrs.width) !== null && _props$originalNode$a !== void 0 ? _props$originalNode$a : null,
33
- nodeSize: props.resizedNode.nodeSize,
34
- totalTableWidth: hasTableBeenResized(props.resizedNode) ? getTableWidth(props.resizedNode) : null,
35
- totalRowCount: tableMap.height,
36
- totalColumnCount: tableMap.width
37
- }
38
- };
39
- };
40
21
  var getResizerHandleHeight = function getResizerHandleHeight(tableRef) {
41
22
  var tableHeight = tableRef === null || tableRef === void 0 ? void 0 : tableRef.clientHeight;
42
23
  var handleHeightSize = 'small';
@@ -79,6 +60,10 @@ export var TableResizer = function TableResizer(_ref) {
79
60
  setSnappingEnabled = _useState2[1];
80
61
  var resizerMinWidth = getResizerMinWidth(node);
81
62
  var handleHeightSize = getResizerHandleHeight(tableRef);
63
+ var _useMeasureFramerate = useMeasureFramerate(),
64
+ startMeasure = _useMeasureFramerate.startMeasure,
65
+ endMeasure = _useMeasureFramerate.endMeasure,
66
+ countFrames = _useMeasureFramerate.countFrames;
82
67
  var updateActiveGuidelines = useCallback(function (_ref2) {
83
68
  var gap = _ref2.gap,
84
69
  keys = _ref2.keys;
@@ -93,13 +78,14 @@ export var TableResizer = function TableResizer(_ref) {
93
78
  } : undefined;
94
79
  }, [snappingEnabled]);
95
80
  var handleResizeStart = useCallback(function () {
81
+ startMeasure();
96
82
  var dispatch = editorView.dispatch,
97
83
  tr = editorView.state.tr;
98
84
  dispatch(tr.setMeta(tableWidthPluginKey, {
99
85
  resizing: true
100
86
  }));
101
87
  setSnappingEnabled(displayGuideline(defaultGuidelines));
102
- }, [displayGuideline, editorView]);
88
+ }, [displayGuideline, editorView, startMeasure]);
103
89
  var handleResizeStop = useCallback(function (originalState, delta) {
104
90
  var newWidth = originalState.width + delta.width;
105
91
  var state = editorView.state,
@@ -108,8 +94,20 @@ export var TableResizer = function TableResizer(_ref) {
108
94
  var tr = state.tr.setMeta(tableWidthPluginKey, {
109
95
  resizing: false
110
96
  });
97
+ var frameRateSamples = endMeasure();
98
+ if (frameRateSamples.length > 0) {
99
+ var resizeFrameRatePayloads = generateResizeFrameRatePayloads({
100
+ docSize: state.doc.nodeSize,
101
+ frameRateSamples: frameRateSamples,
102
+ originalNode: node
103
+ });
104
+ resizeFrameRatePayloads.forEach(function (payload) {
105
+ var _attachAnalyticsEvent;
106
+ (_attachAnalyticsEvent = attachAnalyticsEvent(payload)) === null || _attachAnalyticsEvent === void 0 ? void 0 : _attachAnalyticsEvent(tr);
107
+ });
108
+ }
111
109
  if (typeof pos === 'number') {
112
- var _attachAnalyticsEvent;
110
+ var _attachAnalyticsEvent2;
113
111
  tr = tr.setNodeMarkup(pos, undefined, _objectSpread(_objectSpread({}, node.attrs), {}, {
114
112
  width: newWidth
115
113
  }));
@@ -121,10 +119,10 @@ export var TableResizer = function TableResizer(_ref) {
121
119
  parentWidth: newWidth
122
120
  }, editorView.domAtPos.bind(editorView))(tr);
123
121
  var scaledNode = tr.doc.nodeAt(pos);
124
- (_attachAnalyticsEvent = attachAnalyticsEvent(generateResizedPayload({
122
+ (_attachAnalyticsEvent2 = attachAnalyticsEvent(generateResizedPayload({
125
123
  originalNode: node,
126
124
  resizedNode: scaledNode
127
- }))) === null || _attachAnalyticsEvent === void 0 ? void 0 : _attachAnalyticsEvent(tr);
125
+ }))) === null || _attachAnalyticsEvent2 === void 0 ? void 0 : _attachAnalyticsEvent2(tr);
128
126
  }
129
127
  dispatch(tr);
130
128
 
@@ -132,8 +130,9 @@ export var TableResizer = function TableResizer(_ref) {
132
130
  displayGuideline([]);
133
131
  updateWidth(newWidth);
134
132
  return newWidth;
135
- }, [updateWidth, editorView, getPos, node, tableRef, displayGuideline, attachAnalyticsEvent]);
133
+ }, [updateWidth, editorView, getPos, node, tableRef, displayGuideline, attachAnalyticsEvent, endMeasure]);
136
134
  var handleResize = useCallback(function (originalState, delta) {
135
+ countFrames();
137
136
  var newWidth = originalState.width + delta.width;
138
137
  var pos = getPos();
139
138
  if (typeof pos !== 'number') {
@@ -148,7 +147,7 @@ export var TableResizer = function TableResizer(_ref) {
148
147
  updateActiveGuidelines(findClosestSnap(newWidth, defaultGuidelineWidths, defaultGuidelines, TABLE_HIGHLIGHT_GAP));
149
148
  updateWidth(newWidth);
150
149
  return newWidth;
151
- }, [editorView, getPos, node, tableRef, updateWidth, updateActiveGuidelines]);
150
+ }, [editorView, getPos, node, tableRef, updateWidth, updateActiveGuidelines, countFrames]);
152
151
  var scheduleResize = useMemo(function () {
153
152
  return rafSchd(handleResize);
154
153
  }, [handleResize]);
@@ -1,5 +1,9 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import { ACTION_SUBJECT, EVENT_TYPE, TABLE_ACTION } from '@atlaskit/editor-common/analytics';
1
3
  import { TableMap } from '@atlaskit/editor-tables/table-map';
2
4
  import { findTable, getSelectionRect } from '@atlaskit/editor-tables/utils';
5
+ import { hasTableBeenResized } from '../pm-plugins/table-resizing/utils';
6
+ import { getTableWidth } from '../utils';
3
7
  export function getSelectedTableInfo(selection) {
4
8
  var map;
5
9
  var totalRowCount = 0;
@@ -59,4 +63,139 @@ export var withEditorAnalyticsAPI = function withEditorAnalyticsAPI(payload) {
59
63
  };
60
64
  };
61
65
  };
66
+ };
67
+ export var generateResizedPayload = function generateResizedPayload(props) {
68
+ var _props$originalNode$a;
69
+ var tableMap = TableMap.get(props.resizedNode);
70
+ return {
71
+ action: TABLE_ACTION.RESIZED,
72
+ actionSubject: ACTION_SUBJECT.TABLE,
73
+ eventType: EVENT_TYPE.TRACK,
74
+ attributes: {
75
+ newWidth: props.resizedNode.attrs.width,
76
+ prevWidth: (_props$originalNode$a = props.originalNode.attrs.width) !== null && _props$originalNode$a !== void 0 ? _props$originalNode$a : null,
77
+ nodeSize: props.resizedNode.nodeSize,
78
+ totalTableWidth: hasTableBeenResized(props.resizedNode) ? getTableWidth(props.resizedNode) : null,
79
+ totalRowCount: tableMap.height,
80
+ totalColumnCount: tableMap.width
81
+ }
82
+ };
83
+ };
84
+ export var reduceResizeFrameRateSamples = function reduceResizeFrameRateSamples(frameRateSamples) {
85
+ if (frameRateSamples.length > 1) {
86
+ var frameRateSum = frameRateSamples.reduce(function (sum, frameRate, index) {
87
+ if (index === 0) {
88
+ return sum;
89
+ } else {
90
+ return sum + frameRate;
91
+ }
92
+ }, 0);
93
+ var averageFrameRate = Math.round(frameRateSum / (frameRateSamples.length - 1));
94
+ return [frameRateSamples[0], averageFrameRate];
95
+ } else {
96
+ return frameRateSamples;
97
+ }
98
+ };
99
+ export var generateResizeFrameRatePayloads = function generateResizeFrameRatePayloads(props) {
100
+ var reducedResizeFrameRateSamples = reduceResizeFrameRateSamples(props.frameRateSamples);
101
+ return reducedResizeFrameRateSamples.map(function (frameRateSample, index) {
102
+ return {
103
+ action: TABLE_ACTION.RESIZE_PERF_SAMPLING,
104
+ actionSubject: ACTION_SUBJECT.TABLE,
105
+ eventType: EVENT_TYPE.OPERATIONAL,
106
+ attributes: {
107
+ frameRate: frameRateSample,
108
+ nodeSize: props.originalNode.nodeSize,
109
+ docSize: props.docSize,
110
+ isInitialSample: index === 0
111
+ }
112
+ };
113
+ });
114
+ };
115
+
116
+ /**
117
+ * Measures the framerate of a component over a given time period.
118
+ */
119
+ export var useMeasureFramerate = function useMeasureFramerate() {
120
+ var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
121
+ var _config$maxSamples = config.maxSamples,
122
+ maxSamples = _config$maxSamples === void 0 ? 10 : _config$maxSamples,
123
+ _config$minFrames = config.minFrames,
124
+ minFrames = _config$minFrames === void 0 ? 5 : _config$minFrames,
125
+ _config$minTimeMs = config.minTimeMs,
126
+ minTimeMs = _config$minTimeMs === void 0 ? 500 : _config$minTimeMs,
127
+ _config$sampleRateMs = config.sampleRateMs,
128
+ sampleRateMs = _config$sampleRateMs === void 0 ? 1000 : _config$sampleRateMs,
129
+ _config$timeoutMs = config.timeoutMs,
130
+ timeoutMs = _config$timeoutMs === void 0 ? 200 : _config$timeoutMs;
131
+ var frameCount = useRef(0);
132
+ var lastTime = useRef(0);
133
+ var timeoutId = useRef();
134
+ var frameRateSamples = useRef([]);
135
+ useEffect(function () {
136
+ return function () {
137
+ if (timeoutId.current) {
138
+ clearTimeout(timeoutId.current);
139
+ }
140
+ };
141
+ }, []);
142
+ var startMeasure = function startMeasure() {
143
+ frameCount.current = 0;
144
+ lastTime.current = performance.now();
145
+ };
146
+
147
+ /**
148
+ * Returns an array of frame rate samples as integers.
149
+ */
150
+ var endMeasure = function endMeasure() {
151
+ var samples = frameRateSamples.current;
152
+ frameRateSamples.current = [];
153
+ return samples;
154
+ };
155
+ var sampleFrameRate = function sampleFrameRate() {
156
+ var delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
157
+ var currentTime = performance.now();
158
+ var deltaTime = currentTime - lastTime.current - delay;
159
+ var isValidSample = deltaTime > minTimeMs && frameCount.current >= minFrames;
160
+ if (isValidSample) {
161
+ var frameRate = Math.round(frameCount.current / (deltaTime / 1000));
162
+ frameRateSamples.current.push(frameRate);
163
+ }
164
+ frameCount.current = 0;
165
+ lastTime.current = 0;
166
+ };
167
+
168
+ /**
169
+ * Counts the number of frames that occur within a given time period. Intended to be called
170
+ * inside a `requestAnimationFrame` callback.
171
+ */
172
+ var countFrames = function countFrames() {
173
+ if (frameRateSamples.current.length >= maxSamples && timeoutId.current) {
174
+ clearTimeout(timeoutId.current);
175
+ return;
176
+ }
177
+
178
+ /**
179
+ * Allows us to keep counting frames even if `startMeasure` is not called
180
+ */
181
+ if (lastTime.current === 0) {
182
+ lastTime.current = performance.now();
183
+ }
184
+ frameCount.current++;
185
+ if (timeoutId.current) {
186
+ clearTimeout(timeoutId.current);
187
+ }
188
+ if (performance.now() - lastTime.current > sampleRateMs) {
189
+ sampleFrameRate();
190
+ } else {
191
+ timeoutId.current = setTimeout(function () {
192
+ return sampleFrameRate(timeoutMs);
193
+ }, timeoutMs);
194
+ }
195
+ };
196
+ return {
197
+ startMeasure: startMeasure,
198
+ endMeasure: endMeasure,
199
+ countFrames: countFrames
200
+ };
62
201
  };
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-table",
3
- "version": "2.7.2",
3
+ "version": "2.8.0",
4
4
  "sideEffects": false
5
5
  }
@@ -1,5 +1,6 @@
1
- import type { AnalyticsEventPayload, AnalyticsEventPayloadCallback, EditorAnalyticsAPI } from '@atlaskit/editor-common/analytics';
1
+ import type { AnalyticsEventPayload, AnalyticsEventPayloadCallback, EditorAnalyticsAPI, TableEventPayload } from '@atlaskit/editor-common/analytics';
2
2
  import { HigherOrderCommand } from '@atlaskit/editor-common/types';
3
+ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
4
  import { Selection } from '@atlaskit/editor-prosemirror/state';
4
5
  import { TableMap } from '@atlaskit/editor-tables/table-map';
5
6
  export declare function getSelectedTableInfo(selection: Selection): {
@@ -16,3 +17,29 @@ export declare function getSelectedCellInfo(selection: Selection): {
16
17
  totalCells: number;
17
18
  };
18
19
  export declare const withEditorAnalyticsAPI: (payload: AnalyticsEventPayload | AnalyticsEventPayloadCallback) => (editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null) => HigherOrderCommand;
20
+ interface UseMeasureFramerateConfig {
21
+ maxSamples?: number;
22
+ minFrames?: number;
23
+ minTimeMs?: number;
24
+ sampleRateMs?: number;
25
+ timeoutMs?: number;
26
+ }
27
+ export declare const generateResizedPayload: (props: {
28
+ originalNode: PMNode;
29
+ resizedNode: PMNode;
30
+ }) => TableEventPayload;
31
+ export declare const reduceResizeFrameRateSamples: (frameRateSamples: number[]) => number[];
32
+ export declare const generateResizeFrameRatePayloads: (props: {
33
+ docSize: number;
34
+ frameRateSamples: number[];
35
+ originalNode: PMNode;
36
+ }) => TableEventPayload[];
37
+ /**
38
+ * Measures the framerate of a component over a given time period.
39
+ */
40
+ export declare const useMeasureFramerate: (config?: UseMeasureFramerateConfig) => {
41
+ startMeasure: () => void;
42
+ endMeasure: () => number[];
43
+ countFrames: () => void;
44
+ };
45
+ export {};
@@ -1,5 +1,6 @@
1
- import type { AnalyticsEventPayload, AnalyticsEventPayloadCallback, EditorAnalyticsAPI } from '@atlaskit/editor-common/analytics';
1
+ import type { AnalyticsEventPayload, AnalyticsEventPayloadCallback, EditorAnalyticsAPI, TableEventPayload } from '@atlaskit/editor-common/analytics';
2
2
  import { HigherOrderCommand } from '@atlaskit/editor-common/types';
3
+ import type { Node as PMNode } from '@atlaskit/editor-prosemirror/model';
3
4
  import { Selection } from '@atlaskit/editor-prosemirror/state';
4
5
  import { TableMap } from '@atlaskit/editor-tables/table-map';
5
6
  export declare function getSelectedTableInfo(selection: Selection): {
@@ -16,3 +17,29 @@ export declare function getSelectedCellInfo(selection: Selection): {
16
17
  totalCells: number;
17
18
  };
18
19
  export declare const withEditorAnalyticsAPI: (payload: AnalyticsEventPayload | AnalyticsEventPayloadCallback) => (editorAnalyticsAPI: EditorAnalyticsAPI | undefined | null) => HigherOrderCommand;
20
+ interface UseMeasureFramerateConfig {
21
+ maxSamples?: number;
22
+ minFrames?: number;
23
+ minTimeMs?: number;
24
+ sampleRateMs?: number;
25
+ timeoutMs?: number;
26
+ }
27
+ export declare const generateResizedPayload: (props: {
28
+ originalNode: PMNode;
29
+ resizedNode: PMNode;
30
+ }) => TableEventPayload;
31
+ export declare const reduceResizeFrameRateSamples: (frameRateSamples: number[]) => number[];
32
+ export declare const generateResizeFrameRatePayloads: (props: {
33
+ docSize: number;
34
+ frameRateSamples: number[];
35
+ originalNode: PMNode;
36
+ }) => TableEventPayload[];
37
+ /**
38
+ * Measures the framerate of a component over a given time period.
39
+ */
40
+ export declare const useMeasureFramerate: (config?: UseMeasureFramerateConfig) => {
41
+ startMeasure: () => void;
42
+ endMeasure: () => number[];
43
+ countFrames: () => void;
44
+ };
45
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/editor-plugin-table",
3
- "version": "2.7.2",
3
+ "version": "2.8.0",
4
4
  "description": "Table plugin for the @atlaskit/editor",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@atlaskit/adf-schema": "^28.0.0",
31
- "@atlaskit/editor-common": "^74.41.0",
31
+ "@atlaskit/editor-common": "^74.42.0",
32
32
  "@atlaskit/editor-palette": "1.5.1",
33
33
  "@atlaskit/editor-plugin-analytics": "^0.1.0",
34
34
  "@atlaskit/editor-plugin-content-insertion": "^0.0.7",
@@ -67,6 +67,7 @@
67
67
  "@atlassian/atlassian-frontend-prettier-config-1.0.1": "npm:@atlassian/atlassian-frontend-prettier-config@1.0.1",
68
68
  "@atlassian/feature-flags-test-utils": "^0.1.2",
69
69
  "@testing-library/react": "^12.1.5",
70
+ "@testing-library/react-hooks": "^8.0.1",
70
71
  "raf-stub": "^2.0.1",
71
72
  "typescript": "~4.9.5"
72
73
  },
@@ -29,6 +29,23 @@ import {
29
29
  import { pluginKey } from '../../../plugins/table/pm-plugins/plugin-key';
30
30
  import { TablePluginState } from '../../../plugins/table/types';
31
31
 
32
+ const mockStartMeasure = jest.fn();
33
+ const mockEndMeasure = jest.fn(() => {
34
+ return [51, 52, 53, 54];
35
+ });
36
+ const mockCountFrames = jest.fn();
37
+
38
+ jest.mock('../../../plugins/table/utils/analytics', () => ({
39
+ ...jest.requireActual('../../../plugins/table/utils/analytics'),
40
+ useMeasureFramerate: () => {
41
+ return {
42
+ startMeasure: mockStartMeasure,
43
+ endMeasure: mockEndMeasure,
44
+ countFrames: mockCountFrames,
45
+ };
46
+ },
47
+ }));
48
+
32
49
  describe('table -> nodeviews -> TableContainer.tsx', () => {
33
50
  const createEditor = createEditorFactory<TablePluginState>();
34
51
  const editor = (
@@ -202,7 +219,7 @@ describe('table -> nodeviews -> TableContainer.tsx', () => {
202
219
  fireEvent.mouseMove(container.querySelector('.resizer-handle-right')!);
203
220
  fireEvent.mouseUp(container.querySelector('.resizer-handle-right')!);
204
221
 
205
- expect(analyticsMock).toHaveBeenLastCalledWith({
222
+ expect(analyticsMock).toHaveBeenCalledWith({
206
223
  action: TABLE_ACTION.RESIZED,
207
224
  actionSubject: ACTION_SUBJECT.TABLE,
208
225
  eventType: EVENT_TYPE.TRACK,
@@ -215,6 +232,45 @@ describe('table -> nodeviews -> TableContainer.tsx', () => {
215
232
  totalColumnCount: 3,
216
233
  },
217
234
  });
235
+
236
+ expect(analyticsMock).toHaveBeenCalledWith({
237
+ action: TABLE_ACTION.RESIZE_PERF_SAMPLING,
238
+ actionSubject: ACTION_SUBJECT.TABLE,
239
+ eventType: EVENT_TYPE.OPERATIONAL,
240
+ attributes: {
241
+ docSize: 22,
242
+ frameRate: 51,
243
+ isInitialSample: true,
244
+ nodeSize: 20,
245
+ },
246
+ });
247
+
248
+ expect(analyticsMock).toHaveBeenCalledWith({
249
+ action: TABLE_ACTION.RESIZE_PERF_SAMPLING,
250
+ actionSubject: ACTION_SUBJECT.TABLE,
251
+ eventType: EVENT_TYPE.OPERATIONAL,
252
+ attributes: {
253
+ docSize: 22,
254
+ frameRate: 53,
255
+ isInitialSample: false,
256
+ nodeSize: 20,
257
+ },
258
+ });
259
+
260
+ analyticsMock.mockReset();
261
+ });
262
+
263
+ test('calls useMeasureFramerate handlers', async () => {
264
+ const { container } = buildContainer({ layout: 'wide' });
265
+
266
+ fireEvent.mouseDown(container.querySelector('.resizer-handle-right')!);
267
+ fireEvent.mouseMove(container.querySelector('.resizer-handle-right')!, {
268
+ clientX: 100,
269
+ });
270
+ fireEvent.mouseUp(container.querySelector('.resizer-handle-right')!);
271
+
272
+ expect(mockStartMeasure).toHaveBeenCalled();
273
+ expect(mockEndMeasure).toHaveBeenCalled();
218
274
  });
219
275
  });
220
276
  });
@@ -0,0 +1,98 @@
1
+ import { renderHook } from '@testing-library/react-hooks';
2
+
3
+ import {
4
+ ACTION_SUBJECT,
5
+ EVENT_TYPE,
6
+ TABLE_ACTION,
7
+ } from '@atlaskit/editor-common/analytics';
8
+
9
+ import {
10
+ generateResizeFrameRatePayloads,
11
+ reduceResizeFrameRateSamples,
12
+ useMeasureFramerate,
13
+ } from '../../../plugins/table/utils/analytics';
14
+
15
+ describe('reduceResizeFrameRateSamples()', () => {
16
+ it('should return the same array if it has only one element', () => {
17
+ expect(reduceResizeFrameRateSamples([1])).toEqual([1]);
18
+ });
19
+
20
+ it('should return the first element and the average of the array if length > 1', () => {
21
+ expect(reduceResizeFrameRateSamples([3, 2, 4, 6])).toEqual([3, 4]);
22
+ });
23
+ });
24
+
25
+ describe('generateResizeFrameRatePayloads()', () => {
26
+ it('should return an empty array if the array is empty', () => {
27
+ expect(
28
+ generateResizeFrameRatePayloads({
29
+ docSize: 10,
30
+ frameRateSamples: [],
31
+ originalNode: { nodeSize: 5 } as any,
32
+ }),
33
+ ).toEqual([]);
34
+ });
35
+
36
+ it('should return an array of payloads with the correct attributes', () => {
37
+ expect(
38
+ generateResizeFrameRatePayloads({
39
+ docSize: 10,
40
+ frameRateSamples: [3, 2, 4, 6],
41
+ originalNode: { nodeSize: 5 } as any,
42
+ }),
43
+ ).toEqual([
44
+ {
45
+ action: TABLE_ACTION.RESIZE_PERF_SAMPLING,
46
+ actionSubject: ACTION_SUBJECT.TABLE,
47
+ eventType: EVENT_TYPE.OPERATIONAL,
48
+ attributes: {
49
+ frameRate: 3,
50
+ nodeSize: 5,
51
+ docSize: 10,
52
+ isInitialSample: true,
53
+ },
54
+ },
55
+ {
56
+ action: TABLE_ACTION.RESIZE_PERF_SAMPLING,
57
+ actionSubject: ACTION_SUBJECT.TABLE,
58
+ eventType: EVENT_TYPE.OPERATIONAL,
59
+ attributes: {
60
+ frameRate: 4,
61
+ nodeSize: 5,
62
+ docSize: 10,
63
+ isInitialSample: false,
64
+ },
65
+ },
66
+ ]);
67
+ });
68
+ });
69
+
70
+ describe('useMeasureFramerate()', () => {
71
+ jest.useFakeTimers('modern');
72
+
73
+ it('should return the correct handlers', () => {
74
+ const { result } = renderHook(() => useMeasureFramerate());
75
+ const { startMeasure, endMeasure, countFrames } = result.current;
76
+
77
+ expect(startMeasure).toBeInstanceOf(Function);
78
+ expect(endMeasure).toBeInstanceOf(Function);
79
+ expect(countFrames).toBeInstanceOf(Function);
80
+ });
81
+
82
+ it('should return the correct frame rate sample', async () => {
83
+ const { result } = renderHook(() =>
84
+ useMeasureFramerate({ minTimeMs: 0, minFrames: 0, sampleRateMs: 0 }),
85
+ );
86
+
87
+ const { startMeasure, endMeasure, countFrames } = result.current;
88
+ jest.advanceTimersByTime(100);
89
+ startMeasure();
90
+ jest.advanceTimersByTime(100);
91
+ countFrames();
92
+ const samples = endMeasure();
93
+
94
+ expect(samples).toEqual([10]);
95
+
96
+ jest.useRealTimers();
97
+ });
98
+ });