Skip to content

Commit a51e5c2

Browse files
[DURACOM-444] adapt tests for group component
1 parent 7501939 commit a51e5c2

2 files changed

Lines changed: 82 additions & 133 deletions

File tree

src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/dynamic-relation-group.component.spec.ts

Lines changed: 79 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ import {
3434
NgbModule,
3535
NgbTooltip,
3636
} from '@ng-bootstrap/ng-bootstrap';
37-
import { DYNAMIC_FORM_CONTROL_MAP_FN, DynamicFormLayoutService, DynamicFormValidationService } from '@ng-dynamic-forms/core';
37+
import {
38+
DYNAMIC_FORM_CONTROL_MAP_FN,
39+
DynamicFormLayoutService,
40+
DynamicFormValidationService,
41+
} from '@ng-dynamic-forms/core';
42+
import { provideMockActions } from '@ngrx/effects/testing';
3843
import { provideMockStore } from '@ngrx/store/testing';
3944
import { TranslateModule } from '@ngx-translate/core';
4045
import { MockComponent } from 'ng-mocks';
46+
import { Observable } from 'rxjs';
4147
import { ChipsComponent } from 'src/app/shared/form/chips/chips.component';
4248
import { ThemedLoadingComponent } from 'src/app/shared/loading/themed-loading.component';
4349

@@ -52,15 +58,11 @@ import { FormService } from '../../../../form.service';
5258
import { FormBuilderService } from '../../../form-builder.service';
5359
import { dsDynamicFormControlMapFn } from '../../ds-dynamic-form-control-map-fn';
5460
import { DsDynamicTypeBindRelationService } from '../../ds-dynamic-type-bind-relation.service';
55-
import { DsDynamicInputModel } from '../ds-dynamic-input.model';
5661
import { DsDynamicRelationGroupComponent } from './dynamic-relation-group.components';
5762
import {
5863
DynamicRelationGroupModel,
5964
DynamicRelationGroupModelConfig,
6065
} from './dynamic-relation-group.model';
61-
import { provideMockActions } from "@ngrx/effects/testing";
62-
import { Observable } from "rxjs";
63-
import { SubmissionFormsModel } from "@dspace/core/config/models/config-submission-forms.model";
6466

6567
export let FORM_GROUP_TEST_MODEL_CONFIG;
6668

