summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/test/chrome/test_smart-trace-grouping.html
blob: 174d0f87b4e638c05b46ba6d05334098e1804c8a (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
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
<!-- 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/. -->
<!DOCTYPE HTML>
<html>
<!--
Test the rendering of a stack trace
-->
<head>
  <meta charset="utf-8">
  <title>StackTrace component test</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<script src="head.js"></script>
<script>
"use strict";

window.onload = function() {
  const ReactDOM = browserRequire("devtools/client/shared/vendor/react-dom");
  const React = browserRequire("devtools/client/shared/vendor/react");
  const SmartTrace = React.createFactory(
    browserRequire("devtools/client/shared/components/SmartTrace"));
  ok(SmartTrace, "Got the SmartTrace factory");

  add_task(async function() {
    const REACT_FRAMES_COUNT = 10;

    const stacktrace = [
      {
        filename: "https://myfile.com/mahscripts.js",
        lineNumber: 55,
        columnNumber: 10,
        functionName: null,
      },
      // Simulated Redux frame
      {
        functionName: "rootReducer",
        filename: "https://myfile.com/loader.js -> https://myfile.com/redux.js",
        lineNumber: 2,
      },
      {
        functionName: "loadFunc",
        filename: "https://myfile.com/loader.js -> https://myfile.com/loadee.js",
        lineNumber: 10,
      },
      // Simulated react frames
      ...(Array.from({length: REACT_FRAMES_COUNT}, (_, i) => ({
        functionName: "internalReact" + (REACT_FRAMES_COUNT - i),
        filename: "https://myfile.com/loader.js -> https://myfile.com/react.js",
        lineNumber: Number(i.toString().repeat(2)),
      }))),
      {
        filename: "https://myfile.com/mahscripts.js",
        lineNumber: 10,
        columnNumber: 3,
        functionName: "onClick",
      },
    ];

    const props = {
      stacktrace,
      onViewSourceInDebugger: () => {},
    };

    const trace = ReactDOM.render(SmartTrace(props), window.document.body);
    await forceRender(trace);

    const traceEl = ReactDOM.findDOMNode(trace);
    ok(traceEl, "Rendered SmartTrace has an element");

    isDeeply(getStacktraceText(traceEl), [
      `<anonymous> https://myfile.com/mahscripts.js:55`,
      `rootReducer Redux`,
      `loadFunc https://myfile.com/loadee.js:10`,
      `▶︎ React 10`,
      `onClick https://myfile.com/mahscripts.js:10`,
    ], "React frames are grouped - Redux frame is not");

    info("Expand React group");
    let onReactGroupExpanded = waitFor(() =>
      traceEl.querySelector(".frames-group.expanded"));
    traceEl.querySelector(".group").click();
    await onReactGroupExpanded;

    isDeeply(getStacktraceText(traceEl), [
      `<anonymous> https://myfile.com/mahscripts.js:55`,
      `rootReducer Redux`,
      `loadFunc https://myfile.com/loadee.js:10`,
      `▼ React 10`,
      `| internalReact10`,
      `| internalReact9`,
      `| internalReact8`,
      `| internalReact7`,
      `| internalReact6`,
      `| internalReact5`,
      `| internalReact4`,
      `| internalReact3`,
      `| internalReact2`,
      `| internalReact1`,
      `onClick https://myfile.com/mahscripts.js:10`,
    ], "React frames can be expanded");

    info("Collapse React group");
    onReactGroupExpanded = waitFor(() =>
      !traceEl.querySelector(".frames-group.expanded"));
    traceEl.querySelector(".group").click();
    await onReactGroupExpanded;

    isDeeply(getStacktraceText(traceEl), [
      `<anonymous> https://myfile.com/mahscripts.js:55`,
      `rootReducer Redux`,
      `loadFunc https://myfile.com/loadee.js:10`,
      `▶︎ React 10`,
      `onClick https://myfile.com/mahscripts.js:10`,
    ], "React frames can be collapsed");
  });

  function getStacktraceText(traceElement) {
    return Array.from(traceElement.querySelectorAll(".frame, .frames-group")).map(el => {
        // If it's a group, we want to append an arrow representing the group state
        if (el.classList.contains("frames-group")) {
          const arrow = el.classList.contains("expanded") ? "▼" : "▶︎";
          const content = el.querySelector(".group").textContent.trim();
          return `${arrow} ${content}`;
        }

        const title = el.querySelector(".title");
        if (el.closest(".frames-group")) {
          return `| ${title.textContent}`;
        }

        const location = el.querySelector(".location");
        return `${title.textContent} ${location.textContent}`;
      });
  }
};
</script>
</body>
</html>