From 8d78c93eb77fe0ecc2963e970766aa51fe4d0fc8 Mon Sep 17 00:00:00 2001 From: kbeyro <121854496+kbeyro@users.noreply.github.com> Date: Thu, 5 Jun 2025 15:30:46 +0200 Subject: [PATCH] fix test --- .../add-cluster/add-cluster.component.spec.ts | 52 +++++++++++++++---- .../manager/manager.component.spec.ts | 13 ----- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/app/shared/admin/clusters/add-cluster/add-cluster.component.spec.ts b/src/app/shared/admin/clusters/add-cluster/add-cluster.component.spec.ts index bfc7d7f7..1007d6e3 100644 --- a/src/app/shared/admin/clusters/add-cluster/add-cluster.component.spec.ts +++ b/src/app/shared/admin/clusters/add-cluster/add-cluster.component.spec.ts @@ -1,23 +1,57 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; - +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { AddClusterComponent } from './add-cluster.component'; +import { ClusterManagerService } from '../../../../service/cluster-manager.service'; +import { Router } from '@angular/router'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { FormsModule } from '@angular/forms'; +import { of, throwError } from 'rxjs'; +import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core'; +import { TranslateFakeLoader, TranslateLoader, TranslateModule } from '@ngx-translate/core'; +import { DatePipe } from '@angular/common'; describe('AddClusterComponent', () => { let component: AddClusterComponent; let fixture: ComponentFixture<AddClusterComponent>; + let clusterService: jasmine.SpyObj<ClusterManagerService>; + let mockRouter: jasmine.SpyObj<Router>; + + beforeEach(waitForAsync(() => { + const clusterServiceSpy = jasmine.createSpyObj('ClusterManagerService', ['sendCluster']); + const routerSpy = jasmine.createSpyObj('Router', ['navigate']); + + TestBed.configureTestingModule({ + declarations: [AddClusterComponent, ], + imports: [ + FormsModule, + HttpClientTestingModule, + TranslateModule.forRoot({ + loader: { + provide: TranslateLoader, + useClass: TranslateFakeLoader + } + }), + ], + providers: [ + { provide: ClusterManagerService, useValue: clusterServiceSpy }, + { provide: Router, useValue: routerSpy }, + DatePipe + ], + schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA] + }).compileComponents(); - beforeEach(async () => { - await TestBed.configureTestingModule({ - declarations: [AddClusterComponent] - }) - .compileComponents(); - + clusterService = TestBed.inject(ClusterManagerService) as jasmine.SpyObj<ClusterManagerService>; + mockRouter = TestBed.inject(Router) as jasmine.SpyObj<Router>; + })); + + beforeEach(() => { fixture = TestBed.createComponent(AddClusterComponent); component = fixture.componentInstance; fixture.detectChanges(); }); - it('should create', () => { + it('should create the component', () => { expect(component).toBeTruthy(); }); + + }); diff --git a/src/app/shared/admin/clusters/manager/manager.component.spec.ts b/src/app/shared/admin/clusters/manager/manager.component.spec.ts index a0d38438..a6292206 100644 --- a/src/app/shared/admin/clusters/manager/manager.component.spec.ts +++ b/src/app/shared/admin/clusters/manager/manager.component.spec.ts @@ -129,18 +129,5 @@ describe('ClusterManagerComponent', () => { expect(component.updatedFile.name).toBe('test.yaml'); }); - it('should call closeModalAndSaveCluster and reset state after saving', () => { - const mockFile = new File(['test content'], 'test.yaml', { type: 'application/x-yaml' }); - // const mockCluster = { id: 3, name: 'Cluster C', codename: 'CodeC' }; - component.updatedFile = mockFile; - component.addedCluster = mockClusters[0]; - - clusterService.sendCluster.and.returnValue(of(mockClusters[0])); - component.closeModalAndSaveCluster(); - - expect(clusterService.sendCluster).toHaveBeenCalledWith(mockFile, mockClusters[0]); - // expect(component.updatedFile).toBeNull(); - // expect(component.addedCluster).toEqual(new ClusterManager()); - }); }); \ No newline at end of file -- GitLab