From 0ca379b5e88fdf01e97700ba4ec9260252f8f96d Mon Sep 17 00:00:00 2001 From: gpascal123 Date: Thu, 24 Jul 2025 13:48:46 +0200 Subject: [PATCH 1/2] Added Unit Tests for ui-settings.component --- .../ui-settings/ui-settings.component.spec.ts | 56 +++++++++++++++++++ .../ui-settings/ui-settings.component.ts | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/app/account/settings/ui-settings/ui-settings.component.spec.ts diff --git a/src/app/account/settings/ui-settings/ui-settings.component.spec.ts b/src/app/account/settings/ui-settings/ui-settings.component.spec.ts new file mode 100644 index 000000000..56fa4e38a --- /dev/null +++ b/src/app/account/settings/ui-settings/ui-settings.component.spec.ts @@ -0,0 +1,56 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { AlertService } from '@services/shared/alert.service'; + +import { UiSettingsComponent } from '@src/app/account/settings/ui-settings/ui-settings.component'; +describe('UiSettingsComponent', () => { + let component: UiSettingsComponent; + let fixture: ComponentFixture; + let alertService: AlertService; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [UiSettingsComponent] + }).compileComponents(); + fixture = TestBed.createComponent(UiSettingsComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + alertService = TestBed.inject(AlertService); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should initialize form with default values', () => { + expect(component.form).toBeDefined(); + expect(component.form.get('timefmt')).toBeDefined(); + expect(component.form.get('layout')).toBeDefined(); + expect(component.form.get('theme')).toBeDefined(); + }); + + it('should call updateForm and patch form values', () => { + spyOn(component.form, 'patchValue'); + component.util = { uiConfig: { timefmt: 'dd/MM/yyyy h:mm:ss', layout: 'fixed', theme: 'dark' } } as any; + component.updateForm(); + expect(component.form.patchValue).toHaveBeenCalledWith({ + timefmt: 'dd/MM/yyyy h:mm:ss', + layout: 'fixed', + theme: 'dark' + }); + }); + + it('should show info message on submit', () => { + spyOn(component.util, 'updateSettings').and.returnValue(1); + spyOn(alertService, 'showInfoMessage'); + component.onSubmit(); + expect(alertService.showInfoMessage).toHaveBeenCalledWith('Reloading settings ...'); + }); + + it('should show "No changes were saved" if nothing changed', () => { + spyOn(component.util, 'updateSettings').and.returnValue(0); + spyOn(alertService, 'showInfoMessage'); + component.onSubmit(); + expect(alertService.showInfoMessage).toHaveBeenCalledWith('No changes were saved'); + }); +}); diff --git a/src/app/account/settings/ui-settings/ui-settings.component.ts b/src/app/account/settings/ui-settings/ui-settings.component.ts index 9a2877513..cad5bcae4 100644 --- a/src/app/account/settings/ui-settings/ui-settings.component.ts +++ b/src/app/account/settings/ui-settings/ui-settings.component.ts @@ -46,7 +46,7 @@ export class UiSettingsComponent implements OnInit { }); } - private updateForm(): void { + updateForm(): void { this.form.patchValue({ timefmt: this.util.uiConfig.timefmt, layout: this.util.uiConfig.layout, From ecb40c37eec5bfdfebf44af8a4aeaf9902cb3751 Mon Sep 17 00:00:00 2001 From: gpascal123 Date: Fri, 25 Jul 2025 08:33:18 +0200 Subject: [PATCH 2/2] Added Unit Tests for ui-settings.component --- .../ui-settings/ui-settings.component.spec.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app/account/settings/ui-settings/ui-settings.component.spec.ts b/src/app/account/settings/ui-settings/ui-settings.component.spec.ts index 56fa4e38a..a8d479efa 100644 --- a/src/app/account/settings/ui-settings/ui-settings.component.spec.ts +++ b/src/app/account/settings/ui-settings/ui-settings.component.spec.ts @@ -1,8 +1,14 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ReactiveFormsModule } from '@angular/forms'; +import { MatSelectModule } from '@angular/material/select'; import { AlertService } from '@services/shared/alert.service'; import { UiSettingsComponent } from '@src/app/account/settings/ui-settings/ui-settings.component'; +import { PageTitleModule } from '@src/app/shared/page-headers/page-title.module'; +import { TableModule } from '@src/app/shared/table/table-actions.module'; +import { UISettingsUtilityClass } from '@src/app/shared/utils/config'; + describe('UiSettingsComponent', () => { let component: UiSettingsComponent; let fixture: ComponentFixture; @@ -10,7 +16,8 @@ describe('UiSettingsComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - declarations: [UiSettingsComponent] + declarations: [UiSettingsComponent], + imports: [MatSelectModule, ReactiveFormsModule, PageTitleModule, TableModule] }).compileComponents(); fixture = TestBed.createComponent(UiSettingsComponent); component = fixture.componentInstance; @@ -31,7 +38,9 @@ describe('UiSettingsComponent', () => { it('should call updateForm and patch form values', () => { spyOn(component.form, 'patchValue'); - component.util = { uiConfig: { timefmt: 'dd/MM/yyyy h:mm:ss', layout: 'fixed', theme: 'dark' } } as any; + component.util = { + uiConfig: { timefmt: 'dd/MM/yyyy h:mm:ss', layout: 'fixed', theme: 'dark' } + } as UISettingsUtilityClass; component.updateForm(); expect(component.form.patchValue).toHaveBeenCalledWith({ timefmt: 'dd/MM/yyyy h:mm:ss',