CST-12498 Tests added, need to check TODOs in ldn-services-directory.component.spec.ts

This commit is contained in:
Mattia Vianelli
2023-11-17 21:19:38 +01:00
parent 92b791ed15
commit bdd2a8bcab

View File

@@ -1,17 +1,17 @@
import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
import { ChangeDetectorRef, EventEmitter, TemplateRef, ViewChild } from '@angular/core'; import {ChangeDetectorRef, EventEmitter} from '@angular/core';
import { NotificationsService } from '../../../shared/notifications/notifications.service'; import {NotificationsService} from '../../../shared/notifications/notifications.service';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; import {NotificationsServiceStub} from '../../../shared/testing/notifications-service.stub';
import { TranslateModule, TranslateService } from '@ngx-translate/core'; import {TranslateModule, TranslateService} from '@ngx-translate/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import { LdnServicesService } from '../ldn-services-data/ldn-services-data.service'; import {LdnServicesService} from '../ldn-services-data/ldn-services-data.service';
import { PaginationService } from '../../../core/pagination/pagination.service'; import {PaginationService} from '../../../core/pagination/pagination.service';
import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub'; import {PaginationServiceStub} from '../../../shared/testing/pagination-service.stub';
import { of } from 'rxjs'; import {of} from 'rxjs';
import { LdnService } from "../ldn-services-model/ldn-services.model"; import {LdnService} from "../ldn-services-model/ldn-services.model";
import { PaginatedList } from "../../../core/data/paginated-list.model"; import {PaginatedList} from "../../../core/data/paginated-list.model";
import { RemoteData } from "../../../core/data/remote-data"; import {RemoteData} from "../../../core/data/remote-data";
import { LdnServicesOverviewComponent } from './ldn-services-directory.component'; import {LdnServicesOverviewComponent} from './ldn-services-directory.component';
describe('LdnServicesOverviewComponent', () => { describe('LdnServicesOverviewComponent', () => {
let component: LdnServicesOverviewComponent; let component: LdnServicesOverviewComponent;
@@ -34,12 +34,17 @@ describe('LdnServicesOverviewComponent', () => {
imports: [TranslateModule.forRoot()], imports: [TranslateModule.forRoot()],
declarations: [LdnServicesOverviewComponent], declarations: [LdnServicesOverviewComponent],
providers: [ providers: [
{ provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch']) }, {provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch'])},
{ provide: PaginationService, useValue: new PaginationServiceStub() }, {provide: PaginationService, useValue: new PaginationServiceStub()},
{ provide: NgbModal, useValue: { open: () => { /*comment*/ } } }, {
{ provide: ChangeDetectorRef, useValue: {} }, provide: NgbModal, useValue: {
{ provide: NotificationsService, useValue: NotificationsServiceStub }, open: () => { /*comment*/
{ provide: TranslateService, useValue: translateServiceStub }, }
}
},
{provide: ChangeDetectorRef, useValue: {}},
{provide: NotificationsService, useValue: NotificationsServiceStub},
{provide: TranslateService, useValue: translateServiceStub},
] ]
}).compileComponents(); }).compileComponents();
}); });
@@ -52,9 +57,9 @@ describe('LdnServicesOverviewComponent', () => {
modalService = TestBed.inject(NgbModal); modalService = TestBed.inject(NgbModal);
notificationsService = TestBed.inject(NotificationsService); notificationsService = TestBed.inject(NotificationsService);
translateService = TestBed.inject(TranslateService); translateService = TestBed.inject(TranslateService);
component.modalRef = jasmine.createSpyObj({ close: null }); component.modalRef = jasmine.createSpyObj({close: null});
component.isProcessingSub = jasmine.createSpyObj({ unsubscribe: null }); component.isProcessingSub = jasmine.createSpyObj({unsubscribe: null});
component.ldnServicesRD$ = of({} as RemoteData<PaginatedList<LdnService>>); // You can adjust the mock data as needed component.ldnServicesRD$ = of({} as RemoteData<PaginatedList<LdnService>>);
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -72,12 +77,30 @@ describe('LdnServicesOverviewComponent', () => {
it('should set ldnServicesRD$ with mock data', fakeAsync(() => { it('should set ldnServicesRD$ with mock data', fakeAsync(() => {
spyOn(component, 'setLdnServices').and.callThrough(); spyOn(component, 'setLdnServices').and.callThrough();
const mockData = { /* your mock data here */ }; const mockData = {
payload: {
page: [
{id: 1, name: 'Service 1', description: 'Description 1', enabled: true},
{id: 2, name: 'Service 2', description: 'Description 2', enabled: false},
{id: 3, name: 'Service 3', description: 'Description 3', enabled: true},
],
totalElements: 3
}
} as RemoteData<PaginatedList<LdnService>>;
component.ldnServicesRD$ = of(mockData as RemoteData<PaginatedList<LdnService>>); component.ldnServicesRD$ = of(mockData as RemoteData<PaginatedList<LdnService>>);
component.ngOnInit(); component.ngOnInit();
tick(); tick();
expect(component.setLdnServices).toHaveBeenCalled(); expect(component.setLdnServices).toHaveBeenCalled();
// Add more expectations based on your mock data and component behavior fixture.detectChanges();
console.log(fixture.nativeElement.innerHTML);
console.log('Mock Data:', mockData);
const tableRows = fixture.nativeElement.querySelectorAll('tbody tr');
console.log('Table Rows:', tableRows);
//TODO: check why the tbody tr length table rows is 0 when i sjhould be 3 accordingly to the mock
expect(tableRows.length).toBe(mockData.payload.page.length);
const firstRowContent = tableRows[0].textContent;
expect(firstRowContent).toContain('Service 1');
expect(firstRowContent).toContain('Description 1');
})); }));
}); });
@@ -112,14 +135,14 @@ describe('LdnServicesOverviewComponent', () => {
describe('deleteSelected', () => { describe('deleteSelected', () => {
it('should delete selected service and update data', fakeAsync(() => { it('should delete selected service and update data', fakeAsync(() => {
const serviceId = '123'; const serviceId = '123';
const mockRemoteData = { /* insert mock data with service id 123 */ }; //TODO: finish up this test to use a mockdata containing something
const mockRemoteData = { /* insert mock data with service id 123 */};
spyOn(component, 'setLdnServices').and.callThrough(); spyOn(component, 'setLdnServices').and.callThrough();
const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData<PaginatedList<LdnService>>)); const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData<PaginatedList<LdnService>>));
component.selectedServiceId = serviceId; component.selectedServiceId = serviceId;
component.deleteSelected(serviceId, ldnServicesService); component.deleteSelected(serviceId, ldnServicesService);
tick(); tick();
expect(deleteSpy).toHaveBeenCalledWith(serviceId); expect(deleteSpy).toHaveBeenCalledWith(serviceId);
expect(mockRemoteData)
})); }));
}); });
}); });