|
| 1 | +import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {FilesTableComponent} from './filesTable.component'; |
| 3 | +import {MatIconModule} from '@angular/material/icon'; |
| 4 | +import {MatTableModule} from '@angular/material/table'; |
| 5 | +import {DebugElement} from '@angular/core'; |
| 6 | +import {MaterialHarnesses} from '../../../test-helpers/MaterialHarnesses'; |
| 7 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 8 | +import {expect} from '@angular/flex-layout/_private-utils/testing'; |
| 9 | +import {ConfigurationService} from 'ddp-sdk'; |
| 10 | +import {By} from '@angular/platform-browser'; |
| 11 | +import {HttpRequestStatusEnum} from '../../enums/httpRequestStatus-enum'; |
| 12 | +import {SomaticResultsFileVirusStatusEnum} from '../../enums/somaticResultsFileVirusStatus-enum'; |
| 13 | +import {MatTooltipModule} from '@angular/material/tooltip'; |
| 14 | +import {MatProgressSpinnerModule} from '@angular/material/progress-spinner'; |
| 15 | +import {DatePipe} from '@angular/common'; |
| 16 | +import { cloneDeep } from 'lodash'; |
| 17 | +import {SomaticResultsFileWithStatus} from '../../interfaces/somaticResultsFile'; |
| 18 | + |
| 19 | +const testDocuments: any = [ |
| 20 | + // Clean, Sent File |
| 21 | + { |
| 22 | + somaticDocumentId: 111, |
| 23 | + fileName: 'testFileSuccessSent.pdf', |
| 24 | + createdAt: 1688476158, |
| 25 | + sentAt: 1688476500, |
| 26 | + virusStatus: SomaticResultsFileVirusStatusEnum.CLEAN, |
| 27 | + sendToParticipantStatus: { |
| 28 | + status: HttpRequestStatusEnum.NONE, |
| 29 | + message: null |
| 30 | + }, |
| 31 | + deleteStatus: { |
| 32 | + status: HttpRequestStatusEnum.NONE, |
| 33 | + message: null |
| 34 | + } |
| 35 | + } |
| 36 | +]; |
| 37 | + |
| 38 | +enum DocumentIconsEnum { |
| 39 | + CLEAN = 'check_circle_outline', |
| 40 | + INFECTED = 'bug_report', |
| 41 | + SPINNING = 'SPINNING', |
| 42 | + UNABLE_TO_SCAN = 'error_outline', |
| 43 | + RETRY = 'replay', |
| 44 | + FAIL = 'error', |
| 45 | + SUCCESS = 'check_circle', |
| 46 | + SEND = 'send', |
| 47 | + DELETE = 'delete_forever' |
| 48 | +} |
| 49 | + |
| 50 | +enum ColumnNameEnum { |
| 51 | + STATUS = 'VirusStatus', |
| 52 | + FILE = 'Name', |
| 53 | + DATE_UPLOADED = 'UploadDate', |
| 54 | + SEND_TO_PARTICIPANT = 'SendToParticipant', |
| 55 | + DATE_SENT = 'SentDate', |
| 56 | + DELETE = 'Delete' |
| 57 | +} |
| 58 | + |
| 59 | +describe('FilesTableComponent', () => { |
| 60 | + let fixture: ComponentFixture<FilesTableComponent>; |
| 61 | + let component: FilesTableComponent; |
| 62 | + let componentHTML: DebugElement; |
| 63 | + let materialHarnessLoader: MaterialHarnesses; |
| 64 | + |
| 65 | + const dateFormat = 'MM/dd/YYYY h:mm:ss aa'; |
| 66 | + const datePipe: DatePipe = new DatePipe('en-US'); |
| 67 | + |
| 68 | + beforeEach(waitForAsync(() => { |
| 69 | + const sdkConfig = new ConfigurationService(); |
| 70 | + |
| 71 | + TestBed.configureTestingModule({ |
| 72 | + declarations: [FilesTableComponent, DatePipe], |
| 73 | + imports: [MatIconModule, MatTableModule, MatTooltipModule, MatProgressSpinnerModule], |
| 74 | + providers: [{ |
| 75 | + provide: 'ddp.config', |
| 76 | + useValue: sdkConfig |
| 77 | + }] |
| 78 | + }).compileComponents(); |
| 79 | + })); |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + fixture = TestBed.createComponent(FilesTableComponent); |
| 83 | + component = fixture.debugElement.componentInstance; |
| 84 | + componentHTML = fixture.debugElement; |
| 85 | + materialHarnessLoader = new MaterialHarnesses(TestbedHarnessEnvironment.loader(fixture)); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should create component', () => { |
| 89 | + expect(component).toBeTruthy('Component has not been instantiated'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should display No files found', () => { |
| 93 | + fixture.detectChanges(); |
| 94 | + const noFilesNote = componentHTML.query(By.css('.noFilesNote p')); |
| 95 | + expect(noFilesNote.nativeElement.textContent) |
| 96 | + .toEqual('No files were found', 'No files found note is not displayed'); |
| 97 | + }); |
| 98 | + |
| 99 | + /* Text data */ |
| 100 | + |
| 101 | + it('should display correct file name',async () => { |
| 102 | + component.somaticResultsFiles = testDocuments; |
| 103 | + fixture.detectChanges(); |
| 104 | + const fileName = await getDocumentCellData(0, ColumnNameEnum.FILE); |
| 105 | + expect(fileName).toEqual('testFileSuccessSent.pdf', 'Wrong file name'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should display uploaded date in correct format', async () => { |
| 109 | + component.somaticResultsFiles = testDocuments; |
| 110 | + fixture.detectChanges(); |
| 111 | + const dateUploaded = await getDocumentCellData(0, ColumnNameEnum.DATE_UPLOADED); |
| 112 | + expect(dateUploaded).toEqual(formatDate(1688476158), 'Wrong created at date format'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should display sent date in correct format', async () => { |
| 116 | + component.somaticResultsFiles = testDocuments; |
| 117 | + fixture.detectChanges(); |
| 118 | + const sentDate = await getDocumentCellData(0, ColumnNameEnum.DATE_SENT); |
| 119 | + expect(sentDate).toEqual(formatDate(1688476500), 'Wrong sent date format'); |
| 120 | + }); |
| 121 | + |
| 122 | + /* Status icons */ |
| 123 | + |
| 124 | + it('should display clean status file', async () => { |
| 125 | + component.somaticResultsFiles = testDocuments; |
| 126 | + fixture.detectChanges(); |
| 127 | + const status = await getDocumentCellData(0, ColumnNameEnum.STATUS); |
| 128 | + expect(status).toEqual(DocumentIconsEnum.CLEAN, 'Wrong file status'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should display send icon',async () => { |
| 132 | + component.somaticResultsFiles = testDocuments; |
| 133 | + fixture.detectChanges(); |
| 134 | + const sendToParticipant = await getDocumentCellData(0, ColumnNameEnum.SEND_TO_PARTICIPANT); |
| 135 | + expect(sendToParticipant).toEqual(DocumentIconsEnum.SEND, 'Wrong send to participant icon'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should display delete icon',async () => { |
| 139 | + component.somaticResultsFiles = testDocuments; |
| 140 | + fixture.detectChanges(); |
| 141 | + const deleteIcon = await getDocumentCellData(0, ColumnNameEnum.DELETE); |
| 142 | + expect(deleteIcon).toEqual(DocumentIconsEnum.DELETE, 'Wrong delete icon'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should display infected status',async () => { |
| 146 | + const infectedDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 147 | + infectedDocument.virusStatus = SomaticResultsFileVirusStatusEnum.INFECTED; |
| 148 | + component.somaticResultsFiles = [infectedDocument]; |
| 149 | + fixture.detectChanges(); |
| 150 | + const status = await getDocumentCellData(0, ColumnNameEnum.STATUS); |
| 151 | + expect(status).toEqual(DocumentIconsEnum.INFECTED, 'Wrong virus status'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should display scanning status',async () => { |
| 155 | + const scanningDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 156 | + scanningDocument.virusStatus = SomaticResultsFileVirusStatusEnum.SCANNING; |
| 157 | + component.somaticResultsFiles = [scanningDocument]; |
| 158 | + fixture.detectChanges(); |
| 159 | + const status = await getDocumentCellData(0, ColumnNameEnum.STATUS); |
| 160 | + expect(status).toEqual(DocumentIconsEnum.SPINNING, 'Wrong virus status'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should display unable to scan status',async () => { |
| 164 | + const unableToScanDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 165 | + unableToScanDocument.virusStatus = SomaticResultsFileVirusStatusEnum.UNABLE_TO_SCAN; |
| 166 | + component.somaticResultsFiles = [unableToScanDocument]; |
| 167 | + fixture.detectChanges(); |
| 168 | + const status = await getDocumentCellData(0, ColumnNameEnum.STATUS); |
| 169 | + expect(status).toEqual(DocumentIconsEnum.UNABLE_TO_SCAN, 'Wrong virus status'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should display send to participant SUCCESS',async () => { |
| 173 | + const sendToParticipantSuccessDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 174 | + sendToParticipantSuccessDocument.sendToParticipantStatus.status = HttpRequestStatusEnum.SUCCESS; |
| 175 | + component.somaticResultsFiles = [sendToParticipantSuccessDocument]; |
| 176 | + fixture.detectChanges(); |
| 177 | + const status = await getDocumentCellData(0, ColumnNameEnum.SEND_TO_PARTICIPANT); |
| 178 | + expect(status).toEqual(DocumentIconsEnum.SUCCESS, 'Wrong send to participant status'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should display send to participant FAIL',async () => { |
| 182 | + const sendToParticipantFailDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 183 | + sendToParticipantFailDocument.sendToParticipantStatus.status = HttpRequestStatusEnum.FAIL; |
| 184 | + component.somaticResultsFiles = [sendToParticipantFailDocument]; |
| 185 | + fixture.detectChanges(); |
| 186 | + const status = await getDocumentCellData(0, ColumnNameEnum.SEND_TO_PARTICIPANT); |
| 187 | + expect(status).toEqual(DocumentIconsEnum.FAIL, 'Wrong send to participant status'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should display send to participant IN PROGRESS',async () => { |
| 191 | + const sendToParticipantInProgressDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 192 | + sendToParticipantInProgressDocument.sendToParticipantStatus.status = HttpRequestStatusEnum.IN_PROGRESS; |
| 193 | + component.somaticResultsFiles = [sendToParticipantInProgressDocument]; |
| 194 | + fixture.detectChanges(); |
| 195 | + const status = await getDocumentCellData(0, ColumnNameEnum.SEND_TO_PARTICIPANT); |
| 196 | + expect(status).toEqual(DocumentIconsEnum.SPINNING, 'Wrong send to participant status'); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should display delete IN PROGRESS',async () => { |
| 200 | + const deleteInProgressDocument: SomaticResultsFileWithStatus = cloneDeep(testDocuments[0]); |
| 201 | + deleteInProgressDocument.deleteStatus.status = HttpRequestStatusEnum.IN_PROGRESS; |
| 202 | + component.somaticResultsFiles = [deleteInProgressDocument]; |
| 203 | + fixture.detectChanges(); |
| 204 | + const status = await getDocumentCellData(0, ColumnNameEnum.DELETE); |
| 205 | + expect(status).toEqual(DocumentIconsEnum.SPINNING, 'Wrong delete status'); |
| 206 | + }); |
| 207 | + |
| 208 | + |
| 209 | + /* HELPER FUNCTIONS */ |
| 210 | + |
| 211 | + const getDocumentCellData = async (rowIndex: number, columName: ColumnNameEnum): Promise<DocumentIconsEnum> => { |
| 212 | + const matTableHarness = await materialHarnessLoader.getMatTableHarness(); |
| 213 | + const rows = await matTableHarness.getRows(); |
| 214 | + // the status information cell |
| 215 | + const cells = await rows[rowIndex].getCells(); |
| 216 | + // cell data |
| 217 | + let cellData = ''; |
| 218 | + |
| 219 | + // looking for cell data for the provided column |
| 220 | + for(const cell of cells) { |
| 221 | + if(await cell.getColumnName() === columName) { |
| 222 | + cellData = await cell.getText(); |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + // returns either matIcon text or SCANNING (matSpinner doesn't have text) |
| 228 | + return (cellData || 'SPINNING') as DocumentIconsEnum; |
| 229 | + }; |
| 230 | + |
| 231 | + const formatDate = (milliseconds: number): string => |
| 232 | + datePipe.transform(milliseconds, dateFormat); |
| 233 | +}); |
0 commit comments