Fix some test
testing/ogGui-multibranch/pipeline/head This commit is unstable Details

pull/7/head^2
Alvaro Puente Mella 2024-11-22 13:15:46 +01:00
parent e943da7111
commit 04b631ff08
3 changed files with 307 additions and 20 deletions

View File

@ -1,19 +1,23 @@
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
import { TranslateModule, TranslateService } from '@ngx-translate/core';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
let translateService: TranslateService;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
RouterTestingModule,
TranslateModule.forRoot()
TranslateModule.forRoot()
],
declarations: [
AppComponent
],
}).compileComponents();
translateService = TestBed.inject(TranslateService);
});
it('should create the app', () => {
@ -21,4 +25,42 @@ describe('AppComponent', () => {
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it(`should have as title 'ogWebconsole'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('ogWebconsole');
});
it('should set the language from localStorage on creation', () => {
spyOn(localStorage, 'getItem').and.returnValue('en'); // Simula que el idioma guardado es "en"
spyOn(translateService, 'use');
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(localStorage.getItem).toHaveBeenCalledWith('language');
expect(translateService.use).toHaveBeenCalledWith('en');
});
it('should default to Spanish if no language is saved in localStorage', () => {
spyOn(localStorage, 'getItem').and.returnValue(null); // Simula que no hay idioma guardado
spyOn(translateService, 'use');
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(localStorage.getItem).toHaveBeenCalledWith('language');
expect(translateService.use).toHaveBeenCalledWith('es');
});
it('should set language to Spanish in sessionStorage on ngOnInit', () => {
spyOn(sessionStorage, 'setItem');
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
app.ngOnInit();
expect(sessionStorage.setItem).toHaveBeenCalledWith('language', 'es');
});
});

View File

@ -0,0 +1,243 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { GroupsComponent } from './groups.component';
import { MatInputModule } from '@angular/material/input';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatOptionModule } from '@angular/material/core';
import { MatDividerModule } from '@angular/material/divider';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSelectModule } from '@angular/material/select';
import { MatTableModule } from '@angular/material/table';
import { MatTooltipModule } from '@angular/material/tooltip';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatListModule } from '@angular/material/list';
import { MatTabsModule } from '@angular/material/tabs';
import { MatCardModule } from '@angular/material/card';
import { TranslateModule } from '@ngx-translate/core';
import { JoyrideModule } from 'ngx-joyride';
import { AdvancedSearchComponent } from './components/advanced-search/advanced-search.component';
import { ClientTabViewComponent } from './components/client-tab-view/client-tab-view.component';
import { OrganizationalUnitTabViewComponent } from './components/organizational-unit-tab-view/organizational-unit-tab-view.component';
import { MatMenuModule } from '@angular/material/menu';
describe('GroupsComponent', () => {
let component: GroupsComponent;
let fixture: ComponentFixture<GroupsComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [GroupsComponent, AdvancedSearchComponent, ClientTabViewComponent, OrganizationalUnitTabViewComponent],
imports: [
HttpClientTestingModule,
ToastrModule.forRoot(),
BrowserAnimationsModule,
MatDividerModule,
MatFormFieldModule,
MatInputModule,
MatIconModule,
MatButtonModule,
MatTableModule,
MatPaginatorModule,
MatTooltipModule,
FormsModule,
ReactiveFormsModule,
MatProgressSpinnerModule,
MatDialogModule,
MatSelectModule,
MatTabsModule,
MatAutocompleteModule,
MatListModule,
MatCardModule,
MatMenuModule,
TranslateModule.forRoot(),
JoyrideModule.forRoot(),
],
providers: [
{ provide: MatDialogRef, useValue: {} },
{ provide: MAT_DIALOG_DATA, useValue: { data: { id: 123 } } }
]
})
.compileComponents();
fixture = TestBed.createComponent(GroupsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should call search on ngOnInit', () => {
spyOn(component, 'search');
component.ngOnInit();
expect(component.search).toHaveBeenCalled();
});
it('should call getFilters on ngOnInit', () => {
spyOn(component, 'getFilters');
component.ngOnInit();
expect(component.getFilters).toHaveBeenCalled();
});
it('should call search method', () => {
spyOn(component, 'search');
component.search();
expect(component.search).toHaveBeenCalled();
});
it('should call getFilters method', () => {
spyOn(component, 'getFilters');
component.getFilters();
expect(component.getFilters).toHaveBeenCalled();
});
it('should call onTabChange method', () => {
spyOn(component, 'onTabChange');
const event = { index: 2 } as any;
component.onTabChange(event);
expect(component.onTabChange).toHaveBeenCalledWith(event);
});
it('should call onSelectUnidad method', () => {
spyOn(component, 'onSelectUnidad');
const unidad = { id: '1', name: 'Test' } as any;
component.onSelectUnidad(unidad);
expect(component.onSelectUnidad).toHaveBeenCalledWith(unidad);
});
it('should call onSelectChild method', () => {
spyOn(component, 'onSelectChild');
const child = { id: '1', name: 'Test', type: 'unit' } as any;
component.onSelectChild(child);
expect(component.onSelectChild).toHaveBeenCalledWith(child);
});
it('should call navigateToBreadcrumb method', () => {
spyOn(component, 'navigateToBreadcrumb');
component.navigateToBreadcrumb(1);
expect(component.navigateToBreadcrumb).toHaveBeenCalledWith(1);
});
it('should call loadChildrenAndClients method', () => {
spyOn(component, 'loadChildrenAndClients');
component.loadChildrenAndClients('1');
expect(component.loadChildrenAndClients).toHaveBeenCalledWith('1');
});
it('should call onDeleteClick method', () => {
spyOn(component, 'onDeleteClick');
const event = new MouseEvent('click');
component.onDeleteClick(event, 'uuid', 'name', 'client');
expect(component.onDeleteClick).toHaveBeenCalledWith(event, 'uuid', 'name', 'client');
});
it('should call onEditClick method', () => {
spyOn(component, 'onEditClick');
const event = new MouseEvent('click');
component.onEditClick(event, 'client', 'uuid');
expect(component.onEditClick).toHaveBeenCalledWith(event, 'client', 'uuid');
});
it('should call onShowClick method', () => {
spyOn(component, 'onShowClick');
const event = new MouseEvent('click');
component.onShowClick(event, { type: 'unit' });
expect(component.onShowClick).toHaveBeenCalledWith(event, { type: 'unit' });
});
it('should call onTreeClick method', () => {
spyOn(component, 'onTreeClick');
const event = new MouseEvent('click');
component.onTreeClick(event, { type: 'unit' });
expect(component.onTreeClick).toHaveBeenCalledWith(event, { type: 'unit' });
});
it('should call onExecuteCommand method', () => {
spyOn(component, 'onExecuteCommand');
const event = new MouseEvent('click');
component.onExecuteCommand(event, 'child', 'name', 'type');
expect(component.onExecuteCommand).toHaveBeenCalledWith(event, 'child', 'name', 'type');
});
it('should call openSnackBar method', () => {
spyOn(component, 'openSnackBar');
component.openSnackBar(true, 'message');
expect(component.openSnackBar).toHaveBeenCalledWith(true, 'message');
});
it('should call openBottomSheet method', () => {
spyOn(component, 'openBottomSheet');
component.openBottomSheet();
expect(component.openBottomSheet).toHaveBeenCalled();
});
it('should call roomMap method', () => {
spyOn(component, 'roomMap');
component.roomMap();
expect(component.roomMap).toHaveBeenCalled();
});
it('should call applyFilter method', () => {
spyOn(component, 'applyFilter');
component.applyFilter();
expect(component.applyFilter).toHaveBeenCalled();
});
it('should call onPageChange method', () => {
spyOn(component, 'onPageChange');
const event = { pageIndex: 1, pageSize: 10 } as any;
component.onPageChange(event);
expect(component.onPageChange).toHaveBeenCalledWith(event);
});
it('should call saveFilters method', () => {
spyOn(component, 'saveFilters');
component.saveFilters();
expect(component.saveFilters).toHaveBeenCalled();
});
it('should call loadSelectedFilter method', () => {
spyOn(component, 'loadSelectedFilter');
component.loadSelectedFilter(['name', 'uuid']);
expect(component.loadSelectedFilter).toHaveBeenCalledWith(['name', 'uuid']);
});
it('should call onCheckboxChange method', () => {
spyOn(component, 'onCheckboxChange');
const event = { checked: true } as any;
component.onCheckboxChange(event, 'name', 'uuid');
expect(component.onCheckboxChange).toHaveBeenCalledWith(event, 'name', 'uuid');
});
it('should call toggleSelectAll method', () => {
spyOn(component, 'toggleSelectAll');
component.toggleSelectAll();
expect(component.toggleSelectAll).toHaveBeenCalled();
});
it('should call isSelected method', () => {
spyOn(component, 'isSelected');
component.isSelected('name');
expect(component.isSelected).toHaveBeenCalledWith('name');
});
it('should call sendActions method', () => {
spyOn(component, 'sendActions');
component.sendActions();
expect(component.sendActions).toHaveBeenCalled();
});
it('should call iniciarTour method', () => {
spyOn(component, 'iniciarTour');
component.iniciarTour();
expect(component.iniciarTour).toHaveBeenCalled();
});
});

View File

@ -4,9 +4,9 @@ import { MatDialog } from '@angular/material/dialog';
import { HttpClient } from '@angular/common/http';
import { ToastrService } from 'ngx-toastr';
import { of } from 'rxjs';
import { MatAccordion, MatExpansionPanel, MatExpansionPanelHeader, MatExpansionPanelTitle, MatExpansionPanelDescription } from '@angular/material/expansion';
import { MatIcon } from '@angular/material/icon';
import { MatDivider } from '@angular/material/divider';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatIconModule } from '@angular/material/icon';
import { MatDividerModule } from '@angular/material/divider';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatSelectModule } from '@angular/material/select';
import { MatPaginatorModule } from '@angular/material/paginator';
@ -28,22 +28,18 @@ describe('OgDhcpSubnetsComponent', () => {
mockDialog = jasmine.createSpyObj('MatDialog', ['open']);
mockHttpClient = jasmine.createSpyObj('HttpClient', ['get', 'post', 'put', 'delete']);
mockToastrService = jasmine.createSpyObj('ToastrService', ['success', 'error']);
mockHttpClient.get.and.returnValue(of([]));
mockHttpClient.get.and.returnValue(of({ 'hydra:member': [], 'hydra:totalItems': 0 }));
mockHttpClient.post.and.returnValue(of({}));
await TestBed.configureTestingModule({
declarations: [OgDhcpSubnetsComponent],
imports: [
MatAccordion,
MatExpansionPanel,
MatExpansionPanelHeader,
MatExpansionPanelTitle,
MatExpansionPanelDescription,
MatIcon,
MatDivider,
MatFormFieldModule,
MatSelectModule,
MatPaginatorModule,
imports: [
MatExpansionModule,
MatIconModule,
MatDividerModule,
MatFormFieldModule,
MatSelectModule,
MatPaginatorModule,
BrowserAnimationsModule,
FormsModule,
MatInputModule,
@ -54,8 +50,8 @@ describe('OgDhcpSubnetsComponent', () => {
providers: [
{ provide: MatDialog, useValue: mockDialog },
{ provide: HttpClient, useValue: mockHttpClient },
{ provide: ToastrService, useValue: mockToastrService }
]
{ provide: ToastrService, useValue: mockToastrService },
],
}).compileComponents();
});
@ -69,4 +65,10 @@ describe('OgDhcpSubnetsComponent', () => {
expect(component).toBeTruthy();
});
it('should call syncSubnets and handle success', () => {
component.syncSubnets();
expect(mockHttpClient.post).toHaveBeenCalledWith(`${component.baseUrl}/subnets/sync`, {});
expect(mockToastrService.success).toHaveBeenCalledWith('Sincronización con componente DHCP exitosa');
});
});