All files / ee/app/assets/javascripts/analytics/repository_analytics/components test_coverage_table.vue

70.58% Statements 24/34
71.42% Branches 5/7
78.26% Functions 18/23
70.58% Lines 24/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                                        2x                                                           4x           4x     4x                           4x           8x     11x         7x                     14x       11x               11x 4x     7x         14x 8x     6x       4x 1x 1x                     5x           4x   4x 4x 4x                                                                                                                                                                                                                                                                                                          
<script>
import emptyStateIllustrationUrl from '@gitlab/svgs/dist/illustrations/empty-state/empty-search-md.svg?url';
import {
  GlCard,
  GlEmptyState,
  GlLink,
  GlSkeletonLoader,
  GlTableLite,
  GlIcon,
  GlPopover,
  GlSprintf,
} from '@gitlab/ui';
import api from '~/api';
import { SUPPORTED_FORMATS, getFormatter } from '~/lib/utils/unit_format';
import { joinPaths } from '~/lib/utils/url_utility';
import { __, s__ } from '~/locale';
import TimeAgoTooltip from '~/vue_shared/components/time_ago_tooltip.vue';
import { tablei18n as i18n } from '../constants';
import getProjectsTestCoverage from '../graphql/queries/get_projects_test_coverage.query.graphql';
import SelectProjectsDropdown from './select_projects_dropdown.vue';
import DownloadTestCoverage from './download_test_coverage.vue';
 
