Skip to content

Commit

Permalink
Merge pull request #733 from aneoconsulting/reorganize-lines-dialog-t…
Browse files Browse the repository at this point in the history
…ests

test: reorganize lines dialog component
  • Loading branch information
GunaDinesh authored Nov 28, 2023
2 parents 124b194 + 7aaa613 commit 6043ba3
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 12 deletions.
124 changes: 124 additions & 0 deletions src/app/dashboard/components/reorganize-lines-dialog.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { TaskStatus } from '@aneoconsultingfr/armonik.api.angular';
import { CdkDragDrop } from '@angular/cdk/drag-drop';
import { TestBed } from '@angular/core/testing';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { Observable, of } from 'rxjs';
import { EditNameLineResult } from '@app/types/dialog';
import { IconsService } from '@services/icons.service';
import { ReorganizeLinesDialogComponent } from './reorganize-lines-dialog.component';
import { Line } from '../types';

describe('ReorganizeLinesDialogComponent', () => {
let component: ReorganizeLinesDialogComponent;

const defaultLines: Line[] = [
{
name: 'line1',
interval: 10,
hideGroupsHeader: false,
filters: [],
taskStatusesGroups: [
{ name: 'Success', color: 'green', statuses: [TaskStatus.TASK_STATUS_COMPLETED, TaskStatus.TASK_STATUS_PROCESSED]},
{ name: 'Running', color: 'yellow', statuses: [TaskStatus.TASK_STATUS_CREATING, TaskStatus.TASK_STATUS_PROCESSING]},
{ name: 'Error', color: 'red', statuses: [TaskStatus.TASK_STATUS_CANCELLED, TaskStatus.TASK_STATUS_TIMEOUT]}
],
},
{
name: 'line2',
interval: 20,
hideGroupsHeader: true,
filters: [],
taskStatusesGroups: [
{ name: 'Success', color: 'green', statuses: [TaskStatus.TASK_STATUS_COMPLETED, TaskStatus.TASK_STATUS_PROCESSED]},
{ name: 'Running', color: 'yellow', statuses: [TaskStatus.TASK_STATUS_CREATING, TaskStatus.TASK_STATUS_PROCESSING]},
{ name: 'Unspecified', color: 'grey', statuses: [TaskStatus.TASK_STATUS_UNSPECIFIED, TaskStatus.TASK_STATUS_RETRIED]}
],
}
];

let dialogRef$: Observable<EditNameLineResult>;
const mockMatDialog = {
open: jest.fn(() => {
return {
afterClosed() {
return dialogRef$;
}
};
})
};

const mockMatDialogRef = {
close: jest.fn()
};

beforeEach(() => {
component = TestBed.configureTestingModule({
providers: [
ReorganizeLinesDialogComponent,
IconsService,
{ provide: MatDialogRef, useValue: mockMatDialogRef },
{ provide: MAT_DIALOG_DATA, useValue: {
lines: structuredClone(defaultLines)
}},
{ provide: MatDialog, useValue: mockMatDialog }
]
}).inject(ReorganizeLinesDialogComponent);
component.ngOnInit();
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should init', () => {
expect(component.lines).toEqual(defaultLines);
});

it('should get required icons', () => {
expect(component.getIcon('drag')).toEqual('drag_indicator');
expect(component.getIcon('edit')).toEqual('edit');
expect(component.getIcon('delete')).toEqual('delete');
});

it('should close', () => {
component.onNoClick();
expect(mockMatDialogRef.close).toHaveBeenCalled();
});

it('should change array on drop', () => {
const event = {
previousIndex: 0,
currentIndex: 1,
} as unknown as CdkDragDrop<Line[]>;
component.onDrop(event);
expect(component.lines).toEqual(structuredClone(defaultLines).reverse());
});

it('should delete a line', () => {
component.onDeleteLine(component.lines[0]);
expect(component.lines).toEqual(
[{
name: 'line2',
interval: 20,
hideGroupsHeader: true,
filters: [],
taskStatusesGroups: [
{ name: 'Success', color: 'green', statuses: [TaskStatus.TASK_STATUS_COMPLETED, TaskStatus.TASK_STATUS_PROCESSED]},
{ name: 'Running', color: 'yellow', statuses: [TaskStatus.TASK_STATUS_CREATING, TaskStatus.TASK_STATUS_PROCESSING]},
{ name: 'Unspecified', color: 'grey', statuses: [TaskStatus.TASK_STATUS_UNSPECIFIED, TaskStatus.TASK_STATUS_RETRIED]}
],
}]
);
});

it('should edit a name line', () => {
const newName = {name: 'newLineName'};
dialogRef$ = of(newName);
component.onEditNameLine(component.lines[1], 1);
expect(component.lines[1].name).toEqual('newLineName');
});

it('should track by line', () => {
expect(component.trackByLine(0, component.lines[0])).toEqual('line10');
});
});
25 changes: 13 additions & 12 deletions src/app/dashboard/components/reorganize-lines-dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Line } from '../types';
<mat-dialog-content>
<p i18n="Dialog description">Drag and drop lines to update the order</p>
<div class="lines" cdkDropList (cdkDropListDropped)="drop($event)">
<div class="lines" cdkDropList (cdkDropListDropped)="onDrop($event)">
<div class="line" *ngFor="let line of lines; let index = index" cdkDrag>
<div class="line-name">
<mat-icon mat-icon aria-hidden="true" i18n-aria-label aria-label="Drag status" [fontIcon]="getIcon('drag')"></mat-icon>
Expand Down Expand Up @@ -126,11 +126,11 @@ export class ReorganizeLinesDialogComponent implements OnInit {
this.#dialogRef.close();
}

