test: add test for dialog component

This commit is contained in:
2025-02-11 16:45:10 +01:00
parent c53f6f93b8
commit 6fc1e0ec58

View File

@ -7,11 +7,17 @@ import {
MatDialogRef,
} from '@angular/material/dialog';
import { MatCheckboxChange } from '@angular/material/checkbox';
describe('ColumnSelectionDialogComponent', () => {
let component: ColumnSelectionDialogComponent;
let fixture: ComponentFixture<ColumnSelectionDialogComponent>;
let dialogRefSpy: jasmine.SpyObj<MatDialogRef<ColumnSelectionDialogComponent>>;
beforeEach(async () => {
dialogRefSpy = jasmine.createSpyObj<MatDialogRef<ColumnSelectionDialogComponent>>(['close']);
await TestBed.configureTestingModule({
providers: [
{
@ -23,7 +29,7 @@ describe('ColumnSelectionDialogComponent', () => {
},
{
provide: MatDialogRef,
useValue: {},
useValue: dialogRefSpy,
},
],
imports: [ColumnSelectionDialogComponent, MatDialogModule],
@ -37,4 +43,33 @@ describe('ColumnSelectionDialogComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('should update the selected columns', () => {
component.columns = [
{ name: 'column1', selected: true },
{ name: 'column2', selected: false },
];
let checkbox_changed = new MatCheckboxChange();
checkbox_changed.checked = true;
component.handleCheckboxChecked(checkbox_changed, 1);
expect(component.columns).toEqual([
{ name: 'column1', selected: true },
{ name: 'column2', selected: true },
]);
});
it('should close dialog on cancel, no data', () => {
component.onCancelClick();
expect(dialogRefSpy.close).toHaveBeenCalledWith(null);
});
it('should close dialog on apply, with data', () => {
component.columns = [
{ name: 'column1', selected: true },
{ name: 'column2', selected: false },
];
component.onApplyClick();
expect(dialogRefSpy.close).toHaveBeenCalledWith(['column1']);
});
});