Added test for ResourcePolicyEditComponent

This commit is contained in:
Giuseppe Digilio
2020-04-15 16:13:38 +02:00
parent 4ac3eb5f9b
commit b81d8ed900
2 changed files with 246 additions and 1 deletions

View File

@@ -0,0 +1,220 @@
import { async, ComponentFixture, inject, TestBed } from '@angular/core/testing';
import { ChangeDetectorRef, Component, Injector, NO_ERRORS_SCHEMA } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { cold, getTestScheduler } from 'jasmine-marbles';
import { of as observableOf } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { TranslateModule } from '@ngx-translate/core';
import {
createFailedRemoteDataObject,
createSuccessfulRemoteDataObject,
createTestComponent
} from '../../testing/utils';
import { LinkService } from '../../../core/cache/builders/link.service';
import { NotificationsService } from '../../notifications/notifications.service';
import { NotificationsServiceStub } from '../../testing/notifications-service-stub';
import { ResourcePolicyService } from '../../../core/resource-policy/resource-policy.service';
import { getMockResourcePolicyService } from '../../mocks/mock-resource-policy-service';
import { getMockLinkService } from '../../mocks/mock-link-service';
import { RouterStub } from '../../testing/router-stub';
import { ResourcePolicyEvent } from '../form/resource-policy-form.component';
import { GroupMock } from '../../testing/group-mock';
import { submittedResourcePolicy } from '../form/resource-policy-form.component.spec';
import { PolicyType } from '../../../core/resource-policy/models/policy-type.model';
import { ActionType } from '../../../core/resource-policy/models/action-type.model';
import { ResourcePolicyEditComponent } from './resource-policy-edit.component';
import { RESOURCE_POLICY } from '../../../core/resource-policy/models/resource-policy.resource-type';
describe('ResourcePolicyEditComponent test suite', () => {
let comp: ResourcePolicyEditComponent;
let compAsAny: any;
let fixture: ComponentFixture<ResourcePolicyEditComponent>;
let de;
let scheduler: TestScheduler;
let eventPayload: ResourcePolicyEvent;
let updatedObject;
const resourcePolicy: any = {
id: '1',
name: null,
description: null,
policyType: PolicyType.TYPE_SUBMISSION,
action: ActionType.READ,
startDate: null,
endDate: null,
type: 'resourcepolicy',
uuid: 'resource-policy-1',
_links: {
eperson: {
href: 'https://rest.api/rest/api/eperson'
},
group: {
href: 'https://rest.api/rest/api/group'
},
self: {
href: 'https://rest.api/rest/api/resourcepolicies/1'
},
},
eperson: observableOf(createSuccessfulRemoteDataObject({})),
group: observableOf(createSuccessfulRemoteDataObject(GroupMock))
};
const resourcePolicyService: any = getMockResourcePolicyService();
const linkService: any = getMockLinkService();
const routeStub = {
data: observableOf({
resourcePolicy: createSuccessfulRemoteDataObject(resourcePolicy)
})
};
const routerStub = Object.assign(new RouterStub(), {
url: `url/edit`
});
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot()
],
declarations: [
ResourcePolicyEditComponent,
TestComponent
],
providers: [
{ provide: LinkService, useValue: linkService },
{ provide: ActivatedRoute, useValue: routeStub },
{ provide: NotificationsService, useValue: new NotificationsServiceStub() },
{ provide: ResourcePolicyService, useValue: resourcePolicyService },
{ provide: Router, useValue: routerStub },
ResourcePolicyEditComponent,
ChangeDetectorRef,
Injector
],
schemas: [
NO_ERRORS_SCHEMA
]
}).compileComponents();
}));
describe('', () => {
let testComp: TestComponent;
let testFixture: ComponentFixture<TestComponent>;
// synchronous beforeEach
beforeEach(() => {
const html = `
<ds-resource-policy-edit></ds-resource-policy-edit>`;
testFixture = createTestComponent(html, TestComponent) as ComponentFixture<TestComponent>;
testComp = testFixture.componentInstance;
});
afterEach(() => {
testFixture.destroy();
});
it('should create ResourcePolicyEditComponent', inject([ResourcePolicyEditComponent], (app: ResourcePolicyEditComponent) => {
expect(app).toBeDefined();
}));
});
describe('', () => {
beforeEach(() => {
// initTestScheduler();
fixture = TestBed.createComponent(ResourcePolicyEditComponent);
comp = fixture.componentInstance;
compAsAny = fixture.componentInstance;
});
afterEach(() => {
comp = null;
compAsAny = null;
de = null;
fixture.destroy();
});
it('should init component properly', () => {
fixture.detectChanges();
expect(compAsAny.resourcePolicy).toEqual(resourcePolicy);
});
it('should redirect to authorizations page', () => {
comp.redirectToAuthorizationsPage();
expect(compAsAny.router.navigate).toHaveBeenCalled();
});
it('should return true when is Processing', () => {
compAsAny.processing$.next(true);
expect(comp.isProcessing()).toBeObservable(cold('a', {
a: true
}));
});
it('should return false when is not Processing', () => {
compAsAny.processing$.next(false);
expect(comp.isProcessing()).toBeObservable(cold('a', {
a: false
}));
});
describe('', () => {
beforeEach(() => {
spyOn(comp, 'redirectToAuthorizationsPage').and.callThrough();
compAsAny.resourcePolicyService.update.and.returnValue(observableOf(createSuccessfulRemoteDataObject(resourcePolicy)));
compAsAny.targetResourceUUID = 'itemUUID';
eventPayload = Object.create({});
eventPayload.object = submittedResourcePolicy;
eventPayload.target = {
type: 'group',
uuid: GroupMock.id
};
compAsAny.resourcePolicy = resourcePolicy;
updatedObject = Object.assign({}, submittedResourcePolicy, {
id: resourcePolicy.id,
type: RESOURCE_POLICY.value,
_links: resourcePolicy._links
});
});
it('should notify success when update is successful', () => {
compAsAny.resourcePolicyService.update.and.returnValue(observableOf(createSuccessfulRemoteDataObject(resourcePolicy)));
scheduler = getTestScheduler();
scheduler.schedule(() => comp.updateResourcePolicy(eventPayload));
scheduler.flush();
expect(compAsAny.resourcePolicyService.update).toHaveBeenCalledWith(updatedObject);
expect(comp.redirectToAuthorizationsPage).toHaveBeenCalled();
});
it('should notify error when update is not successful', () => {
compAsAny.resourcePolicyService.update.and.returnValue(observableOf(createFailedRemoteDataObject({})));
scheduler = getTestScheduler();
scheduler.schedule(() => comp.updateResourcePolicy(eventPayload));
scheduler.flush();
expect(compAsAny.resourcePolicyService.update).toHaveBeenCalledWith(updatedObject);
expect(comp.redirectToAuthorizationsPage).not.toHaveBeenCalled();
});
});
});
});
// declare a test component
@Component({
selector: 'ds-test-cmp',
template: ``
})
class TestComponent {
}

