1
0

[CST-4499] Version history - Tests WIP

This commit is contained in:
Davide Negretti
2021-09-21 12:07:27 +02:00
parent 3255a29b30
commit 62fea1fa78
2 changed files with 41 additions and 27 deletions

View File

@@ -2,73 +2,95 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { DsoPageVersionButtonComponent } from './dso-page-version-button.component'; import { DsoPageVersionButtonComponent } from './dso-page-version-button.component';
import { Item } from '../../../core/shared/item.model'; import { Item } from '../../../core/shared/item.model';
import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service'; import { AuthorizationDataService } from '../../../core/data/feature-authorization/authorization-data.service';
import { of as observableOf } from 'rxjs'; import { Observable, of, of as observableOf } from 'rxjs';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { FeatureID } from '../../../core/data/feature-authorization/feature-id'; import { FeatureID } from '../../../core/data/feature-authorization/feature-id';
import { By } from '@angular/platform-browser'; import { By } from '@angular/platform-browser';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { VersionHistoryDataService } from '../../../core/data/version-history-data.service';
describe('DsoPageVersionButtonComponent', () => { describe('DsoPageVersionButtonComponent', () => {
let component: DsoPageVersionButtonComponent; let component: DsoPageVersionButtonComponent;
let fixture: ComponentFixture<DsoPageVersionButtonComponent>; let fixture: ComponentFixture<DsoPageVersionButtonComponent>;
let authorizationService: AuthorizationDataService; let authorizationService: AuthorizationDataService;
let versionHistoryService: VersionHistoryDataService;
let dso: Item; let dso: Item;
let tooltipMsg: Observable<string>;
const authorizationServiceSpy = jasmine.createSpyObj('authorizationService', ['isAuthorized']);
const versionHistoryServiceSpy = jasmine.createSpyObj('versionHistoryService',
['getVersions', 'getLatestVersionFromHistory$', 'isLatest$', 'hasDraftVersion$']
);
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
dso = Object.assign(new Item(), { dso = Object.assign(new Item(), {
id: 'test-item', id: 'test-item',
_links: { _links: {
self: { href: 'test-item-selflink' } self: { href: 'test-item-selflink' },
} version: { href: 'test-item-version-selflink' },
}); },
authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: observableOf(true)
}); });
tooltipMsg = of('tooltip-msg');
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [DsoPageVersionButtonComponent], declarations: [DsoPageVersionButtonComponent],
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NgbModule], imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([]), NgbModule],
providers: [ providers: [
{ provide: AuthorizationDataService, useValue: authorizationService } { provide: AuthorizationDataService, useValue: authorizationServiceSpy },
{ provide: VersionHistoryDataService, useValue: versionHistoryServiceSpy },
] ]
}).compileComponents(); }).compileComponents();
authorizationService = TestBed.inject(AuthorizationDataService);
versionHistoryService = TestBed.inject(VersionHistoryDataService);
versionHistoryServiceSpy.hasDraftVersion$.and.returnValue(observableOf(true));
})); }));
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(DsoPageVersionButtonComponent); fixture = TestBed.createComponent(DsoPageVersionButtonComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
component.dso = dso; component.dso = dso;
component.tooltipMsg$ = tooltipMsg;
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should check the authorization of the current user', () => { it('should check the authorization of the current user', () => {
expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.CanEditMetadata, dso.self); expect(authorizationService.isAuthorized).toHaveBeenCalledWith(FeatureID.CanCreateVersion, dso.self);
});
it('should check if the item has a draft version', () => {
expect(versionHistoryServiceSpy.hasDraftVersion$).toHaveBeenCalledWith(dso._links.version.href);
}); });
describe('when the user is authorized', () => { describe('when the user is authorized', () => {
beforeEach(() => { beforeEach(() => {
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(true)); authorizationServiceSpy.isAuthorized.and.returnValue(observableOf(true));
component.ngOnInit(); component.ngOnInit();
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should render a link', () => { it('should render a button', () => {
const link = fixture.debugElement.query(By.css('a')); const button = fixture.debugElement.query(By.css('button'));
expect(link).not.toBeNull(); expect(button).not.toBeNull();
}); });
}); });
describe('when the user is not authorized', () => { describe('when the user is not authorized', () => {
beforeEach(() => { beforeEach(() => {
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false)); authorizationServiceSpy.isAuthorized.and.returnValue(observableOf(false));
component.ngOnInit(); component.ngOnInit();
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should not render a link', () => { it('should render a button', () => {
const link = fixture.debugElement.query(By.css('a')); const button = fixture.debugElement.query(By.css('button'));
expect(link).toBeNull(); expect(button).toBeNull();
}); });
}); });
}); });

View File

@@ -52,20 +52,12 @@ describe('ItemVersionsNoticeComponent', () => {
firstVersion.item = createSuccessfulRemoteDataObject$(firstItem); firstVersion.item = createSuccessfulRemoteDataObject$(firstItem);
latestVersion.item = createSuccessfulRemoteDataObject$(latestItem); latestVersion.item = createSuccessfulRemoteDataObject$(latestItem);
// versionHistoryService.getLatestVersionFromHistory$.createSpy('getLatestVersionFromHistory$').and.callFake((id) => { const versionHistoryServiceSpy = jasmine.createSpyObj('versionHistoryService',
// return of(latestVersion); ['getVersions', 'getLatestVersionFromHistory$', 'isLatest$', ]
// }); );
// versionHistoryService.isLatest$.createSpy('isLatest$').and.callFake((id) => {
// return of(true);
// });
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
const versionHistoryServiceSpy = jasmine.createSpyObj('versionHistoryService',
['getVersions', 'getLatestVersionFromHistory$', 'isLatest$', ]
);
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [ItemVersionsNoticeComponent], declarations: [ItemVersionsNoticeComponent],
imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])],