unit tests

This commit is contained in:
Bastian Wagner
2026-02-13 15:12:54 +01:00
parent ea947caf54
commit 2eafa21baf
20 changed files with 662 additions and 8064 deletions

View File

@@ -1,62 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { HotToastService, provideHotToastConfig } from '@ngxpert/hot-toast';
import { from, map, of } from 'rxjs';
import { GridReadyEvent } from 'ag-grid-community';
import { MatDialog } from '@angular/material/dialog';
import { ArchiveComponent } from './archive.component';
import { ApiService } from '../../../../shared/api.service';
import { MockApiService } from '../../../../../../mocks/services/mock.api.service';
// Mocking the dependencies
jest.mock('@ngxpert/hot-toast', () => ({
HotToastService: jest.fn(),
}));
describe('ArchiveComponent', () => {
let component: ArchiveComponent;
let fixture: ComponentFixture<ArchiveComponent>;
const mockGridReadyEvent: GridReadyEvent = {
api: { setGridOption: jest.fn(), addEventListener: jest.fn() },
columnApi: { someColumnApiMethod: jest.fn() },
type: 'gridReady',
} as any;
const mockHotToastService = {
observe: jest.fn().mockImplementation(() => ({
loading: 'speichern...',
success: 'Änderungen gespeichert',
error: 'Änderungen konnten nicht gespeichert werden!',
subscribe: jest.fn().mockReturnValue(of([]))
}))
};
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ArchiveComponent, ],
providers: [
{ provide: HotToastService, useValue: mockHotToastService },
{ provide: ApiService, useClass: MockApiService },
]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(ArchiveComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.onGridReady(mockGridReadyEvent);
});
it('should create the archive', () => {
expect(component).toBeTruthy();
});
it('should load the data on start', () => {
expect(component['api'].getKeyArchive).toHaveBeenCalled();
});
});

View File

@@ -1,28 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LostKeyComponent } from './lost-key.component';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
describe('LostKeyComponent', () => {
let component: LostKeyComponent;
let fixture: ComponentFixture<LostKeyComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [LostKeyComponent],
providers: [
{ provide: MatDialogRef, useValue: [] },
{ provide: MAT_DIALOG_DATA, useValue: [] }
]
})
.compileComponents();
fixture = TestBed.createComponent(LostKeyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -1,30 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { LostKeysComponent } from './lost-keys.component';
import { ApiService } from '../../../../shared/api.service';
import { MockApiService } from '../../../../../../mocks/services/mock.api.service';
import { HotToastService } from '@ngxpert/hot-toast';
describe('LostKeysComponent', () => {
let component: LostKeysComponent;
let fixture: ComponentFixture<LostKeysComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [LostKeysComponent],
providers: [
{ provide: ApiService, useClass: MockApiService },
HotToastService
]
})
.compileComponents();
fixture = TestBed.createComponent(LostKeysComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -1,37 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SelectKeyCylinderComponent } from './select-key-cylinder.component';
import { HotToastService } from '@ngxpert/hot-toast';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { MockHotToastService } from '../../../../../../mocks/services/mock.hottoast.service';
describe('SelectKeyCylinderComponent', () => {
let component: SelectKeyCylinderComponent;
let fixture: ComponentFixture<SelectKeyCylinderComponent>;
const mockHotToastService = {
info: jest.fn(),
error: jest.fn(),
success: jest.fn(),
}
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [SelectKeyCylinderComponent],
providers: [
{ provide: HotToastService, useClass: MockHotToastService },
{ provide: MatDialogRef, useValue: {} },
{ provide: MAT_DIALOG_DATA, useValue: [] }
]
})
.compileComponents();
fixture = TestBed.createComponent(SelectKeyCylinderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -1,109 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { KeysComponent } from './keys.component';
import { HotToastService } from '@ngxpert/hot-toast';
import { of } from 'rxjs';
import { ApiService } from '../../shared/api.service';
import { GridReadyEvent } from 'ag-grid-community';
import { MatDialog } from '@angular/material/dialog';
import { MockApiService } from '../../../../mocks/services/mock.api.service';
describe('KeysComponent', () => {
let component: KeysComponent;
let fixture: ComponentFixture<KeysComponent>;
const mockGridReadyEvent: GridReadyEvent = {
api: { setGridOption: jest.fn(), addEventListener: jest.fn(), getGridOption: jest.fn() },
columnApi: { someColumnApiMethod: jest.fn() },
type: 'gridReady',
} as any;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [KeysComponent, ],
providers: [
{ provide: ApiService, useClass: MockApiService },
{ provide: MatDialog, useClass: MockMatDialog },
HotToastService
]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(KeysComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.onGridReady(mockGridReadyEvent);
});
it('should create the keyscomponent', () => {
expect(component).toBeTruthy();
});
it('should call getCylinders on ngOnInit', () => {
component.ngOnInit();
expect(component['api'].getCylinders).toHaveBeenCalled();
});
it('should call getKeys and set rowData when loadKeys is called', () => {
component.loadKeys();
expect(component['api'].getKeys).toHaveBeenCalled();
expect(component.gridApi.setGridOption).toHaveBeenCalledWith('rowData', [{ name: 'Key 1' }]);
});
it('should call deleteKey when deleteKey is triggered', () => {
const keyId = '123';
component.deleteKey(keyId);
expect(component['api'].deleteKey).toHaveBeenCalledWith(keyId);
});
it('should call updateKey on cellEditEnd when a value is changed', () => {
const mockEvent = {
data: { id: '1', name: 'Old Name' },
oldValue: 'Old Name',
newValue: 'New Name',
valueChanged: true,
};
component.cellEditEnd(mockEvent as any);
expect(component['api'].updateKey).toHaveBeenCalledWith(mockEvent.data);
});
it('should not call updateKey on cellEditEnd if value is not changed', () => {
const mockEvent = {
data: { id: '1', name: 'Old Name' },
oldValue: 'Old Name',
newValue: 'Old Name',
valueChanged: false,
};
component.cellEditEnd(mockEvent as any);
expect(component['api'].updateKey).not.toHaveBeenCalled();
});
it('should reload Keys after creation', () => {
component['dialog'].open = jest.fn().mockReturnValue({
afterClosed: jest.fn().mockReturnValue(of(true)),
})
component.openCreateKey();
expect(component['dialog'].open).toHaveBeenCalled();
expect(component['api'].getKeys).toHaveBeenCalledTimes(2)
})
it('should not reload Keys after cancellation', () => {
component['dialog'].open = jest.fn().mockReturnValue({
afterClosed: jest.fn().mockReturnValue(of(null)),
})
component.openCreateKey();
expect(component['dialog'].open).toHaveBeenCalled();
expect(component['api'].getKeys).toHaveBeenCalledTimes(1)
})
});
class MockMatDialog {
open = jest.fn().mockReturnValue({
afterClosed: jest.fn().mockReturnValue(of(true)),
})
};