From 2802841b5c6413dd6cbf0df2326f48104c655e04 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Wed, 6 Mar 2024 14:15:14 +0100 Subject: [PATCH] fixed failing unit tests --- .../clarin-files-section.component.spec.ts | 50 ++++++++++++++++++- .../clarin-license-info.component.spec.ts | 6 +++ .../full/full-item-page.component.spec.ts | 3 +- .../clarin-date-item-field.component.spec.ts | 18 +++++++ ...n-description-item-field.component.spec.ts | 18 ++++++- ...in-identifier-item-field.component.spec.ts | 46 +++++++++++++++-- .../simple/item-page.component.spec.ts | 2 +- src/app/shared/clarin-shared-util.ts | 2 +- 8 files changed, 134 insertions(+), 11 deletions(-) diff --git a/src/app/item-page/clarin-files-section/clarin-files-section.component.spec.ts b/src/app/item-page/clarin-files-section/clarin-files-section.component.spec.ts index 0cbb68c4ed7..d7f0f08d869 100644 --- a/src/app/item-page/clarin-files-section/clarin-files-section.component.spec.ts +++ b/src/app/item-page/clarin-files-section/clarin-files-section.component.spec.ts @@ -1,14 +1,60 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; - import { ClarinFilesSectionComponent } from './clarin-files-section.component'; +import { RegistryService } from '../../core/registry/registry.service'; +import { Router } from '@angular/router'; +import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; +import { RouterMock } from '../../shared/mocks/router.mock'; +import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub'; +import { TranslateModule } from '@ngx-translate/core'; +import { MetadataBitstream } from '../../core/metadata/metadata-bitstream.model'; +import { ResourceType } from '../../core/shared/resource-type'; +import { HALLink } from '../../core/shared/hal-link.model'; +import { BehaviorSubject , of} from 'rxjs'; describe('ClarinFilesSectionComponent', () => { let component: ClarinFilesSectionComponent; let fixture: ComponentFixture; + let mockRegistryService: any; + let halService: HALEndpointService; + // Set up the mock service's getMetadataBitstream method to return a simple stream + const metadatabitstream = new MetadataBitstream(); + metadatabitstream.id = 123; + metadatabitstream.name = 'test'; + metadatabitstream.description = 'test'; + metadatabitstream.fileSize = '1MB'; + metadatabitstream.checksum = 'abc'; + metadatabitstream.type = new ResourceType('item'); + metadatabitstream.fileInfo = []; + metadatabitstream.format = 'text'; + metadatabitstream.canPreview = false; + metadatabitstream._links = { + self: new HALLink(), + schema: new HALLink(), + }; + + metadatabitstream._links.self.href = ''; + metadatabitstream._links.schema.href = ''; + const metadataBitstreams: MetadataBitstream[] = [metadatabitstream]; + const bitstreamStream = new BehaviorSubject(metadataBitstreams); + beforeEach(async () => { + mockRegistryService = jasmine.createSpyObj('RegistryService', { + 'getMetadataBitstream': of(bitstreamStream) + } + ); + halService = Object.assign(new HALEndpointServiceStub('some url')); + await TestBed.configureTestingModule({ - declarations: [ ClarinFilesSectionComponent ] + declarations: [ ClarinFilesSectionComponent ], + imports: [ + TranslateModule.forRoot() + ], + providers: [ + { provide: RegistryService, useValue: mockRegistryService }, + { provide: Router, useValue: new RouterMock() }, + { provide: HALEndpointService, useValue: halService } + ], }) .compileComponents(); diff --git a/src/app/item-page/clarin-license-info/clarin-license-info.component.spec.ts b/src/app/item-page/clarin-license-info/clarin-license-info.component.spec.ts index e72b3e9da0d..3ccb05ad6e3 100644 --- a/src/app/item-page/clarin-license-info/clarin-license-info.component.spec.ts +++ b/src/app/item-page/clarin-license-info/clarin-license-info.component.spec.ts @@ -10,6 +10,7 @@ import { RouterTestingModule } from '@angular/router/testing'; import { ClarinLicenseDataService } from '../../core/data/clarin/clarin-license-data.service'; import { ItemMock } from '../../shared/mocks/item.mock'; import { MetadataValue } from '../../core/shared/metadata.models'; +import { LocaleService } from '../../core/locale/locale.service'; const item = ItemMock; const license = 'Test License Name'; @@ -22,6 +23,7 @@ describe('ClarinLicenseInfoComponent', () => { let clarinLicenseDataService: ClarinLicenseDataService; let sanitizerStub: DomSanitizer; + let localeService: LocaleService; // initialize license metadata item.metadata['dc.rights.label'] = [Object.assign(new MetadataValue(), { @@ -46,6 +48,9 @@ describe('ClarinLicenseInfoComponent', () => { sanitizerStub = jasmine.createSpyObj('sanitizer', { bypassSecurityTrustUrl: null }); + localeService = jasmine.createSpyObj('LocaleService', { + getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'), + }); await TestBed.configureTestingModule({ imports: [ @@ -59,6 +64,7 @@ describe('ClarinLicenseInfoComponent', () => { providers: [ { provide: ClarinLicenseDataService, useValue: clarinLicenseDataService }, { provide: DomSanitizer, useValue: sanitizerStub }, + { provide: LocaleService, useValue: localeService } ], }) .compileComponents(); diff --git a/src/app/item-page/full/full-item-page.component.spec.ts b/src/app/item-page/full/full-item-page.component.spec.ts index ef33231943e..bf41bd5d807 100644 --- a/src/app/item-page/full/full-item-page.component.spec.ts +++ b/src/app/item-page/full/full-item-page.component.spec.ts @@ -33,6 +33,7 @@ import { getMockTranslateService } from 'src/app/shared/mocks/translate.service. import { ConfigurationProperty } from '../../core/shared/configuration-property.model'; import { HALEndpointService } from '../../core/shared/hal-endpoint.service'; import { cold } from 'jasmine-marbles'; +import { ReplacePipe } from '../../shared/utils/replace.pipe'; const mockItem: Item = Object.assign(new Item(), { bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])), @@ -144,7 +145,7 @@ describe('FullItemPageComponent', () => { useClass: TranslateLoaderMock } }), RouterTestingModule.withRoutes([]), BrowserAnimationsModule], - declarations: [FullItemPageComponent, TruncatePipe, VarDirective], + declarations: [FullItemPageComponent, TruncatePipe, VarDirective, ReplacePipe], providers: [ { provide: ActivatedRoute, useValue: routeStub }, { provide: ItemDataService, useValue: {} }, diff --git a/src/app/item-page/simple/field-components/clarin-date-item-field/clarin-date-item-field.component.spec.ts b/src/app/item-page/simple/field-components/clarin-date-item-field/clarin-date-item-field.component.spec.ts index 0669a6d5c9a..3a5d2825dc0 100644 --- a/src/app/item-page/simple/field-components/clarin-date-item-field/clarin-date-item-field.component.spec.ts +++ b/src/app/item-page/simple/field-components/clarin-date-item-field/clarin-date-item-field.component.spec.ts @@ -1,19 +1,37 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ClarinDateItemFieldComponent } from './clarin-date-item-field.component'; +import { TranslateModule } from '@ngx-translate/core'; +import { Item } from '../../../../core/shared/item.model'; +import { createSuccessfulRemoteDataObject$ } from '../../../../shared/remote-data.utils'; +import { createPaginatedList } from '../../../../shared/testing/utils.test'; describe('ClarinDateItemFieldComponent', () => { let component: ClarinDateItemFieldComponent; let fixture: ComponentFixture; + const mockItem: Item = Object.assign(new Item(), { + bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])), + metadata: { + 'dc.identifier.uri': [ + { + language: 'en_US', + value: 'some handle' + } + ] + } + }); + beforeEach(async () => { await TestBed.configureTestingModule({ + imports: [ TranslateModule.forRoot() ], declarations: [ ClarinDateItemFieldComponent ] }) .compileComponents(); fixture = TestBed.createComponent(ClarinDateItemFieldComponent); component = fixture.componentInstance; + component.item = mockItem; fixture.detectChanges(); }); diff --git a/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts b/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts index 6bc482f095a..9eb04c1b3ae 100644 --- a/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts +++ b/src/app/item-page/simple/field-components/clarin-description-item-field/clarin-description-item-field.component.spec.ts @@ -1,11 +1,25 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; - import { ClarinDescriptionItemFieldComponent } from './clarin-description-item-field.component'; +import { Item } from '../../../../core/shared/item.model'; +import { createSuccessfulRemoteDataObject$ } from '../../../../shared/remote-data.utils'; +import { createPaginatedList } from '../../../../shared/testing/utils.test'; describe('ClarinDescriptionItemFieldComponent', () => { let component: ClarinDescriptionItemFieldComponent; let fixture: ComponentFixture; + const mockItem: Item = Object.assign(new Item(), { + bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])), + metadata: { + 'dc.identifier.uri': [ + { + language: 'en_US', + value: 'some handle' + } + ] + } + }); + beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ ClarinDescriptionItemFieldComponent ] @@ -14,6 +28,8 @@ describe('ClarinDescriptionItemFieldComponent', () => { fixture = TestBed.createComponent(ClarinDescriptionItemFieldComponent); component = fixture.componentInstance; + component.fields = ['dc.identifier.uri']; + component.item = mockItem; fixture.detectChanges(); }); diff --git a/src/app/item-page/simple/field-components/clarin-identifier-item-field/clarin-identifier-item-field.component.spec.ts b/src/app/item-page/simple/field-components/clarin-identifier-item-field/clarin-identifier-item-field.component.spec.ts index afc9d96c293..6e41581cb88 100644 --- a/src/app/item-page/simple/field-components/clarin-identifier-item-field/clarin-identifier-item-field.component.spec.ts +++ b/src/app/item-page/simple/field-components/clarin-identifier-item-field/clarin-identifier-item-field.component.spec.ts @@ -1,22 +1,58 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { ClarinIdentifierItemFieldComponent } from './clarin-identifier-item-field.component'; +import { ConfigurationDataService } from '../../../../core/data/configuration-data.service'; +import { NgbTooltipModule } from '@ng-bootstrap/ng-bootstrap'; +import { createSuccessfulRemoteDataObject$ } from '../../../../shared/remote-data.utils'; +import { ConfigurationProperty } from '../../../../core/shared/configuration-property.model'; +import { Item } from '../../../../core/shared/item.model'; +import { createPaginatedList } from '../../../../shared/testing/utils.test'; describe('ClarinIdentifierItemFieldComponent', () => { let component: ClarinIdentifierItemFieldComponent; let fixture: ComponentFixture; + const mockItem: Item = Object.assign(new Item(), { + bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])), + metadata: { + 'dc.identifier.uri': [ + { + language: 'en_US', + value: 'some handle' + } + ] + } + }); + beforeEach(async () => { + const configurationServiceSpy = jasmine.createSpyObj('configurationService', { + findByPropertyName: createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { + name: 'test', + values: [ + true + ] + })), + }); + await TestBed.configureTestingModule({ - declarations: [ ClarinIdentifierItemFieldComponent ] + imports: [ + NgbTooltipModule, + ], + declarations: [ ClarinIdentifierItemFieldComponent ], + providers: [ + { provide: ConfigurationDataService, useValue: configurationServiceSpy } + ] }) .compileComponents(); fixture = TestBed.createComponent(ClarinIdentifierItemFieldComponent); component = fixture.componentInstance; + component.item = mockItem; fixture.detectChanges(); }); - it('should create', () => { - expect(component).toBeTruthy(); - }); + it('should create', waitForAsync(() => { + fixture.whenStable().then(() => { + expect(component).toBeTruthy(); + }); + })); }); diff --git a/src/app/item-page/simple/item-page.component.spec.ts b/src/app/item-page/simple/item-page.component.spec.ts index 6a2ef384a90..6718bc20b46 100644 --- a/src/app/item-page/simple/item-page.component.spec.ts +++ b/src/app/item-page/simple/item-page.component.spec.ts @@ -295,7 +295,7 @@ describe('ItemPageComponent', () => { fixture.detectChanges(); void fixture.whenStable().then(() => { - const objectLoader = fixture.debugElement.query(By.css('ds-clarin-license-info')); + const objectLoader = fixture.debugElement.query(By.css('ds-clarin-files-section')); expect(objectLoader.nativeElement).toBeDefined(); }); })); diff --git a/src/app/shared/clarin-shared-util.ts b/src/app/shared/clarin-shared-util.ts index 1613bb9703b..e6f51835d9f 100644 --- a/src/app/shared/clarin-shared-util.ts +++ b/src/app/shared/clarin-shared-util.ts @@ -80,5 +80,5 @@ export function loadItemAuthors(item, itemAuthors, baseUrl) { export function makeLinks(text: string): string { // Use a regular expression to find URLs and convert them into clickable links const regex = /(?:https?|ftp):\/\/[^\s)]+|www\.[^\s)]+/g; - return text.replace(regex, (url) => `${url}`); + return text?.replace(regex, (url) => `${url}`); }