Skip to content

Commit

Permalink
fixed failing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
milanmajchrak committed Mar 6, 2024
1 parent 4c72bd5 commit 2802841
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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<ClarinFilesSectionComponent>;

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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(), {
Expand All @@ -46,6 +48,9 @@ describe('ClarinLicenseInfoComponent', () => {
sanitizerStub = jasmine.createSpyObj('sanitizer', {
bypassSecurityTrustUrl: null
});
localeService = jasmine.createSpyObj('LocaleService', {
getCurrentLanguageCode: jasmine.createSpy('getCurrentLanguageCode'),
});

await TestBed.configureTestingModule({
imports: [
Expand All @@ -59,6 +64,7 @@ describe('ClarinLicenseInfoComponent', () => {
providers: [
{ provide: ClarinLicenseDataService, useValue: clarinLicenseDataService },
{ provide: DomSanitizer, useValue: sanitizerStub },
{ provide: LocaleService, useValue: localeService }
],
})
.compileComponents();
Expand Down
3 changes: 2 additions & 1 deletion src/app/item-page/full/full-item-page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])),
Expand Down Expand Up @@ -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: {} },
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ClarinDateItemFieldComponent>;

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();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ClarinDescriptionItemFieldComponent>;

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 ]
Expand All @@ -14,6 +28,8 @@ describe('ClarinDescriptionItemFieldComponent', () => {

fixture = TestBed.createComponent(ClarinDescriptionItemFieldComponent);
component = fixture.componentInstance;
component.fields = ['dc.identifier.uri'];
component.item = mockItem;
fixture.detectChanges();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ClarinIdentifierItemFieldComponent>;

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();
});
}));
});
2 changes: 1 addition & 1 deletion src/app/item-page/simple/item-page.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}));
Expand Down
2 changes: 1 addition & 1 deletion src/app/shared/clarin-shared-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => `<a href="${url}" target="_blank">${url}</a>`);
return text?.replace(regex, (url) => `<a href="${url}" target="_blank">${url}</a>`);
}

0 comments on commit 2802841

Please sign in to comment.