summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/test/node/components/reps/test-helpers.js
blob: d601f71e883012cb992a990923d5a3c3caff8c54 (plain)
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
/* 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/>. */

"use strict";

const { shallow } = require("enzyme");

const {
  lengthBubble,
} = require("resource://devtools/client/shared/components/reps/shared/grip-length-bubble.js");
const {
  maxLengthMap: arrayLikeMaxLengthMap,
  getLength: getArrayLikeLength,
} = require("resource://devtools/client/shared/components/reps/reps/grip-array.js");
const {
  maxLengthMap: mapMaxLengths,
  getLength: getMapLength,
} = require("resource://devtools/client/shared/components/reps/reps/grip-map.js");
const {
  getGripPreviewItems,
} = require("resource://devtools/client/shared/components/reps/reps/rep-utils.js");
const nodeConstants = require("resource://devtools/client/shared/components/reps/shared/dom-node-constants.js");

/**
 * Get an array of all the items from the grip in parameter (including the grip
 * itself) which can be selected in the inspector.
 *
 * @param {Object} Grip
 * @return {Array} Flat array of the grips which can be selected in the
 *                 inspector
 */
function getSelectableInInspectorGrips(grip) {
  const grips = new Set(getFlattenedGrips([grip]));
  return [...grips].filter(isGripSelectableInInspector);
}

/**
 * Indicate if a Grip can be selected in the inspector,
 * i.e. if it represents a node element.
 *
 * @param {Object} Grip
 * @return {Boolean}
 */
function isGripSelectableInInspector(grip) {
  return (
    grip &&
    typeof grip === "object" &&
    grip.preview &&
    [nodeConstants.TEXT_NODE, nodeConstants.ELEMENT_NODE].includes(
      grip.preview.nodeType
    )
  );
}

/**
 * Get a flat array of all the grips and their preview items.
 *
 * @param {Array} Grips
 * @return {Array} Flat array of the grips and their preview items
 */
function getFlattenedGrips(grips) {
  return grips.reduce((res, grip) => {
    const previewItems = getGripPreviewItems(grip);
    const flatPreviewItems = previewItems.length
      ? getFlattenedGrips(previewItems)
      : [];

    return [...res, grip, ...flatPreviewItems];
  }, []);
}

function expectActorAttribute(wrapper, expectedValue) {
  const actorIdAttribute = "data-link-actor-id";
  const attrElement = wrapper.find(`[${actorIdAttribute}]`);
  expect(attrElement.exists()).toBeTruthy();
  expect(attrElement.first().prop("data-link-actor-id")).toBe(expectedValue);
}

function getGripLengthBubbleText(object, props) {
  const component = lengthBubble({
    object,
    maxLengthMap: arrayLikeMaxLengthMap,
    getLength: getArrayLikeLength,
    ...props,
  });

  return component ? shallow(component).text() : "";
}

function getMapLengthBubbleText(object, props) {
  return getGripLengthBubbleText(object, {
    maxLengthMap: mapMaxLengths,
    getLength: getMapLength,
    showZeroLength: true,
    ...props,
  });
}

function createGripMapEntry(key, value) {
  return {
    type: "mapEntry",
    preview: {
      key,
      value,
    },
  };
}

module.exports = {
  createGripMapEntry,
  expectActorAttribute,
  getSelectableInInspectorGrips,
  getGripLengthBubbleText,
  getMapLengthBubbleText,
};