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

@@ -88,6 +88,9 @@
},
"defaultConfiguration": "development"
},
"test": {
"builder": "@angular/build:unit-test"
},
"extract-i18n": {
"builder": "@angular/build:extract-i18n"
}

View File

@@ -1,11 +0,0 @@
import type { Config } from 'jest';
const jestConfig: Config = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
moduleNameMapper: {
'@ngxpert/hot-toast': '<rootDir>/mocks/modules/hot-toast',
},
};
export default jestConfig;

8026
client/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -33,17 +33,19 @@
"zone.js": "~0.15.1"
},
"devDependencies": {
"@analogjs/vitest-angular": "^2.2.3",
"@angular/build": "^21.1.4",
"@angular/cli": "^21.1.4",
"@angular/compiler-cli": "^21.1.4",
"@faker-js/faker": "^9.0.3",
"@types/jest": "^29.5.14",
"@vitest/coverage-v8": "^4.0.18",
"autoprefixer": "^10.4.20",
"jest": "^30.2.0",
"jest-preset-angular": "^16.0.0",
"jsdom": "^28.0.0",
"postcss": "^8.4.49",
"tailwindcss": "^3.4.16",
"ts-node": "^10.9.2",
"typescript": "~5.9.3"
"typescript": "~5.9.3",
"vite": "^7.3.1",
"vitest": "^4.0.18"
}
}

View File

@@ -1,22 +1,14 @@
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { provideHttpClient } from '@angular/common/http';
import { describe, it, expect, beforeEach } from 'vitest';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AppComponent],
providers: [
provideHttpClient(),
provideHttpClient(),
]
}).compileComponents();
describe('MathService', () => {
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
it('should add two numbers', () => {
expect(5).toEqual(5)
});
});

View File

@@ -1,34 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateCylinderComponent } from './create-cylinder.component';
import { MatDialogRef } from '@angular/material/dialog';
import { HotToastService } from '@ngxpert/hot-toast';
import { ApiService } from '../../../../shared/api.service';
import { MockApiService } from '../../../../../../mocks/services/mock.api.service';
import { MockHotToastService } from '../../../../../../mocks/services/mock.hottoast.service';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
describe('CreateCylinderComponent', () => {
let component: CreateCylinderComponent;
let fixture: ComponentFixture<CreateCylinderComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CreateCylinderComponent, NoopAnimationsModule],
providers: [
{ provide: ApiService, useClass: MockApiService },
{ provide: MatDialogRef, useValue: [] },
{ provide: HotToastService, useClass: MockHotToastService }
]
})
.compileComponents();
fixture = TestBed.createComponent(CreateCylinderComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

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)),
})
};

View File

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

View File

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

View File

@@ -1,68 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SystemManagerComponent } from './system-manager.component';
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { AgGridAngular } from 'ag-grid-angular';
import { ApiService } from '../../../../shared/api.service';
import { HotToastService } from '@ngxpert/hot-toast';
import { MockApiService } from '../../../../../../mocks/services/mock.api.service';
import { GridReadyEvent } from 'ag-grid-community';
import { AuthService } from '../../../../core/auth/auth.service';
import { MockAuthService } from '../../../../../../mocks/services/mock.auth.service';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
describe('SystemManagerComponent', () => {
let component: SystemManagerComponent;
let fixture: ComponentFixture<SystemManagerComponent>;
let api: ApiService;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [SystemManagerComponent, AgGridAngular, MatDialogModule, NoopAnimationsModule],
providers: [
HotToastService,
{ provide: ApiService, useClass: MockApiService },
{
provide: MatDialogRef,
useValue: []
},
{
provide: MAT_DIALOG_DATA,
useValue: []
},
{ provide: AuthService, useClass: MockAuthService }
]
})
.compileComponents();
fixture = TestBed.createComponent(SystemManagerComponent);
component = fixture.componentInstance;
api = component['api']
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should initialize gridApi and gridColumnApi on gridReady and fill data', () => {
// Mock des GridReadyEvent
let mockData = [{ id: 1, name: 'Test' }];
const mockGridReadyEvent: GridReadyEvent = {
api: { setGridOption: jest.fn() },
columnApi: { someColumnApiMethod: jest.fn() },
type: 'gridReady',
} as any;
// Methode aufrufen
component.onGridReady(mockGridReadyEvent);
// Assertions
expect(component.gridApi).toBe(mockGridReadyEvent.api);
expect(api.getSystemManagers).toHaveBeenCalled();
expect(component.gridApi.setGridOption).toHaveBeenCalled();
});
});

View File

