From bdd2a8bcab6b75eba52e43dccea7c80ff28f371b Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Fri, 17 Nov 2023 21:19:38 +0100 Subject: [PATCH] CST-12498 Tests added, need to check TODOs in ldn-services-directory.component.spec.ts --- .../ldn-services-directory.component.spec.ts | 251 ++++++++++-------- 1 file changed, 137 insertions(+), 114 deletions(-) diff --git a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts index 6912379b78..8213e5bc53 100644 --- a/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts +++ b/src/app/admin/admin-ldn-services/ldn-services-directory/ldn-services-directory.component.spec.ts @@ -1,125 +1,148 @@ -import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; -import { ChangeDetectorRef, EventEmitter, TemplateRef, ViewChild } from '@angular/core'; -import { NotificationsService } from '../../../shared/notifications/notifications.service'; -import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub'; -import { TranslateModule, TranslateService } from '@ngx-translate/core'; -import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; -import { LdnServicesService } from '../ldn-services-data/ldn-services-data.service'; -import { PaginationService } from '../../../core/pagination/pagination.service'; -import { PaginationServiceStub } from '../../../shared/testing/pagination-service.stub'; -import { of } from 'rxjs'; -import { LdnService } from "../ldn-services-model/ldn-services.model"; -import { PaginatedList } from "../../../core/data/paginated-list.model"; -import { RemoteData } from "../../../core/data/remote-data"; -import { LdnServicesOverviewComponent } from './ldn-services-directory.component'; +import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; +import {ChangeDetectorRef, EventEmitter} from '@angular/core'; +import {NotificationsService} from '../../../shared/notifications/notifications.service'; +import {NotificationsServiceStub} from '../../../shared/testing/notifications-service.stub'; +import {TranslateModule, TranslateService} from '@ngx-translate/core'; +import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; +import {LdnServicesService} from '../ldn-services-data/ldn-services-data.service'; +import {PaginationService} from '../../../core/pagination/pagination.service'; +import {PaginationServiceStub} from '../../../shared/testing/pagination-service.stub'; +import {of} from 'rxjs'; +import {LdnService} from "../ldn-services-model/ldn-services.model"; +import {PaginatedList} from "../../../core/data/paginated-list.model"; +import {RemoteData} from "../../../core/data/remote-data"; +import {LdnServicesOverviewComponent} from './ldn-services-directory.component'; describe('LdnServicesOverviewComponent', () => { - let component: LdnServicesOverviewComponent; - let fixture: ComponentFixture; - let ldnServicesService: LdnServicesService; - let paginationService: PaginationService; - let modalService: NgbModal; - let notificationsService: NotificationsService; - let translateService: TranslateService; + let component: LdnServicesOverviewComponent; + let fixture: ComponentFixture; + let ldnServicesService: LdnServicesService; + let paginationService: PaginationService; + let modalService: NgbModal; + let notificationsService: NotificationsService; + let translateService: TranslateService; - const translateServiceStub = { - get: () => of('translated-text'), - onLangChange: new EventEmitter(), - onTranslationChange: new EventEmitter(), - onDefaultLangChange: new EventEmitter() - }; + const translateServiceStub = { + get: () => of('translated-text'), + onLangChange: new EventEmitter(), + onTranslationChange: new EventEmitter(), + onDefaultLangChange: new EventEmitter() + }; - beforeEach(async () => { - await TestBed.configureTestingModule({ - imports: [TranslateModule.forRoot()], - declarations: [LdnServicesOverviewComponent], - providers: [ - { provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch']) }, - { provide: PaginationService, useValue: new PaginationServiceStub() }, - { provide: NgbModal, useValue: { open: () => { /*comment*/ } } }, - { provide: ChangeDetectorRef, useValue: {} }, - { provide: NotificationsService, useValue: NotificationsServiceStub }, - { provide: TranslateService, useValue: translateServiceStub }, - ] - }).compileComponents(); - }); - - beforeEach(() => { - fixture = TestBed.createComponent(LdnServicesOverviewComponent); - component = fixture.componentInstance; - ldnServicesService = TestBed.inject(LdnServicesService); - paginationService = TestBed.inject(PaginationService); - modalService = TestBed.inject(NgbModal); - notificationsService = TestBed.inject(NotificationsService); - translateService = TestBed.inject(TranslateService); - component.modalRef = jasmine.createSpyObj({ close: null }); - component.isProcessingSub = jasmine.createSpyObj({ unsubscribe: null }); - component.ldnServicesRD$ = of({} as RemoteData>); // You can adjust the mock data as needed - fixture.detectChanges(); - }); - - it('should create', () => { - expect(component).toBeTruthy(); - }); - - describe('ngOnInit', () => { - it('should call setLdnServices', fakeAsync(() => { - spyOn(component, 'setLdnServices').and.callThrough(); - component.ngOnInit(); - tick(); - expect(component.setLdnServices).toHaveBeenCalled(); - })); - - it('should set ldnServicesRD$ with mock data', fakeAsync(() => { - spyOn(component, 'setLdnServices').and.callThrough(); - const mockData = { /* your mock data here */ }; - component.ldnServicesRD$ = of(mockData as RemoteData>); - component.ngOnInit(); - tick(); - expect(component.setLdnServices).toHaveBeenCalled(); - // Add more expectations based on your mock data and component behavior - })); - }); - - describe('ngOnDestroy', () => { - it('should call paginationService.clearPagination and unsubscribe', () => { - spyOn(paginationService, 'clearPagination'); - spyOn(component.isProcessingSub, 'unsubscribe'); - component.ngOnDestroy(); - expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); - expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot()], + declarations: [LdnServicesOverviewComponent], + providers: [ + {provide: LdnServicesService, useValue: jasmine.createSpyObj('LdnServicesService', ['findAll', 'delete', 'patch'])}, + {provide: PaginationService, useValue: new PaginationServiceStub()}, + { + provide: NgbModal, useValue: { + open: () => { /*comment*/ + } + } + }, + {provide: ChangeDetectorRef, useValue: {}}, + {provide: NotificationsService, useValue: NotificationsServiceStub}, + {provide: TranslateService, useValue: translateServiceStub}, + ] + }).compileComponents(); }); - }); - describe('openDeleteModal', () => { - it('should open delete modal', () => { - spyOn(modalService, 'open'); - component.openDeleteModal(component.deleteModal); - expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); + beforeEach(() => { + fixture = TestBed.createComponent(LdnServicesOverviewComponent); + component = fixture.componentInstance; + ldnServicesService = TestBed.inject(LdnServicesService); + paginationService = TestBed.inject(PaginationService); + modalService = TestBed.inject(NgbModal); + notificationsService = TestBed.inject(NotificationsService); + translateService = TestBed.inject(TranslateService); + component.modalRef = jasmine.createSpyObj({close: null}); + component.isProcessingSub = jasmine.createSpyObj({unsubscribe: null}); + component.ldnServicesRD$ = of({} as RemoteData>); + fixture.detectChanges(); }); - }); - describe('closeModal', () => { - it('should close modal and detect changes', () => { - spyOn(component.modalRef, 'close'); - spyOn(component.cdRef, 'detectChanges'); - component.closeModal(); - expect(component.modalRef.close).toHaveBeenCalled(); - expect(component.cdRef.detectChanges).toHaveBeenCalled(); + it('should create', () => { + expect(component).toBeTruthy(); }); - }); - describe('deleteSelected', () => { - it('should delete selected service and update data', fakeAsync(() => { - const serviceId = '123'; - const mockRemoteData = { /* insert mock data with service id 123 */ }; - spyOn(component, 'setLdnServices').and.callThrough(); - const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData>)); - component.selectedServiceId = serviceId; - component.deleteSelected(serviceId, ldnServicesService); - tick(); - expect(deleteSpy).toHaveBeenCalledWith(serviceId); - expect(mockRemoteData) - })); - }); + describe('ngOnInit', () => { + it('should call setLdnServices', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + })); + + it('should set ldnServicesRD$ with mock data', fakeAsync(() => { + spyOn(component, 'setLdnServices').and.callThrough(); + 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>; + component.ldnServicesRD$ = of(mockData as RemoteData>); + component.ngOnInit(); + tick(); + expect(component.setLdnServices).toHaveBeenCalled(); + 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'); + })); + }); + + describe('ngOnDestroy', () => { + it('should call paginationService.clearPagination and unsubscribe', () => { + spyOn(paginationService, 'clearPagination'); + spyOn(component.isProcessingSub, 'unsubscribe'); + component.ngOnDestroy(); + expect(paginationService.clearPagination).toHaveBeenCalledWith(component.pageConfig.id); + expect(component.isProcessingSub.unsubscribe).toHaveBeenCalled(); + }); + }); + + describe('openDeleteModal', () => { + it('should open delete modal', () => { + spyOn(modalService, 'open'); + component.openDeleteModal(component.deleteModal); + expect(modalService.open).toHaveBeenCalledWith(component.deleteModal); + }); + }); + + describe('closeModal', () => { + it('should close modal and detect changes', () => { + spyOn(component.modalRef, 'close'); + spyOn(component.cdRef, 'detectChanges'); + component.closeModal(); + expect(component.modalRef.close).toHaveBeenCalled(); + expect(component.cdRef.detectChanges).toHaveBeenCalled(); + }); + }); + + describe('deleteSelected', () => { + it('should delete selected service and update data', fakeAsync(() => { + const serviceId = '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(); + const deleteSpy = spyOn(ldnServicesService, 'delete').and.returnValue(of(mockRemoteData as RemoteData>)); + component.selectedServiceId = serviceId; + component.deleteSelected(serviceId, ldnServicesService); + tick(); + expect(deleteSpy).toHaveBeenCalledWith(serviceId); + })); + }); });