1import {
2  Component,
3  Input,
4  OnChanges,
5  AfterViewInit,
6  ViewChild,
7  SimpleChanges,
8} from '@angular/core';
9import { MatListModule, MatSelectionListChange } from '@angular/material/list';
10
11import { MotionGolden } from '../golden';
12import { MotionGoldenComponent } from '../motion-golden/motion-golden.component';
13import { MatDividerModule } from '@angular/material/divider';
14
15import {
16  MatPaginator,
17  MatPaginatorModule,
18  PageEvent,
19} from '@angular/material/paginator';
20import { MatSort, MatSortModule } from '@angular/material/sort';
21import {
22  MatTableDataSource,
23  MatTable,
24  MatTableModule,
25} from '@angular/material/table';
26import { MatRadioModule } from '@angular/material/radio';
27import { MatButtonModule } from '@angular/material/button';
28import { MatIconModule } from '@angular/material/icon';
29
30@Component({
31  selector: 'app-test-overview',
32  standalone: true,
33  imports: [
34    MatListModule,
35    MotionGoldenComponent,
36    MatDividerModule,
37    MatTableModule,
38    MatRadioModule,
39    MatSortModule,
40    MatIconModule,
41    MatButtonModule,
42    MatPaginatorModule,
43  ],
44  templateUrl: './test-overview.component.html',
45  styleUrl: './test-overview.component.scss',
46})
47export class TestOverviewComponent implements OnChanges, AfterViewInit {
48  dataSource: MatTableDataSource<MotionGolden> = new MatTableDataSource();
49  displayedColumns: string[] = [
50    'select',
51    'status',
52    'golden',
53    'testClassName',
54    'testMethodName',
55    'testTime',
56  ];
57
58  constructor() {
59    this.dataSource.sortingDataAccessor = (data, sortHeaderIdstring) => {
60      switch (sortHeaderIdstring) {
61        case 'status':
62          return data.result;
63        case 'testClassName':
64          return data.testClassName;
65        case 'testMethodName':
66          return data.testMethodName;
67        case 'testTime':
68          return Date.parse(data.testTime);
69      }
70      return 0;
71    };
72  }
73
74  @Input() goldens: MotionGolden[] = [];
75
76  totalTestCount = 0;
77  passingTestCount = 0;
78  failingTestCount = 0;
79
80  ngOnChanges(changes: SimpleChanges): void {
81    if (changes['goldens']) {
82      this.totalTestCount = this.goldens.length;
83      this.failingTestCount = this.goldens.filter(
84        (golden) => golden.result !== 'PASSED',
85      ).length;
86      this.passingTestCount = this.totalTestCount - this.failingTestCount;
87      this.dataSource.data = this.goldens;
88    }
89  }
90  @ViewChild(MatPaginator) paginator!: MatPaginator;
91  @ViewChild(MatSort) sort!: MatSort;
92  get selectedGolden() {
93    return this.goldens.find((it) => it.id == this.selectedGoldenId);
94  }
95
96  ngAfterViewInit() {
97    this.dataSource.paginator = this.paginator;
98    this.dataSource.sort = this.sort;
99  }
100
101  get testStatusClass() {
102    if (this.failingTestCount > 0) {
103      return 'failing';
104    } else if (this.passingTestCount === this.totalTestCount) {
105      return 'passing';
106    } else {
107      return 'no-status';
108    }
109  }
110
111  selectedGoldenId: string | null = null;
112
113  goldenSelected(golden: MotionGolden) {
114    this.selectedGoldenId =
115      golden.id !== this.selectedGoldenId ? golden.id : null;
116  }
117}
118