summaryTableLine.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const React = require('react');
  2. function MetricCells({ metrics }) {
  3. const { classForPercent, pct, covered, missed, total } = metrics;
  4. return (
  5. <>
  6. <td className={'pct ' + classForPercent}>{Math.round(pct)}% </td>
  7. <td className={classForPercent}>
  8. <div className="bar">
  9. <div
  10. className={`bar__data ${classForPercent} ${classForPercent}--dark`}
  11. style={{ width: pct + '%' }}
  12. ></div>
  13. </div>
  14. </td>
  15. <td className={'abs ' + classForPercent}>{covered}</td>
  16. <td className={'abs ' + classForPercent}>{missed}</td>
  17. <td className={'abs ' + classForPercent}>{total}</td>
  18. </>
  19. );
  20. }
  21. function FileCell({
  22. file,
  23. prefix,
  24. expandedLines,
  25. setExpandedLines,
  26. hasChildren,
  27. setFileFilter
  28. }) {
  29. if (hasChildren) {
  30. const expandedIndex = expandedLines.indexOf(prefix + file);
  31. const isExpanded = expandedIndex >= 0;
  32. const newExpandedLines = isExpanded
  33. ? [
  34. ...expandedLines.slice(0, expandedIndex),
  35. ...expandedLines.slice(expandedIndex + 1)
  36. ]
  37. : [...expandedLines, prefix + file];
  38. return (
  39. <>
  40. <button
  41. type="button"
  42. onClick={() => setExpandedLines(newExpandedLines)}
  43. className="expandbutton"
  44. >
  45. {isExpanded ? String.fromCharCode(0x2013) : '+'}
  46. </button>
  47. <a
  48. href="javascript:void(0)"
  49. onClick={() => setFileFilter(prefix + file)}
  50. >
  51. {file}
  52. </a>
  53. </>
  54. );
  55. } else {
  56. return <a href={`./${prefix}${file}.html`}>{file}</a>;
  57. }
  58. }
  59. function getWorstMetricClassForPercent(metricsToShow, metrics) {
  60. let classForPercent = 'none';
  61. for (const metricToShow in metricsToShow) {
  62. if (metricsToShow[metricToShow]) {
  63. const metricClassForPercent = metrics[metricToShow].classForPercent;
  64. // ignore none metrics so they don't change whats shown
  65. if (metricClassForPercent === 'none') {
  66. continue;
  67. }
  68. // if the metric low or lower than whats currently being used, replace it
  69. if (
  70. metricClassForPercent == 'low' ||
  71. (metricClassForPercent === 'medium' &&
  72. classForPercent !== 'low') ||
  73. (metricClassForPercent === 'high' &&
  74. classForPercent !== 'low' &&
  75. classForPercent !== 'medium')
  76. ) {
  77. classForPercent = metricClassForPercent;
  78. }
  79. }
  80. }
  81. return classForPercent;
  82. }
  83. module.exports = function SummaryTableLine({
  84. prefix,
  85. metrics,
  86. file,
  87. children,
  88. tabSize,
  89. metricsToShow,
  90. expandedLines,
  91. setExpandedLines,
  92. fileFilter,
  93. setFileFilter
  94. }) {
  95. tabSize = tabSize || 0;
  96. if (children && tabSize > 0) {
  97. tabSize--;
  98. }
  99. prefix = (fileFilter ? fileFilter + '/' : '') + (prefix || '');
  100. return (
  101. <>
  102. <tr>
  103. <td
  104. className={
  105. 'file ' +
  106. getWorstMetricClassForPercent(metricsToShow, metrics)
  107. }
  108. >
  109. {/* eslint-disable-line prefer-spread */ Array.apply(null, {
  110. length: tabSize
  111. }).map((nothing, index) => (
  112. <span className="filetab" key={index} />
  113. ))}
  114. <FileCell
  115. file={file}
  116. prefix={prefix}
  117. expandedLines={expandedLines}
  118. setExpandedLines={setExpandedLines}
  119. hasChildren={Boolean(children)}
  120. setFileFilter={setFileFilter}
  121. />
  122. </td>
  123. {metricsToShow.statements && (
  124. <MetricCells metrics={metrics.statements} />
  125. )}
  126. {metricsToShow.branches && (
  127. <MetricCells metrics={metrics.branches} />
  128. )}
  129. {metricsToShow.functions && (
  130. <MetricCells metrics={metrics.functions} />
  131. )}
  132. {metricsToShow.lines && <MetricCells metrics={metrics.lines} />}
  133. </tr>
  134. {children &&
  135. expandedLines.indexOf(prefix + file) >= 0 &&
  136. children.map(child => (
  137. <SummaryTableLine
  138. {...child}
  139. tabSize={tabSize + 2}
  140. key={child.file}
  141. prefix={prefix + file + '/'}
  142. metricsToShow={metricsToShow}
  143. expandedLines={expandedLines}
  144. setExpandedLines={setExpandedLines}
  145. setFileFilter={setFileFilter}
  146. />
  147. ))}
  148. </>
  149. );
  150. };