From 1e56f0378502274eba66dc8a98053d16eab9afb1 Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Fri, 12 Jul 2019 17:43:19 +0200 Subject: [PATCH] 63669: ContentSource tests pt1 --- .../collection-source.component.spec.ts | 158 ++++++++++++++++++ .../collection-source.component.ts | 30 ++-- 2 files changed, 174 insertions(+), 14 deletions(-) create mode 100644 src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.spec.ts diff --git a/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.spec.ts b/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.spec.ts new file mode 100644 index 0000000000..454634ebfc --- /dev/null +++ b/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.spec.ts @@ -0,0 +1,158 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { TranslateModule } from '@ngx-translate/core'; +import { RouterTestingModule } from '@angular/router/testing'; +import { ActivatedRoute, Router } from '@angular/router'; +import { of as observableOf } from 'rxjs/internal/observable/of'; +import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { CollectionSourceComponent } from './collection-source.component'; +import { ContentSource } from '../../../core/shared/content-source.model'; +import { ObjectUpdatesService } from '../../../core/data/object-updates/object-updates.service'; +import { INotification, Notification } from '../../../shared/notifications/models/notification.model'; +import { NotificationType } from '../../../shared/notifications/models/notification-type'; +import { FieldUpdate } from '../../../core/data/object-updates/object-updates.reducer'; +import { NotificationsService } from '../../../shared/notifications/notifications.service'; +import { DynamicFormControlModel, DynamicFormService } from '@ng-dynamic-forms/core'; +import { hasValue } from '../../../shared/empty.util'; +import { FormControl, FormGroup } from '@angular/forms'; +import { RouterStub } from '../../../shared/testing/router-stub'; +import { GLOBAL_CONFIG } from '../../../../config'; +import { By } from '@angular/platform-browser'; + +const infoNotification: INotification = new Notification('id', NotificationType.Info, 'info'); +const warningNotification: INotification = new Notification('id', NotificationType.Warning, 'warning'); +const successNotification: INotification = new Notification('id', NotificationType.Success, 'success'); + +const uuid = '29481ed7-ae6b-409a-8c51-34dd347a0ce4'; +let date: Date; +let contentSource: ContentSource; +let fieldUpdate: FieldUpdate; +let objectUpdatesService: ObjectUpdatesService; +let notificationsService: NotificationsService; +let location: Location; +let formService: DynamicFormService; +let router: Router; + +describe('CollectionSourceComponent', () => { + let comp: CollectionSourceComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + date = new Date(); + contentSource = Object.assign(new ContentSource(), { + uuid: uuid + }); + fieldUpdate = { + field: contentSource, + changeType: undefined + }; + objectUpdatesService = jasmine.createSpyObj('objectUpdatesService', + { + getFieldUpdates: observableOf({ + [contentSource.uuid]: fieldUpdate + }), + saveAddFieldUpdate: {}, + discardFieldUpdates: {}, + reinstateFieldUpdates: observableOf(true), + initialize: {}, + getUpdatedFields: observableOf([contentSource]), + getLastModified: observableOf(date), + hasUpdates: observableOf(true), + isReinstatable: observableOf(false), + isValidPage: observableOf(true) + } + ); + notificationsService = jasmine.createSpyObj('notificationsService', + { + info: infoNotification, + warning: warningNotification, + success: successNotification + } + ); + location = jasmine.createSpyObj('location', ['back']); + formService = Object.assign({ + createFormGroup: (fModel: DynamicFormControlModel[]) => { + const controls = {}; + if (hasValue(fModel)) { + fModel.forEach((controlModel) => { + controls[controlModel.id] = new FormControl((controlModel as any).value); + }); + return new FormGroup(controls); + } + return undefined; + } + }); + router = Object.assign(new RouterStub(), { + url: 'http://test-url.com/test-url' + }); + + TestBed.configureTestingModule({ + imports: [TranslateModule.forRoot(), RouterTestingModule], + declarations: [CollectionSourceComponent], + providers: [ + { provide: ObjectUpdatesService, useValue: objectUpdatesService }, + { provide: NotificationsService, useValue: notificationsService }, + { provide: Location, useValue: location }, + { provide: DynamicFormService, useValue: formService }, + { provide: ActivatedRoute, useValue: { parent: { data: observableOf({ dso: { payload: {} } }) } } }, + { provide: Router, useValue: router }, + { provide: GLOBAL_CONFIG, useValue: { collection: { edit: { undoTimeout: 10 } } } as any }, + ], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(CollectionSourceComponent); + comp = fixture.componentInstance; + comp.contentSource = contentSource; + fixture.detectChanges(); + }); + + describe('on startup', () => { + it('ContentSource should be disabled', () => { + expect(comp.contentSource.enabled).toBe(false); + }); + + it('the input-form should be disabled', () => { + expect(comp.formGroup.disabled).toBe(true); + }); + }); + + describe('when selecting the checkbox', () => { + let input; + + beforeEach(() => { + input = fixture.debugElement.query(By.css('#externalSourceCheck')).nativeElement; + input.click(); + fixture.detectChanges(); + }); + + it('should enable ContentSource', () => { + expect(comp.contentSource.enabled).toBe(true); + }); + + it('should send a field update', () => { + expect(objectUpdatesService.saveAddFieldUpdate).toHaveBeenCalledWith(router.url, comp.contentSource) + }); + }); + + describe('isValid', () => { + it('should return true when ContentSource is disabled but the form invalid', () => { + spyOnProperty(comp.formGroup, 'valid').and.returnValue(false); + comp.contentSource.enabled = false; + expect(comp.isValid()).toBe(true); + }); + + it('should return false when ContentSource is enabled but the form is invalid', () => { + spyOnProperty(comp.formGroup, 'valid').and.returnValue(false); + comp.contentSource.enabled = true; + expect(comp.isValid()).toBe(false); + }); + + it('should return true when ContentSource is enabled and the form is valid', () => { + spyOnProperty(comp.formGroup, 'valid').and.returnValue(true); + comp.contentSource.enabled = true; + expect(comp.isValid()).toBe(true); + }); + }); +}); diff --git a/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.ts b/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.ts index 68e54c8a8d..2f82f17254 100644 --- a/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.ts +++ b/src/app/+collection-page/edit-collection-page/collection-source/collection-source.component.ts @@ -229,20 +229,22 @@ export class CollectionSourceComponent extends AbstractTrackableComponent implem map((updates: FieldUpdates) => updates[initialContentSource.uuid]) ); this.updateSub = this.update$.subscribe((update: FieldUpdate) => { - const field = update.field as ContentSource; - this.formGroup.patchValue({ - providerContainer: { - provider: field.provider - }, - setContainer: { - set: field.set, - format: field.format - }, - harvestContainer: { - harvest: field.harvest - } - }); - this.contentSource = cloneDeep(field); + if (update) { + const field = update.field as ContentSource; + this.formGroup.patchValue({ + providerContainer: { + provider: field.provider + }, + setContainer: { + set: field.set, + format: field.format + }, + harvestContainer: { + harvest: field.harvest + } + }); + this.contentSource = cloneDeep(field); + } this.switchEnableForm(); }); }