[CST-5668] Add test

This commit is contained in:
Giuseppe Digilio
2022-06-13 10:39:44 +02:00
parent 98f1baea2f
commit a915659cc9
3 changed files with 262 additions and 8 deletions

View File

@@ -9,7 +9,7 @@
</ds-alert>
<div class="row mb-3">
<div class="col-md">
<div class="card">
<div class="card" data-test="sync-mode">
<div class="card-header">{{ 'person.page.orcid.synchronization-mode'| translate }}</div>
<div class="card-body">
<div class="container">
@@ -28,7 +28,7 @@
</div>
<div class="row mb-3">
<div class="col-md">
<div class="card">
<div class="card" data-test="sync-mode-publication">
<div class="card-header">{{ 'person.page.orcid.publications-preferences'| translate }}</div>
<div class="card-body">
<div class="m-3">
@@ -46,7 +46,7 @@
</div>
</div>
<div class="col-md">
<div class="card">
<div class="card" data-test="sync-mode-funding">
<div class="card-header">{{ 'person.page.orcid.funding-preferences'| translate }}</div>
<div class="card-body">
<div class="m-3">
@@ -63,7 +63,7 @@
</div>
</div>
<div class="col-md">
<div class="card">
<div class="card" data-test="profile-preferences">
<div class="card-header">{{ 'person.page.orcid.profile-preferences'| translate }}</div>
<div class="card-body">
<div class="m-3">

View File