View File

@@ -9,7 +9,7 @@ import { ResourcePolicyService } from '../../../core/resource-policy/resource-po
import { NotificationsService } from '../../notifications/notifications.service';
import { RemoteData } from '../../../core/data/remote-data';
import { ResourcePolicy } from '../../../core/resource-policy/models/resource-policy.model';
import { ResourcePolicyEvent } from '../form/resource-policy-form';
import { ResourcePolicyEvent } from '../form/resource-policy-form.component';
import { ITEM_EDIT_AUTHORIZATIONS_PATH } from '../../../+item-page/edit-item-page/edit-item-page.routing.module';
import { RESOURCE_POLICY } from '../../../core/resource-policy/models/resource-policy.resource-type';
@@ -30,6 +30,15 @@ export class ResourcePolicyEditComponent implements OnInit {
*/
private processing$ = new BehaviorSubject<boolean>(false);
/**
* Initialize instance variables
*
* @param {NotificationsService} notificationsService
* @param {ResourcePolicyService} resourcePolicyService
* @param {ActivatedRoute} route
* @param {Router} router
* @param {TranslateService} translate
*/
constructor(
private notificationsService: NotificationsService,
private resourcePolicyService: ResourcePolicyService,
@@ -38,6 +47,9 @@ export class ResourcePolicyEditComponent implements OnInit {
private translate: TranslateService) {
}
/**
* Initialize the component
*/
ngOnInit(): void {
this.route.data.pipe(
map((data) => data),
@@ -47,14 +59,27 @@ export class ResourcePolicyEditComponent implements OnInit {
});
}
/**
* Return a boolean representing if an operation is pending
*
* @return {Observable<boolean>}
*/
isProcessing(): Observable<boolean> {
return this.processing$.asObservable();
}
/**
* Redirect to the authorizations page
*/
redirectToAuthorizationsPage() {
this.router.navigate([`../../${ITEM_EDIT_AUTHORIZATIONS_PATH}`], { relativeTo: this.route });
}
/**
* Update a resource policy
*
* @param event The {{ResourcePolicyEvent}} emitted
*/
updateResourcePolicy(event: ResourcePolicyEvent) {
this.processing$.next(true);
const updatedObject = Object.assign({}, event.object, {