drop(event: CdkDragDrop<Line[]>) {
onDrop(event: CdkDragDrop<Line[]>) {
moveItemInArray(this.lines, event.previousIndex, event.currentIndex);
}

onDeleteLine( line: Line) {
onDeleteLine(line: Line) {
const index = this.lines.indexOf(line);
if (index > -1) {
this.lines.splice(index, 1);
Expand All @@ -145,17 +145,18 @@ export class ReorganizeLinesDialogComponent implements OnInit {
});

dialogRef.afterClosed().subscribe((result) => {
if (!result) return;
const selectedLine = this.lines[index];
const changeSelectedNameLine = (line: Line, newName: string): void => {
if(line.name === newName) {
line.name = result.name;
}
};
this.lines.map(line => changeSelectedNameLine(line, selectedLine.name));
if (result) {
const selectedLine = this.lines[index];
const changeSelectedNameLine = (line: Line, oldName: string): void => {
if(line.name === oldName) {
line.name = result.name;
}
};
this.lines.map(line => changeSelectedNameLine(line, selectedLine.name));
}
});

}

trackByLine(index: number, line: Line): string {
return line.name + index;
}
Expand Down

1 comment on commit 6043ba3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 92%
93.04% (1713/1841) 84.03% (321/382) 88.75% (434/489)

JUnit

Tests Skipped Failures Errors Time
609 0 💤 32 ❌ 0 🔥 45.665s ⏱️
Files coverage (92%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files93.0484.0388.7592.86 
applications51.4409.3751.82 
   index.component.ts51.4409.3751.82251–307, 315–456
applications/services97.180.9591.6696.96 
   applications-filters.service.ts100100100100 
   applications-grpc.service.ts10066.6610010068–69
   applications-index.service.ts93.18086.6692.8571–75
components98.7598.2495.9199 
   actions-toolbar-group.component.ts100100100100 
   actions-toolbar.component.ts100100100100 
   auto-refresh-button.component.ts100100100100 
   auto-refresh-dialog.component.ts100100100100 
   columns-button.component.ts100100100100 
   columns-modify-dialog.component.ts97.1410093.75100 
   count-tasks-by-status.component.ts1005010010048
   page-header.component.ts8010008039
   page-section-header.component.ts8010008031
   page-section.component.ts100100100100 
   refresh-button.component.ts100100100100 
   share-url.component.ts92.851007592.337
   show-card-content.component.ts100100100100 
   spinner.component.ts100100100100 
   table-actions-toolbar.component.ts100100100100 
   table-container.component.ts100100100100 
   view-tasks-by-status-dialog.component.ts100100100100 
   view-tasks-by-status.component.ts100100100100 
components/filters9578.9495.7494.89 
   filters-chips.component.ts100100100100 
   filters-dialog-and.component.ts100100100100 
   filters-dialog-filter-field.component.ts86.275.7592.385.9699–100, 132, 174, 198–202
   filters-dialog-input.component.ts92.8563.6387.592.5973–74
   filters-dialog-or.component.ts100100100100 
   filters-dialog.component.ts100100100100 
   filters-toolbar.component.ts100100100100 
components/navigation99.4410097.3699.42 
   add-external-service-dialog.component.ts100100100100 
   edit-external-service-dialog.component.ts100100100100 
   form-external-service.component.ts100100100100 
   manage-external-services-dialog.component.ts100100100100 
   navigation.component.ts98.1410088.8898.11215
   theme-selector.component.ts100100100100 
components/table100100100100 
   table-empty-data.component.ts100100100100 
dashboard100100100100 
   index.component.ts100100100100 
dashboard/components97.3397.2295.0697.29 
   add-line-dialog.component.ts100100100100 
   add-statuses-group-dialog.component.ts100100100100 
   edit-name-line-dialog.component.ts100100100100 
   edit-status-group-dialog.component.ts100100100100 
   form-name-line.component.ts100100100100 
   form-statuses-group.component.ts100100100100 
   line.component.ts100100100100 
   manage-groups-dialog.component.ts100100100100 
   reorganize-lines-dialog.component.ts100100100100 
   split-lines-dialog.component.ts42.850042.8538–61
   statuses-group-card.component.ts100100100100 
dashboard/services100100100100 
   dashboard-index.service.ts100100100100 
   dashboard-storage.service.ts100100100100 
directives50100050 
   no-wrap.directive.ts501000505–6
pipes500040 
   empty-cell.pipe.ts5000406–10
services98.3695.4195.6998.24 
   auto-refresh.service.ts100100100100 
   date-handler.service.ts100100100100 
   default-config.service.ts100100100100 
   environment.service.ts80100507519
   filters.service.ts100100100100 
   icons.service.ts100100100100 
   navigation.service.ts10080100100109
   notification.service.ts100100100100 
   query-params.service.ts100100100100 
   share-url.service.ts100100100100 
   storage.service.ts98.0310010097.9595
   table-storage.service.ts500042.8511–31
   table-url.service.ts100100100100 
   table.service.ts100100100100 
   tasks-by-status.service.ts100100100100 
   user-grpc.service.ts100100100100 
   user.service.ts100100100100 
   utils.service.ts100100100100 
   versions-grpc.service.ts100100100100 
   versions.service.ts1007010010014, 25, 32
sessions/services93.336087.592.85 
   sessions-filters.service.ts95.836010095.65145
   sessions-statuses.service.ts83.33100508013
tasks/services77.455083.7876.53 
   tasks-filters.service.ts100100100100 
   tasks-grpc.service.ts23.330020.6813–151
   tasks-index.service.ts100100100100 
   tasks-statuses.service.ts100100100100 
tokens100100100100 
   filters.token.ts100100100100 
types100100100100 
   filter-definition.ts100100100100 

Please sign in to comment.