Skip to content

Commit

Permalink
Merge pull request #646 from aneoconsulting/tests-columns-modify-dial…
Browse files Browse the repository at this point in the history
…og-component

chore: tests for columns-modify-dialog component and some refactor
  • Loading branch information
GunaDinesh authored Nov 28, 2023
2 parents 0970360 + fd26193 commit da6eec4
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
117 changes: 117 additions & 0 deletions src/app/components/columns-modify-dialog.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatCheckboxChange } from '@angular/material/checkbox';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ColumnKey } from '@app/types/data';
import { ColumnsModifyDialogComponent } from './columns-modify-dialog.component';

describe('', () => {
let component: ColumnsModifyDialogComponent<object, object>;
let fixture: ComponentFixture<ColumnsModifyDialogComponent<object, object>>;
const mockMatDialogRef = {
close: jest.fn()
};
const mockMatDialogData = {
currentColumns: ['id', 'created_time'],
columnsLabels: {
'id': 'Id',
'created_time': 'Creation time',
'name': 'Name',
'duration': 'Duration',
'options.task_id': 'Task Id',
'actions': 'Actions'
} as Record<ColumnKey<object, object>, string>,
availableColumns: ['name', 'duration', 'options.task_id', 'actions']
};

beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [
ColumnsModifyDialogComponent,
{ provide: MatDialogRef, useValue: mockMatDialogRef },
{ provide: MAT_DIALOG_DATA, useValue: mockMatDialogData }
]
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(ColumnsModifyDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

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

test('optionsColumnValue should add "options." to any provided string', () => {
expect(component.optionsColumnValue('nameColumn')).toEqual('options.nameColumn');
});

describe('columnToLabel', () => {
it('should get the label of the specified column', () => {
expect(component.columnToLabel('name' as ColumnKey<object, object>))
.toEqual('Name');
});

it('should return the stringified key in case of non-existing label', () => {
expect(component.columnToLabel('nonExistingLabel' as ColumnKey<object, object>))
.toEqual('nonExistingLabel');
});
});

test('availableColumns should return every column without the "options." prefix', () => {
expect(component.availableColumns()).toEqual(['actions', 'duration', 'name']);
});

test('availableOptionsColumns should return every column with the "options." prefix', () => {
expect(component.availableOptionsColumns()).toEqual(['options.task_id']);
});

describe('updateColumn', () => {
it('should push a new column', () => {
component.updateColumn({checked: true} as MatCheckboxChange, 'duration' as ColumnKey<object, object>);
expect(component.columns).toEqual(['id', 'created_time', 'duration']);
expect(component.data.availableColumns).toEqual(['name', 'options.task_id', 'actions']);
});

it('should remove a column if parameter is unchecked', () => {
component.updateColumn({checked: false} as MatCheckboxChange, 'id' as ColumnKey<object, object>);
expect(component.columns).toEqual(['created_time']);
expect(component.data.availableColumns).toEqual(['name', 'options.task_id', 'actions', 'id']);
});

it('should not push a column that is already in the column list', () => {
component.updateColumn({checked: true} as MatCheckboxChange, 'id' as ColumnKey<object, object>);
expect(component.columns).toEqual(['id', 'created_time']);
});

it('should not push a column that is not in the available columns', () => {
component.updateColumn({checked: true} as MatCheckboxChange, 'someColumn' as ColumnKey<object, object>);
expect(component.columns).toEqual(['id', 'created_time']);
});

it('should not do remove a column that is not in the column list', () => {
component.updateColumn({checked: false} as MatCheckboxChange, 'someColumn' as ColumnKey<object, object>);
expect(component.columns).toEqual(['id', 'created_time']);
});
});

describe('isSelected', () => {
it('should return true if a column is selected', () => {
expect(component.isSelected('id' as ColumnKey<object, object>)).toBeTruthy();
});

it('should return false if a column is not selected', () => {
expect(component.isSelected('duration' as ColumnKey<object, object>)).toBeFalsy();
});
});

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

test('should return the column key as a string', () => {
expect(component.trackByColumn(0, 'actions')).toEqual('actions');
});
});
14 changes: 11 additions & 3 deletions src/app/components/columns-modify-dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ColumnsModifyDialogComponent<T extends object,O extends object> imp
}

columnToLabel(column: ColumnKey<T, O>): string {
return this.columnsLabels[column];
return this.columnsLabels[column] ?? column.toString();
}

/**
Expand All @@ -99,12 +99,20 @@ export class ColumnsModifyDialogComponent<T extends object,O extends object> imp

/**
* Update the columns array when a checkbox is checked or unchecked
* checked: add the column.
* Unchecked: remove it
*/
updateColumn({ checked }: MatCheckboxChange, column: ColumnKey<T, O>): void {
if (checked) {
this.columns.push(column);
if (!this.columns.includes(column) && this.data.availableColumns.includes(column)) {
this.columns.push(column);
this.data.availableColumns = this.data.availableColumns.filter(currentColumn => currentColumn !== column);
}
} else {
this.columns = this.columns.filter(c => c !== column);
if(this.columns.includes(column)) {
this.columns = this.columns.filter(currentColumn => currentColumn !== column);
this.data.availableColumns.push(column);
}
}
}

Expand Down

1 comment on commit da6eec4

@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: 91%
91.46% (1543/1687) 81.3% (300/369) 85.93% (391/455)

JUnit

Tests Skipped Failures Errors Time
534 0 💤 0 ❌ 0 🔥 41.356s ⏱️
Files coverage (91%)
File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files91.4681.385.9391.27 
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.7498.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 
   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 
dashboard100100100100 
   index.component.ts100100100100 
dashboard/components68.138.8845.6768.24 
   add-line-dialog.component.ts55.55100055.5527–38
   add-statuses-group-dialog.component.ts45.45100045.4529–45
   edit-name-line-dialog.component.ts5010005031–46
   edit-status-group-dialog.component.ts41.66100041.6630–47
   form-name-line.component.ts47.360047.3657–88
   form-statuses-group.component.ts22.50022.582–159
   line.component.ts100100100100 
   manage-groups-dialog.component.ts100100100100 
   reorganize-lines-dialog.component.ts32.430034.28104–160
   split-lines-dialog.component.ts42.850042.8538–61
   statuses-group-card.component.ts100100100100 
dashboard/services100100100100 
   dashboard-index.service.ts100100100100 
   dashboard-storage.service.ts100100100100 
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 

Please sign in to comment.