From b81d8ed900f5ae0c41398a19e78519b8e2e15534 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Wed, 15 Apr 2020 16:13:38 +0200 Subject: [PATCH] Added test for ResourcePolicyEditComponent --- .../resource-policy-edit.component.spec.ts | 220 ++++++++++++++++++ .../edit/resource-policy-edit.component.ts | 27 ++- 2 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 src/app/shared/resource-policies/edit/resource-policy-edit.component.spec.ts diff --git a/src/app/shared/resource-policies/edit/resource-policy-edit.component.spec.ts b/src/app/shared/resource-policies/edit/resource-policy-edit.component.spec.ts new file mode 100644 index 0000000000..56099aac3d --- /dev/null +++ b/src/app/shared/resource-policies/edit/resource-policy-edit.component.spec.ts @@ -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; + 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; + + // synchronous beforeEach + beforeEach(() => { + const html = ` + `; + + testFixture = createTestComponent(html, TestComponent) as ComponentFixture; + 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 { + +} diff --git a/src/app/shared/resource-policies/edit/resource-policy-edit.component.ts b/src/app/shared/resource-policies/edit/resource-policy-edit.component.ts index 20f2a5a34e..e3927e7fcd 100644 --- a/src/app/shared/resource-policies/edit/resource-policy-edit.component.ts +++ b/src/app/shared/resource-policies/edit/resource-policy-edit.component.ts @@ -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(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} + */ isProcessing(): Observable { 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, {