export default {
  name: 'TestCoverageTable',
  components: {
    GlCard,
    GlEmptyState,
    GlLink,
    GlSkeletonLoader,
    GlTableLite,
    SelectProjectsDropdown,
    TimeAgoTooltip,
    DownloadTestCoverage,
    GlIcon,
    GlPopover,
    GlSprintf,
  },
  inject: {
    groupFullPath: {
      default: '',
    },
    groupName: {
      default: '',
    },
  },
  apollo: {
    projects: {
      query: getProjectsTestCoverage,
      debounce: 500,
      variables() {
        return {
          groupFullPath: this.groupFullPath,
          projectIds: this.projectIdsToFetch,
        };
      },
      result({ data }) {
        const projects = data.group.projects.nodes;
        // Keep data from all queries so that we don't
        // fetch the same data more than once
        this.allCoverageData = [
          ...this.allCoverageData,
          ...projects
            .filter(({ id }) => !this.allCoverageData.some((project) => project.id === id))
            .map((project) => ({
              ...project,
              codeCoveragePath: joinPaths(
                gon.relative_url_root || '',
                `/${project.fullPath}/-/graphs/${project.repository.rootRef}/charts`,
              ),
            })),
        ];
      },
      update(data) {
        return data.group.projects.nodes;
      },
      error() {
        this.handleError();
      },
      watchLoading(isLoading) {
        this.isLoading = isLoading;
      },
      skip() {
        return this.skipQuery;
      },
    },
  },
  data() {
    return {
      allProjectsSelected: true,
      allCoverageData: [], // All data we have ever received whether selected or not
      hasError: false,
      isLoading: false,
      selectedProjectIds: {},
      projects: {},
    };
  },
  computed: {
    hasCoverageData() {
      return Boolean(this.selectedCoverageData.length);
    },
    skipQuery() {
      // Skip if we haven't selected any projects yet
      return !this.allProjectsSelected && !this.projectIdsToFetch.length;
    },
    /**
     * projectIdsToFetch is a subset of selectedProjectIds
     * The difference is that it only returns the projects
     * that we have selected but haven't requested yet
     */
    projectIdsToFetch() {
      if (this.allProjectsSelected) {
        return null;
      }
      // Get the IDs of the projects that we haven't requested yet
      return Object.keys(this.selectedProjectIds).filter(
        (id) => !this.allCoverageData.some((project) => project.id === id),
      );
    },
    selectedCoverageData() {
      if (this.allProjectsSelected) {
        return this.allCoverageData;
      }
 
      return this.allCoverageData.filter(({ id }) => this.selectedProjectIds[id]);
    },
    sortedCoverageData() {
      // Sort the table by most recently updated coverage report
      return [...this.selectedCoverageData].sort((a, b) => {
        if (a.codeCoverageSummary.lastUpdatedOn > b.codeCoverageSummary.lastUpdatedOn) {
          return -1;
        }
        Iif (a.codeCoverageSummary.lastUpdatedOn < b.codeCoverageSummary.lastUpdatedOn) {
          return 1;
        }
        return 0;
      });
    },
  },
  methods: {
    handleError() {
      this.hasError = true;
    },
    onProjectClick() {
      api.trackRedisHllUserEvent(this.$options.servicePingProjectEvent);
    },
    selectAllProjects(ids) {
      this.allProjectsSelected = true;
 
      this.selectedProjectIds = ids.reduce((acc, key) => {
        acc[key] = true;
        return acc;
      }, {});
    },
    toggleProject(ids) {
      Iif (this.allProjectsSelected) {
        this.allProjectsSelected = false;
      }
 
      this.selectedProjectIds = ids.reduce((acc, key) => {
        acc[key] = true;
        return acc;
      }, {});
    },
  },
  tableFields: [
    {
      key: 'project',
      label: __('Project'),
    },
    {
      key: 'averageCoverage',
      label: s__('RepositoriesAnalytics|Coverage'),
    },
    {
      key: 'coverageCount',
      label: s__('RepositoriesAnalytics|Coverage Jobs'),
    },
    {
      key: 'lastUpdatedOn',
      label: s__('RepositoriesAnalytics|Last Update'),
    },
  ],
  i18n,
  emptyStateIllustrationUrl,
  LOADING_STATE: {
    rows: 4,
    height: 10,
    rx: 4,
    groupXs: [0, 95, 180, 330],
    widths: [90, 80, 145, 100],
    totalWidth: 430,
    totalHeight: 15,
  },
  averageCoverageFormatter: getFormatter(SUPPORTED_FORMATS.percentHundred),
  servicePingProjectEvent: 'i_testing_group_code_coverage_project_click_total',
};
</script>
<template>
  <gl-card>
    <template #header>
      <div class="gl-display-flex gl-flex-wrap gl-align-items-center gl-justify-content-end">
        <div class="gl-flex-grow-1">
          <h5 class="gl-display-inline-block">{{ $options.i18n.header }}</h5>
          <gl-icon
            id="latest-coverage-help-icon"
            name="question-o"
            class="gl-text-blue-600 gl-cursor-help"
          />
          <gl-popover target="latest-coverage-help-icon" :title="$options.i18n.header">
            <gl-sprintf :message="$options.i18n.popover">
              <template #groupName>{{ groupName }}</template>
            </gl-sprintf>
          </gl-popover>
        </div>
        <select-projects-dropdown
          class="gl-w-full gl-sm-w-auto gl-mb-3 gl-sm-mb-0"
          placement="right"
          @projects-query-error="handleError"
          @select-all-projects="selectAllProjects"
          @select-project="toggleProject"
        />
        <download-test-coverage />
      </div>
    </template>
 
    <template v-if="isLoading">
      <gl-skeleton-loader
        v-for="index in $options.LOADING_STATE.rows"
        :key="index"
        :width="$options.LOADING_STATE.totalWidth"
        :height="$options.LOADING_STATE.totalHeight"
        data-testid="test-coverage-loading-state"
      >
        <rect
          v-for="(x, xIndex) in $options.LOADING_STATE.groupXs"
          :key="`x-skeleton-${x}`"
          :width="$options.LOADING_STATE.widths[xIndex]"
          :height="$options.LOADING_STATE.height"
          :x="x"
          :y="0"
          :rx="$options.LOADING_STATE.rx"
        />
      </gl-skeleton-loader>
    </template>
 
    <gl-table-lite
      v-else-if="hasCoverageData"
      :fields="$options.tableFields"
      :items="sortedCoverageData"
    >
      <template #head(project)="data">
        <div>{{ data.label }}</div>
      </template>
      <template #head(averageCoverage)="data">
        <div>{{ data.label }}</div>
      </template>
      <template #head(coverageCount)="data">
        <div>{{ data.label }}</div>
      </template>
      <template #head(lastUpdatedOn)="data">
        <div>{{ data.label }}</div>
      </template>
 
      <template #cell(project)="{ item }">
        <gl-link
          target="_blank"
          :href="item.codeCoveragePath"
          :data-testid="`${item.id}-name`"
          @click.once="onProjectClick"
        >
          {{ item.name }}
        </gl-link>
      </template>
      <template #cell(averageCoverage)="{ item }">
        <div :data-testid="`${item.id}-average`">
          {{ $options.averageCoverageFormatter(item.codeCoverageSummary.averageCoverage, 2) }}
        </div>
      </template>
      <template #cell(coverageCount)="{ item }">
        <div :data-testid="`${item.id}-count`">{{ item.codeCoverageSummary.coverageCount }}</div>
      </template>
      <template #cell(lastUpdatedOn)="{ item }">
        <time-ago-tooltip
          :time="item.codeCoverageSummary.lastUpdatedOn"
          :data-testid="`${item.id}-date`"
        />
      </template>
    </gl-table-lite>
 
    <gl-empty-state
      v-else
      class="gl-mt-3"
      :title="$options.i18n.emptyStateTitle"
      :description="$options.i18n.emptyStateDescription"
      :svg-path="$options.emptyStateIllustrationUrl"
      data-testid="test-coverage-table-empty-state"
    />
  </gl-card>
</template>