@@ -192,12 +194,8 @@ describe('DsDynamicRelationGroupComponent test suite', () => {
192194
let vocabularyServiceStub: any;
193195
let modelValue: any;
194196
let html;
195-
let control1: UntypedFormControl;
196-
let model1: DsDynamicInputModel;
197-
let control2: UntypedFormControl;
198-
let model2: DsDynamicInputModel;
199197
let submissionServiceStub: SubmissionServiceStub;
200-
const vocabularyService: any = new VocabularyServiceStub()
198+
const vocabularyService: any = new VocabularyServiceStub();
201199

202200
// waitForAsync beforeEach
203201
beforeEach(waitForAsync(() => {
@@ -244,7 +242,7 @@ describe('DsDynamicRelationGroupComponent test suite', () => {
244242
remove: {
245243
imports: [
246244
ThemedLoadingComponent,
247-
FormComponent
245+
FormComponent,
248246
],
249247
},
250248
})
@@ -278,154 +276,103 @@ describe('DsDynamicRelationGroupComponent test suite', () => {
278276
}));
279277
});
280278

281-
describe('when init model value is empty', () => {
282-
beforeEach(inject([FormBuilderService], (service: FormBuilderService) => {
283-
groupFixture = TestBed.createComponent(DsDynamicRelationGroupComponent);
284-
groupComp = groupFixture.componentInstance; // FormComponent test instance
285-
groupComp.formId = 'testForm';
286-
groupComp.group = FORM_GROUP_TEST_GROUP;
287-
groupComp.model = new DynamicRelationGroupModel(FORM_GROUP_TEST_MODEL_CONFIG);
288-
groupFixture.detectChanges();
289-
control1 = service.getFormControlById('dc_contributor_author', (groupComp as any).formRef.formGroup, groupComp.formModel) as UntypedFormControl;
290-
model1 = service.findById('dc_contributor_author', groupComp.formModel) as DsDynamicInputModel;
291-
control2 = service.getFormControlById('local_contributor_affiliation', (groupComp as any).formRef.formGroup, groupComp.formModel) as UntypedFormControl;
292-
model2 = service.findById('local_contributor_affiliation', groupComp.formModel) as DsDynamicInputModel;
279+
describe('when vocabulary has no external source option', () => {
280+
beforeEach(() => {
293281
spyOn(vocabularyService, 'findVocabularyById').and.returnValue(createSuccessfulRemoteDataObject$(vocabulary));
294-
// spyOn(store, 'dispatch');
295-
}));
296-
297-
afterEach(() => {
298-
groupFixture.destroy();
299-
groupComp = null;
300282
});
301283

302-
it('should init component properly', inject([FormBuilderService], (service: FormBuilderService) => {
303-
const formConfig = { rows: groupComp.model.formConfiguration } as SubmissionFormsModel;
304-
const formModel = service.modelFromConfiguration(submissionId, formConfig, groupComp.model.scopeUUID, {}, groupComp.model.submissionScope, groupComp.model.readOnly);
305-
const chips = new Chips([], 'value', 'dc.contributor.author');
306-
groupComp.formCollapsed.subscribe((value) => {
307-
expect(value).toEqual(false);
308-
});
309-
expect(groupComp.formModel.length).toEqual(formModel.length);
310-
expect(groupComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
311-
}));
284+
describe('when init model value is empty', () => {
312285

313-
it('should save a new chips item', () => {
314-
control1.setValue('test author');
315-
(model1 as any).value = new FormFieldMetadataValueObject('test author');
316-
control2.setValue('test affiliation');
317-
(model2 as any).value = new FormFieldMetadataValueObject('test affiliation');
318-
modelValue = [{
319-
'dc.contributor.author': new FormFieldMetadataValueObject('test author'),
320-
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation'),
321-
}];
322-
groupFixture.detectChanges();
323-
324-
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
325-
const btnEl = buttons[0];
326-
btnEl.click();
327-
328-
expect(groupComp.chips.getChipsItems()).toEqual(modelValue);
329-
groupComp.formCollapsed.subscribe((value) => {
330-
expect(value).toEqual(true);
331-
});
332-
});
286+
beforeEach(inject([FormBuilderService], (service: FormBuilderService) => {
287+
groupFixture = TestBed.createComponent(DsDynamicRelationGroupComponent);
288+
debugElement = groupFixture.debugElement;
289+
groupComp = groupFixture.componentInstance; // FormComponent test instance
290+
submissionServiceStub = TestBed.inject(SubmissionService as any);
291+
groupComp.group = FORM_GROUP_TEST_GROUP;
292+
groupComp.model = new DynamicRelationGroupModel(FORM_GROUP_TEST_MODEL_CONFIG);
293+
groupFixture.detectChanges();
333294

334-
it('should clear form inputs', () => {
335-
control1.setValue('test author');
336-
(model1 as any).value = new FormFieldMetadataValueObject('test author');
337-
control2.setValue('test affiliation');
338-
(model2 as any).value = new FormFieldMetadataValueObject('test affiliation');
295+
}));
339296

340-
groupFixture.detectChanges();
297+
afterEach(() => {
298+
groupFixture.destroy();
299+
groupComp = null;
300+
});
341301

342-
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
343-
const btnEl = buttons[2];
344-
btnEl.click();
302+
it('should init component properly', inject([FormBuilderService], (service: FormBuilderService) => {
303+
const chips = new Chips([], 'value', 'dc.contributor.author');
304+
expect(groupComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
305+
}));
345306

346-
expect(control1.value).toBeNull();
347-
expect(control2.value).toBeNull();
348-
groupComp.formCollapsed.subscribe((value) => {
349-
expect(value).toEqual(false);
350-
});
351-
});
352-
});
307+
it('should save a new chips item', () => {
308+
modelValue = [{
309+
'dc.contributor.author': new FormFieldMetadataValueObject('test author'),
310+
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation'),
311+
}];
353312

354-
describe('when init model value is not empty', () => {
355-
beforeEach(() => {
313+
const modalRef = groupComp.openModal();
314+
groupFixture.detectChanges();
356315

357-
groupFixture = TestBed.createComponent(DsDynamicRelationGroupComponent);
358-
groupComp = groupFixture.componentInstance; // FormComponent test instance
359-
groupComp.formId = 'testForm';
360-
groupComp.group = FORM_GROUP_TEST_GROUP;
361-
groupComp.model = new DynamicRelationGroupModel(FORM_GROUP_TEST_MODEL_CONFIG);
362-
modelValue = [{
363-
'dc.contributor.author': new FormFieldMetadataValueObject('test author'),
364-
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation'),
365-
}];
366-
groupComp.model.value = modelValue;
367-
groupFixture.detectChanges();
316+
modalRef.componentInstance.add.emit(modelValue[0]);
368317

369-
});
318+
expect(groupComp.chips.getChipsItems()).toEqual(modelValue);
370319

371-
afterEach(() => {
372-
groupFixture.destroy();
373-
groupComp = null;
320+
});
374321
});
375322

376-
it('should init component properly', inject([FormBuilderService], (service: FormBuilderService) => {
377-
const formConfig = { rows: groupComp.model.formConfiguration } as SubmissionFormsModel;
378-
const formModel = service.modelFromConfiguration(submissionId, formConfig, groupComp.model.scopeUUID, {}, groupComp.model.submissionScope, groupComp.model.readOnly);
379-
const chips = new Chips(modelValue, 'value', 'dc.contributor.author');
380-
groupComp.formCollapsed.subscribe((value) => {
381-
expect(value).toEqual(true);
323+
describe('when init model value is not empty', () => {
324+
beforeEach(() => {
325+
326+
groupFixture = TestBed.createComponent(DsDynamicRelationGroupComponent);
327+
debugElement = groupFixture.debugElement;
328+
groupComp = groupFixture.componentInstance; // FormComponent test instance
329+
submissionServiceStub = TestBed.inject(SubmissionService as any);
330+
groupComp.group = FORM_GROUP_TEST_GROUP;
331+
groupComp.model = new DynamicRelationGroupModel(FORM_GROUP_TEST_MODEL_CONFIG);
332+
modelValue = [{
333+
'dc.contributor.author': new FormFieldMetadataValueObject('test author'),
334+
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation'),
335+
}];
336+
groupComp.model.value = modelValue;
337+
groupFixture.detectChanges();
338+
382339
});
383-
expect(groupComp.formModel.length).toEqual(formModel.length);
384-
expect(groupComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
385-
}));
386340

387-
it('should modify existing chips item', inject([FormBuilderService], (service: FormBuilderService) => {
388-
groupComp.onChipSelected(0);
389-
groupFixture.detectChanges();
341+
afterEach(() => {
342+
groupFixture.destroy();
343+
groupComp = null;
344+
});
390345

391-
control1 = service.getFormControlById('dc_contributor_author', (groupComp as any).formRef.formGroup, groupComp.formModel) as UntypedFormControl;
392-
model1 = service.findById('dc_contributor_author', groupComp.formModel) as DsDynamicInputModel;
346+
it('should init component properly', inject([FormBuilderService], (service: FormBuilderService) => {
347+
const chips = new Chips(modelValue, 'value', 'dc.contributor.author');
348+
expect(groupComp.chips.getChipsItems()).toEqual(chips.getChipsItems());
349+
}));
393350

394-
control1.setValue('test author modify');
395-
(model1 as any).value = new FormFieldMetadataValueObject('test author modify');
351+
it('should modify existing chips item', inject([FormBuilderService], (service: FormBuilderService) => {
352+
const modalRef = groupComp.onChipSelected(0);
353+
groupFixture.detectChanges();
396354

397-
modelValue = [{
398-
'dc.contributor.author': new FormFieldMetadataValueObject('test author modify'),
399-
'local.contributor.affiliation': new FormFieldMetadataValueObject('test affiliation'),
400-
}];
401-
groupFixture.detectChanges();
355+
expect(modalRef.componentInstance.editMode).toBe(true);
356+
expect(modalRef.componentInstance.itemIndex).toBe(0);
357+
expect(modalRef.componentInstance.item).toBe(groupComp.chips.getChipByIndex(0).item);
402358

403-
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
404-
const btnEl = buttons[0];
405-
btnEl.click();
359+
spyOn(groupComp.chips, 'update').and.callThrough();
406360

407-
groupFixture.detectChanges();
361+
const newItemValue = {
362+
'dc.contributor.author': 'test author modified',
363+
'local.contributor.affiliation': 'test affiliation',
364+
};
365+
modalRef.componentInstance.edit.emit(newItemValue);
408366

409-
expect(groupComp.chips.getChipsItems()).toEqual(modelValue);
410-
groupComp.formCollapsed.subscribe((value) => {
411-
expect(value).toEqual(true);
412-
});
413-
}));
367+
groupFixture.detectChanges();
414368

415-
it('should delete existing chips item', () => {
416-
groupComp.onChipSelected(0);
417-
groupFixture.detectChanges();
369+
expect(groupComp.chips.update).toHaveBeenCalledWith(groupComp.selectedChipItem.id, newItemValue);
418370

419-
const buttons = groupFixture.debugElement.nativeElement.querySelectorAll('button');
420-
const btnEl = buttons[1];
421-
btnEl.click();
371+
}));
422372

423-
expect(groupComp.chips.getChipsItems()).toEqual([]);
424-
groupComp.formCollapsed.subscribe((value) => {
425-
expect(value).toEqual(false);
426-
});
427373
});
428374
});
375+
429376
});
430377

431378
// declare a test component

src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-inline-group/dynamic-relation-inline-group.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Load the implementations that should be tested
21
import { HttpClient } from '@angular/common/http';
32
import {
43
ChangeDetectorRef,
@@ -36,8 +35,10 @@ import {
3635
DynamicFormLayoutService,
3736
DynamicFormValidationService,
3837
} from '@ng-dynamic-forms/core';
38+
import { provideMockActions } from '@ngrx/effects/testing';
3939
import { provideMockStore } from '@ngrx/store/testing';
4040
import { TranslateModule } from '@ngx-translate/core';
41+
import { Observable } from 'rxjs';
4142

4243
import { environment } from '../../../../../../../environments/environment.test';
4344
import { SubmissionService } from '../../../../../../submission/submission.service';
@@ -189,6 +190,7 @@ describe('DsDynamicRelationInlineGroupComponent test suite', () => {
189190
FormComponent,
190191
FormService,
191192
provideMockStore({ initialState }),
193+
provideMockActions(() => new Observable<any>()),
192194
{ provide: SubmissionService, useValue: submissionServiceStub },
193195
{ provide: XSRFService, useValue: {} },
194196
{ provide: LiveRegionService, useValue: {} },

0 commit comments

Comments
 (0)