@@ -1,66 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { CreateSystemComponent } from './create.component';
import { ApiService } from '../../../shared/api.service';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { of, throwError } from 'rxjs';
import { HotToastService } from '@ngxpert/hot-toast';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MockApiService } from '../../../../../mocks/services/mock.api.service';
describe('CreateComponent', () => {
let component: CreateSystemComponent;
let fixture: ComponentFixture<CreateSystemComponent>;
let apiService: ApiService;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CreateSystemComponent, NoopAnimationsModule, FormsModule, ReactiveFormsModule],
providers: [
HotToastService,
{ provide: ApiService, useClass: MockApiService },
{
provide: MatDialogRef,
useValue: []
},
{
provide: MAT_DIALOG_DATA,
useValue: []
}
]
})
.compileComponents();
fixture = TestBed.createComponent(CreateSystemComponent);
component = fixture.componentInstance;
apiService = component['api'];
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call apiService.createSystem when createSystem is called', () => {
expect(apiService.createSystem).not.toHaveBeenCalled();
component.createForm.setValue({ name: 'Test System' });
component.save();
expect(apiService.createSystem).toHaveBeenCalledWith({ name: 'Test System' });
});
it('should handle success response correctly', () => {
jest.spyOn(apiService, 'createSystem').mockReturnValue(of({}));
const toastSpy = jest.spyOn(component['toast'], 'observe');
component.createForm.setValue({ name: 'Test System' });
component.save();
expect(toastSpy).toHaveBeenCalled();
});
it('should handle error response correctly', () => {
jest.spyOn(apiService, 'createSystem').mockReturnValue(throwError(() => new Error('Test Error')));
const toastSpy = jest.spyOn(component['toast'], 'observe');
component.save();
expect(toastSpy).toHaveBeenCalled();
});
});

View File

@@ -1,54 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { SystemComponent } from './system.component';
import { ApiService } from '../../shared/api.service';
import { GridReadyEvent } from 'ag-grid-community';
import { of } from 'rxjs';
describe('SystemcomponentComponent', () => {
let component: SystemComponent;
let mockApiService: MockApiService;
beforeEach(async () => {
mockApiService = new MockApiService();
await TestBed.configureTestingModule({
imports: [SystemComponent],
providers: [
{ provide: ApiService, useValue: mockApiService }
]
}).compileComponents();
const fixture = TestBed.createComponent(SystemComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create the SystemComponent', () => {
expect(component).toBeTruthy();
});
it('should initialize gridApi and gridColumnApi on gridReady and fill data', () => {
// Mock des GridReadyEvent
let mockData = [{ id: 1, name: 'Test' }];
mockApiService.getSystems.mockReturnValue(of(mockData));
const mockGridReadyEvent: GridReadyEvent = {
api: { setGridOption: jest.fn() },
columnApi: { someColumnApiMethod: jest.fn() },
type: 'gridReady',
} as any;
// Methode aufrufen
component.onGridReady(mockGridReadyEvent);
// Assertions
expect(component.gridApi).toBe(mockGridReadyEvent.api);
expect(mockApiService.getSystems).toHaveBeenCalled();
expect(component.gridApi.setGridOption).toHaveBeenCalled();
});
});
class MockApiService {
getSystems = jest.fn();
}

View File

@@ -1,47 +0,0 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AgSystemManagerComponent } from './ag-system-manager.component';
import { MatDialogModule } from '@angular/material/dialog';
import { AgGridAngular } from 'ag-grid-angular';
import { ApiService } from '../../../api.service';
import { of } from 'rxjs';
import { HotToastService } from '@ngxpert/hot-toast';
describe('AgSystemManagerComponent', () => {
let component: AgSystemManagerComponent;
let fixture: ComponentFixture<AgSystemManagerComponent>;
let mockApiService: MockApiService;
const mockHotToastService = {
observe: jest.fn().mockImplementation(() => ({
loading: 'speichern...',
success: 'Änderungen gespeichert',
error: 'Änderungen konnten nicht gespeichert werden!',
subscribe: jest.fn().mockReturnValue(of([]))
}))
};
beforeEach(async () => {
mockApiService = new MockApiService();
await TestBed.configureTestingModule({
imports: [AgSystemManagerComponent, AgGridAngular, MatDialogModule],
providers: [
{ provide: ApiService, useValue: mockApiService },
{ provide: HotToastService, useValue: mockHotToastService },
]
})
.compileComponents();
fixture = TestBed.createComponent(AgSystemManagerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
class MockApiService {
getSystems = jest.fn();
}

12
client/src/test-setup.ts Normal file
View File

@@ -0,0 +1,12 @@
import 'zone.js/testing';
import { TestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing';
// Wichtig: Initialisiert Angulars Test-Environment (wie früher in test.ts)
TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);

View File

@@ -1,13 +1,5 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"module": "CommonJS",
"types": ["jest"]
},
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
"types": ["vitest/globals", "node"]
}
}

15
client/vitest.config.ts Normal file
View File

@@ -0,0 +1,15 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['src/test-setup.ts'],
include: ['src/**/*.spec.ts'],
coverage: {
provider: 'v8',
reporter: ['text', 'html'],
reportsDirectory: './coverage'
}
}
});