@@ -0,0 +1,257 @@
import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { By } from '@angular/platform-browser';
import { getTestScheduler } from 'jasmine-marbles';
import { NgbAccordionModule } from '@ng-bootstrap/ng-bootstrap';
import { TestScheduler } from 'rxjs/testing';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { Operation } from 'fast-json-patch';
import { ResearcherProfileService } from '../../../core/profile/researcher-profile.service';
import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils';
import { Item } from '../../../core/shared/item.model';
import { createPaginatedList } from '../../../shared/testing/utils.test';
import { TranslateLoaderMock } from '../../../shared/mocks/translate-loader.mock';
import { NotificationsService } from '../../../shared/notifications/notifications.service';
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
import { OrcidSyncSettingsComponent } from './orcid-sync-settings.component';
import { ResearcherProfile } from '../../../core/profile/model/researcher-profile.model';
describe('OrcidAuthComponent test suite', () => {
let comp: OrcidSyncSettingsComponent;
let fixture: ComponentFixture<OrcidSyncSettingsComponent>;
let scheduler: TestScheduler;
let researcherProfileService: jasmine.SpyObj<ResearcherProfileService>;
let notificationsService;
let formGroup: FormGroup;
const mockResearcherProfile: ResearcherProfile = Object.assign(new ResearcherProfile(), {
id: 'test-id',
visible: true,
type: 'profile',
_links: {
item: {
href: 'https://rest.api/rest/api/profiles/test-id/item'
},
self: {
href: 'https://rest.api/rest/api/profiles/test-id'
},
}
});
const mockItemLinkedToOrcid: Item = Object.assign(new Item(), {
bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])),
metadata: {
'dc.title': [{
value: 'test person'
}],
'dspace.entity.type': [{
'value': 'Person'
}],
'dspace.object.owner': [{
'value': 'test person',
'language': null,
'authority': 'deced3e7-68e2-495d-bf98-7c44fc33b8ff',
'confidence': 600,
'place': 0
}],
'dspace.orcid.authenticated': [{
'value': '2022-06-10T15:15:12.952872',
'language': null,
'authority': null,
'confidence': -1,
'place': 0
}],
'dspace.orcid.scope': [{
'value': '/authenticate',
'language': null,
'authority': null,
'confidence': -1,
'place': 0
}, {
'value': '/read-limited',
'language': null,
'authority': null,
'confidence': -1,
'place': 1
}, {
'value': '/activities/update',
'language': null,
'authority': null,
'confidence': -1,
'place': 2
}, {
'value': '/person/update',
'language': null,
'authority': null,
'confidence': -1,
'place': 3
}],
'dspace.orcid.sync-mode': [{
'value': 'MANUAL',
'language': null,
'authority': null,
'confidence': -1,
'place': 0
}],
'dspace.orcid.sync-profile': [{
'value': 'BIOGRAPHICAL',
'language': null,
'authority': null,
'confidence': -1,
'place': 0
}, {
'value': 'IDENTIFIERS',
'language': null,
'authority': null,
'confidence': -1,
'place': 1
}],
'dspace.orcid.sync-publications': [{
'value': 'ALL',
'language': null,
'authority': null,
'confidence': -1,
'place': 0
}],
'person.identifier.orcid': [{
'value': 'orcid-id',
'language': null,
'authority': null,
'confidence': -1,
'place': 0
}]
}
});
beforeEach(waitForAsync(() => {
researcherProfileService = jasmine.createSpyObj('researcherProfileService', {
findByRelatedItem: jasmine.createSpy('findByRelatedItem'),
updateByOrcidOperations: jasmine.createSpy('updateByOrcidOperations')
});
void TestBed.configureTestingModule({
imports: [
FormsModule,
NgbAccordionModule,
ReactiveFormsModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useClass: TranslateLoaderMock
}
}),
RouterTestingModule.withRoutes([])
],
declarations: [OrcidSyncSettingsComponent],
providers: [
{ provide: NotificationsService, useClass: NotificationsServiceStub },
{ provide: ResearcherProfileService, useValue: researcherProfileService }
],
schemas: [NO_ERRORS_SCHEMA]
}).overrideComponent(OrcidSyncSettingsComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default }
}).compileComponents();
}));
beforeEach(waitForAsync(() => {
scheduler = getTestScheduler();
fixture = TestBed.createComponent(OrcidSyncSettingsComponent);
comp = fixture.componentInstance;
comp.item = mockItemLinkedToOrcid;
fixture.detectChanges();
}));
it('should create cards properly', () => {
const modes = fixture.debugElement.query(By.css('[data-test="sync-mode"]'));
const publication = fixture.debugElement.query(By.css('[data-test="sync-mode-publication"]'));
const funding = fixture.debugElement.query(By.css('[data-test="sync-mode-funding"]'));
const preferences = fixture.debugElement.query(By.css('[data-test="profile-preferences"]'));
expect(modes).toBeTruthy();
expect(publication).toBeTruthy();
expect(funding).toBeTruthy();
expect(preferences).toBeTruthy();
});
it('should init sync modes properly', () => {
expect(comp.currentSyncMode).toBe('MANUAL');
expect(comp.currentSyncPublications).toBe('ALL');
expect(comp.currentSyncFunding).toBe('DISABLED');
});
describe('form submit', () => {
beforeEach(() => {
scheduler = getTestScheduler();
notificationsService = (comp as any).notificationsService;
formGroup = new FormGroup({
syncMode: new FormControl('MANUAL'),
syncFundings: new FormControl('ALL'),
syncPublications: new FormControl('ALL'),
syncProfile_BIOGRAPHICAL: new FormControl(true),
syncProfile_IDENTIFIERS: new FormControl(true),
});
});
it('should call updateByOrcidOperations properly', () => {
researcherProfileService.findByRelatedItem.and.returnValue(createSuccessfulRemoteDataObject$(mockResearcherProfile));
researcherProfileService.updateByOrcidOperations.and.returnValue(createSuccessfulRemoteDataObject$(mockResearcherProfile));
const expectedOps: Operation[] = [
{
path: '/orcid/mode',
op: 'replace',
value: 'MANUAL'
}, {
path: '/orcid/publications',
op: 'replace',
value: 'ALL'
}, {
path: '/orcid/fundings',
op: 'replace',
value: 'ALL'
}, {
path: '/orcid/profile',
op: 'replace',
value: 'BIOGRAPHICAL,IDENTIFIERS'
}
];
scheduler.schedule(() => comp.onSubmit(formGroup));
scheduler.flush();
expect(researcherProfileService.updateByOrcidOperations).toHaveBeenCalledWith(mockResearcherProfile, expectedOps);
});
it('should show notification on success', () => {
researcherProfileService.findByRelatedItem.and.returnValue(createSuccessfulRemoteDataObject$(mockResearcherProfile));
researcherProfileService.updateByOrcidOperations.and.returnValue(createSuccessfulRemoteDataObject$(mockResearcherProfile));
scheduler.schedule(() => comp.onSubmit(formGroup));
scheduler.flush();
expect(notificationsService.success).toHaveBeenCalled();
});
it('should show notification on error', () => {
researcherProfileService.findByRelatedItem.and.returnValue(createFailedRemoteDataObject$());
scheduler.schedule(() => comp.onSubmit(formGroup));
scheduler.flush();
expect(notificationsService.error).toHaveBeenCalled();
});
it('should show notification on error', () => {
researcherProfileService.findByRelatedItem.and.returnValue(createSuccessfulRemoteDataObject$(mockResearcherProfile));
researcherProfileService.updateByOrcidOperations.and.returnValue(createFailedRemoteDataObject$());
scheduler.schedule(() => comp.onSubmit(formGroup));
scheduler.flush();
expect(notificationsService.error).toHaveBeenCalled();
});
});
});

View File

@@ -6,7 +6,6 @@ import { Operation } from 'fast-json-patch';
import { of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { AuthService } from '../../../core/auth/auth.service';
import { RemoteData } from '../../../core/data/remote-data';
import { ResearcherProfileService } from '../../../core/profile/researcher-profile.service';
import { Item } from '../../../core/shared/item.model';
@@ -68,10 +67,8 @@ export class OrcidSyncSettingsComponent implements OnInit {
constructor(private researcherProfileService: ResearcherProfileService,
protected translateService: TranslateService,
private notificationsService: NotificationsService,
public authService: AuthService
) {
private translateService: TranslateService) {
}
/**