forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathds-dynamic-form-control-container.component.spec.ts
More file actions
497 lines (434 loc) · 19.1 KB
/
ds-dynamic-form-control-container.component.spec.ts
File metadata and controls
497 lines (434 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import {
CUSTOM_ELEMENTS_SCHEMA,
DebugElement,
NgZone,
SimpleChange,
} from '@angular/core';
import {
ComponentFixture,
inject,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import {
FormsModule,
ReactiveFormsModule,
UntypedFormControl,
UntypedFormGroup,
} from '@angular/forms';
import { By } from '@angular/platform-browser';
import { APP_CONFIG } from '@dspace/config/app-config.interface';
import { RelationshipDataService } from '@dspace/core/data/relationship-data.service';
import { APP_DATA_SERVICES_MAP } from '@dspace/core/data-services-map-type';
import { Item } from '@dspace/core/shared/item.model';
import { WorkspaceItem } from '@dspace/core/submission/models/workspaceitem.model';
import { VocabularyOptions } from '@dspace/core/submission/vocabularies/models/vocabulary-options.model';
import { createSuccessfulRemoteDataObject } from '@dspace/core/utilities/remote-data.utils';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import {
DYNAMIC_FORM_CONTROL_MAP_FN,
DynamicCheckboxGroupModel,
DynamicCheckboxModel,
DynamicColorPickerModel,
DynamicDatePickerModel,
DynamicEditorModel,
DynamicFileUploadModel,
DynamicFormArrayModel,
DynamicFormControlModel,
DynamicFormGroupModel,
DynamicFormsCoreModule,
DynamicFormService,
DynamicInputModel,
DynamicRadioGroupModel,
DynamicRatingModel,
DynamicSelectModel,
DynamicSliderModel,
DynamicSwitchModel,
DynamicTextAreaModel,
DynamicTimePickerModel,
MATCH_VISIBLE,
OR_OPERATOR,
} from '@ng-dynamic-forms/core';
import {
DynamicNGBootstrapCalendarComponent,
DynamicNGBootstrapCheckboxComponent,
DynamicNGBootstrapCheckboxGroupComponent,
DynamicNGBootstrapInputComponent,
DynamicNGBootstrapRadioGroupComponent,
DynamicNGBootstrapSelectComponent,
DynamicNGBootstrapTextAreaComponent,
DynamicNGBootstrapTimePickerComponent,
} from '@ng-dynamic-forms/ui-ng-bootstrap';
import { Actions } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { TranslateModule } from '@ngx-translate/core';
import { provideEnvironmentNgxMask } from 'ngx-mask';
import {
of,
ReplaySubject,
} from 'rxjs';
import { environment } from '../../../../../environments/environment';
import {
SaveForLaterSubmissionFormErrorAction,
SaveSubmissionFormErrorAction,
SaveSubmissionFormSuccessAction,
SaveSubmissionSectionFormErrorAction,
SaveSubmissionSectionFormSuccessAction,
} from '../../../../submission/objects/submission-objects.actions';
import { SubmissionService } from '../../../../submission/submission.service';
import { SubmissionObjectService } from '../../../../submission/submission-object.service';
import { LiveRegionService } from '../../../live-region/live-region.service';
import { getLiveRegionServiceStub } from '../../../live-region/live-region.service.stub';
import { SelectableListService } from '../../../object-list/selectable-list/selectable-list.service';
import { getMockFormBuilderService } from '../../testing/form-builder-service.mock';
import { FormBuilderService } from '../form-builder.service';
import { DsDynamicFormControlContainerComponent } from './ds-dynamic-form-control-container.component';
import { dsDynamicFormControlMapFn } from './ds-dynamic-form-control-map-fn';
import { DsDynamicTypeBindRelationService } from './ds-dynamic-type-bind-relation.service';
import { DsDynamicFormArrayComponent } from './models/array-group/dynamic-form-array.component';
import { DsDatePickerComponent } from './models/date-picker/date-picker.component';
import { DynamicDsDatePickerModel } from './models/date-picker/date-picker.model';
import { DsDatePickerInlineComponent } from './models/date-picker-inline/dynamic-date-picker-inline.component';
import { DynamicQualdropModel } from './models/ds-dynamic-qualdrop.model';
import { DsDynamicFormGroupComponent } from './models/form-group/dynamic-form-group.component';
import { DsDynamicListComponent } from './models/list/dynamic-list.component';
import { DynamicListCheckboxGroupModel } from './models/list/dynamic-list-checkbox-group.model';
import { DynamicListRadioGroupModel } from './models/list/dynamic-list-radio-group.model';
import { DsDynamicLookupComponent } from './models/lookup/dynamic-lookup.component';
import { DynamicLookupModel } from './models/lookup/dynamic-lookup.model';
import { DynamicLookupNameModel } from './models/lookup/dynamic-lookup-name.model';
import { DsDynamicOneboxComponent } from './models/onebox/dynamic-onebox.component';
import { DynamicOneboxModel } from './models/onebox/dynamic-onebox.model';
import { DsDynamicRelationGroupComponent } from './models/relation-group/dynamic-relation-group.components';
import { DynamicRelationGroupModel } from './models/relation-group/dynamic-relation-group.model';
import { DsDynamicRelationInlineGroupComponent } from './models/relation-inline-group/dynamic-relation-inline-group.components';
import { DsDynamicScrollableDropdownComponent } from './models/scrollable-dropdown/dynamic-scrollable-dropdown.component';
import { DynamicScrollableDropdownModel } from './models/scrollable-dropdown/dynamic-scrollable-dropdown.model';
import { DsDynamicTagComponent } from './models/tag/dynamic-tag.component';
import { DynamicTagModel } from './models/tag/dynamic-tag.model';
function getMockDsDynamicTypeBindRelationService(): DsDynamicTypeBindRelationService {
return jasmine.createSpyObj('DsDynamicTypeBindRelationService', {
getRelatedFormModel: jasmine.createSpy('getRelatedFormModel'),
matchesCondition: jasmine.createSpy('matchesCondition'),
subscribeRelations: jasmine.createSpy('subscribeRelations'),
});
}
describe('DsDynamicFormControlContainerComponent test suite', () => {
const vocabularyOptions: VocabularyOptions = {
name: 'type_programme',
metadata: null,
scope: null,
closed: false,
};
const formModel = [
new DynamicCheckboxModel({ id: 'checkbox' }),
new DynamicCheckboxGroupModel({ id: 'checkboxGroup', group: [] }),
new DynamicColorPickerModel({ id: 'colorpicker' }),
new DynamicDatePickerModel({ id: 'datepicker' }),
new DynamicEditorModel({ id: 'editor' }),
new DynamicFileUploadModel({ id: 'upload', url: '' }),
new DynamicFormArrayModel({ id: 'formArray', groupFactory: () => [] }),
new DynamicFormGroupModel({ id: 'formGroup', group: [] }),
new DynamicInputModel({ id: 'input', maxLength: 51 }),
new DynamicRadioGroupModel({ id: 'radioGroup' }),
new DynamicRatingModel({ id: 'rating' }),
new DynamicSelectModel({
id: 'select',
options: [{ value: 'One' }, { value: 'Two' }],
value: 'One',
}),
new DynamicSliderModel({ id: 'slider' }),
new DynamicSwitchModel({ id: 'switch' }),
new DynamicTextAreaModel({ id: 'textarea' }),
new DynamicTimePickerModel({ id: 'timepicker' }),
new DynamicOneboxModel({
id: 'typeahead',
metadataFields: [],
repeatable: false,
submissionId: '1234',
hasSelectableMetadata: false,
typeBindRelations: [{
match: MATCH_VISIBLE,
operator: OR_OPERATOR,
when: [{ id: 'dc.type', value: 'Book' }],
}],
}),
new DynamicScrollableDropdownModel({
id: 'scrollableDropdown',
vocabularyOptions: vocabularyOptions,
metadataFields: [],
repeatable: false,
submissionId: '1234',
hasSelectableMetadata: false,
}),
new DynamicTagModel({
id: 'tag',
metadataFields: [],
repeatable: false,
submissionId: '1234',
hasSelectableMetadata: false,
}),
new DynamicListCheckboxGroupModel({
id: 'checkboxList',
vocabularyOptions: vocabularyOptions,
repeatable: true,
required: false,
}),
new DynamicListRadioGroupModel({
id: 'radioList',
vocabularyOptions: vocabularyOptions,
repeatable: false,
required: false,
}),
new DynamicRelationGroupModel({
submissionId: '1234',
id: 'relationGroup',
formConfiguration: [],
isInlineGroup: false,
mandatoryField: '',
name: 'relationGroup',
relationFields: [],
scopeUUID: '',
submissionScope: '',
repeatable: false,
metadataFields: [],
hasSelectableMetadata: false,
}),
new DynamicRelationGroupModel({
submissionId: '1234',
id: 'inlineRelationGroup',
formConfiguration: [],
isInlineGroup: true,
mandatoryField: '',
name: 'inlineRelationGroup',
relationFields: [],
scopeUUID: '',
submissionScope: '',
repeatable: false,
metadataFields: [],
hasSelectableMetadata: false,
}),
new DynamicDsDatePickerModel({ id: 'datepicker', repeatable: false }),
new DynamicLookupModel({
id: 'lookup',
metadataFields: [],
repeatable: false,
submissionId: '1234',
hasSelectableMetadata: false,
}),
new DynamicLookupNameModel({
id: 'lookupName',
metadataFields: [],
repeatable: false,
submissionId: '1234',
hasSelectableMetadata: false,
}),
new DynamicQualdropModel({ id: 'combobox', readOnly: false, required: false }),
];
const testModel = formModel[8];
let formGroup: UntypedFormGroup;
let fixture: ComponentFixture<DsDynamicFormControlContainerComponent>;
let component: DsDynamicFormControlContainerComponent;
let debugElement: DebugElement;
let testElement: DebugElement;
const testItem: Item = new Item();
const testWSI: WorkspaceItem = new WorkspaceItem();
testWSI.item = of(createSuccessfulRemoteDataObject(testItem));
const renderer = jasmine.createSpyObj('Renderer2', ['setAttribute']);
const actions$: ReplaySubject<any> = new ReplaySubject<any>(1);
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
FormsModule,
ReactiveFormsModule,
NgbModule,
DynamicFormsCoreModule,
TranslateModule.forRoot(),
],
providers: [
provideEnvironmentNgxMask(),
DsDynamicFormControlContainerComponent,
DynamicFormService,
{ provide: DsDynamicTypeBindRelationService, useValue: getMockDsDynamicTypeBindRelationService() },
{ provide: RelationshipDataService, useValue: {} },
{ provide: SelectableListService, useValue: {} },
{ provide: Store, useValue: {} },
{ provide: RelationshipDataService, useValue: {} },
{ provide: SelectableListService, useValue: {} },
{ provide: FormBuilderService, useValue: getMockFormBuilderService() },
{ provide: SubmissionService, useValue: {} },
{
provide: SubmissionObjectService,
useValue: {
findById: () => of(createSuccessfulRemoteDataObject(testWSI)),
},
},
{ provide: APP_CONFIG, useValue: environment },
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
{ provide: DYNAMIC_FORM_CONTROL_MAP_FN, useValue: dsDynamicFormControlMapFn },
{ provide: LiveRegionService, useValue: getLiveRegionServiceStub() },
{ provide: Actions, useValue: actions$ },
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
}).compileComponents().then(() => {
fixture = TestBed.createComponent(DsDynamicFormControlContainerComponent);
const ngZone = TestBed.inject(NgZone);
spyOn(ngZone, 'runOutsideAngular').and.callFake((fn) => fn());
component = fixture.componentInstance;
debugElement = fixture.debugElement;
});
}));
beforeEach(inject([DynamicFormService, FormBuilderService], (service: DynamicFormService, formBuilderService: FormBuilderService) => {
formGroup = service.createFormGroup(formModel);
component.group = formGroup;
component.model = testModel;
component.ngOnChanges({
group: new SimpleChange(null, component.group, true),
model: new SimpleChange(null, component.model, true),
});
fixture.detectChanges();
renderer.setAttribute.calls.reset();
testElement = debugElement.query(By.css(`input[id='${testModel.id}']`));
}));
it('should initialize correctly', () => {
expect(component.context).toBeNull();
expect(component.control instanceof UntypedFormControl).toBe(true);
expect(component.group instanceof UntypedFormGroup).toBe(true);
expect(component.model instanceof DynamicFormControlModel).toBe(true);
expect(component.hasErrorMessaging).toBe(false);
expect(component.onControlValueChanges).toBeDefined();
expect(component.onModelDisabledUpdates).toBeDefined();
expect(component.onModelValueUpdates).toBeDefined();
expect(component.blur).toBeDefined();
expect(component.change).toBeDefined();
expect(component.focus).toBeDefined();
expect(component.componentType).toBe(DynamicNGBootstrapInputComponent);
});
it('should have an input element', () => {
expect(testElement instanceof DebugElement).toBe(true);
});
it('should listen to native blur events', () => {
spyOn(component, 'onBlur');
testElement.triggerEventHandler('blur', null);
expect(component.onBlur).toHaveBeenCalled();
});
it('should listen to native focus events', () => {
spyOn(component, 'onFocus');
testElement.triggerEventHandler('focus', null);
expect(component.onFocus).toHaveBeenCalled();
});
it('should listen to native change event', () => {
spyOn(component, 'onChange');
testElement.triggerEventHandler('change', null);
expect(component.onChange).toHaveBeenCalled();
});
it('should update model value when control value changes', () => {
spyOn(component, 'onControlValueChanges');
component.control.setValue('test');
expect(component.onControlValueChanges).toHaveBeenCalled();
});
it('should update control value when model value changes', () => {
spyOn(component, 'onModelValueUpdates');
(testModel as DynamicInputModel).value = 'test';
expect(component.onModelValueUpdates).toHaveBeenCalled();
});
it('should update control activation when model disabled property changes', () => {
spyOn(component, 'onModelDisabledUpdates');
testModel.disabled = true;
expect(component.onModelDisabledUpdates).toHaveBeenCalled();
});
it('should map a form control model to a form control component', () => {
const testFn = dsDynamicFormControlMapFn;
expect(testFn(formModel[0])).toEqual(DynamicNGBootstrapCheckboxComponent);
expect(testFn(formModel[1])).toEqual(DynamicNGBootstrapCheckboxGroupComponent);
expect(testFn(formModel[2])).toBeNull();
expect(testFn(formModel[3])).toEqual(DsDatePickerInlineComponent);
(formModel[3] as DynamicDatePickerModel).inline = true;
expect(testFn(formModel[3])).toEqual(DynamicNGBootstrapCalendarComponent);
expect(testFn(formModel[4])).toBeNull();
expect(testFn(formModel[5])).toBeNull();
expect(testFn(formModel[6])).toEqual(DsDynamicFormArrayComponent);
expect(testFn(formModel[7])).toEqual(DsDynamicFormGroupComponent);
expect(testFn(formModel[8])).toEqual(DynamicNGBootstrapInputComponent);
expect(testFn(formModel[9])).toEqual(DynamicNGBootstrapRadioGroupComponent);
expect(testFn(formModel[10])).toBeNull();
expect(testFn(formModel[11])).toEqual(DynamicNGBootstrapSelectComponent);
expect(testFn(formModel[12])).toBeNull();
expect(testFn(formModel[13])).toBeNull();
expect(testFn(formModel[14])).toEqual(DynamicNGBootstrapTextAreaComponent);
expect(testFn(formModel[15])).toEqual(DynamicNGBootstrapTimePickerComponent);
expect(testFn(formModel[16])).toEqual(DsDynamicOneboxComponent);
expect(testFn(formModel[17])).toEqual(DsDynamicScrollableDropdownComponent);
expect(testFn(formModel[18])).toEqual(DsDynamicTagComponent);
expect(testFn(formModel[19])).toEqual(DsDynamicListComponent);
expect(testFn(formModel[20])).toEqual(DsDynamicListComponent);
expect(testFn(formModel[21])).toEqual(DsDynamicRelationGroupComponent);
expect(testFn(formModel[22])).toEqual(DsDynamicRelationInlineGroupComponent);
expect(testFn(formModel[23])).toEqual(DsDatePickerComponent);
expect(testFn(formModel[24])).toEqual(DsDynamicLookupComponent);
expect(testFn(formModel[25])).toEqual(DsDynamicLookupComponent);
expect(testFn(formModel[26])).toEqual(DsDynamicFormGroupComponent);
});
describe('store action subscriptions', () => {
beforeEach(() => {
fixture.detectChanges();
});
it('should call announceErrorMessages on SAVE_SUBMISSION_FORM_SUCCESS', () => {
spyOn(component, 'announceErrorMessages');
actions$.next(new SaveSubmissionFormSuccessAction('1234', [] as any));
expect(component.announceErrorMessages).toHaveBeenCalled();
});
it('should call announceErrorMessages on SAVE_SUBMISSION_SECTION_FORM_SUCCESS', () => {
spyOn(component, 'announceErrorMessages');
actions$.next(new SaveSubmissionSectionFormSuccessAction('1234', [] as any));
expect(component.announceErrorMessages).toHaveBeenCalled();
});
it('should call announceErrorMessages on SAVE_SUBMISSION_FORM_ERROR', () => {
spyOn(component, 'announceErrorMessages');
actions$.next(new SaveSubmissionFormErrorAction('1234', null, null));
expect(component.announceErrorMessages).toHaveBeenCalled();
});
it('should call announceErrorMessages on SAVE_FOR_LATER_SUBMISSION_FORM_ERROR', () => {
spyOn(component, 'announceErrorMessages');
actions$.next(new SaveForLaterSubmissionFormErrorAction('1234'));
expect(component.announceErrorMessages).toHaveBeenCalled();
});
it('should call announceErrorMessages on SAVE_SUBMISSION_SECTION_FORM_ERROR', () => {
spyOn(component, 'announceErrorMessages');
actions$.next(new SaveSubmissionSectionFormErrorAction('1234', null, null));
expect(component.announceErrorMessages).toHaveBeenCalled();
});
});
it('should not show a label if is a checkbox or a date field', () => {
const checkboxLabel = fixture.debugElement.query(By.css('#label_' + formModel[0].id));
const dsDatePickerLabel = fixture.debugElement.query(By.css('#label_' + formModel[22].id));
expect(checkboxLabel).toBeNull();
expect(dsDatePickerLabel).toBeNull();
});
it('should not call handleAriaLabelForLibraryComponents if is SSR', () => {
(component as any).platformId = 'server';
(component as any).componentRef = {
instance: new DynamicNGBootstrapInputComponent(null, null),
location: { nativeElement: document.createElement('div') },
} as any;
fixture.detectChanges();
(component as any).handleAriaLabelForLibraryComponents();
expect(renderer.setAttribute).not.toHaveBeenCalled();
});
it('should set aria-label when valid input and additional property ariaLabel exist and is on browser', () => {
(component as any).platformId = 'browser';
const inputEl = document.createElement('input');
const hostEl = {
querySelector: jasmine.createSpy('querySelector').and.returnValue(inputEl),
};
(component as any).componentRef = {
instance: new DynamicNGBootstrapInputComponent(null, null),
location: { nativeElement: hostEl },
} as any;
(component as any).renderer = renderer;
component.model = { additional: { ariaLabel: 'Accessible Label' } } as any;
fixture.detectChanges();
(component as any).handleAriaLabelForLibraryComponents();
expect(renderer.setAttribute).toHaveBeenCalledWith(inputEl, 'aria-label', 'Accessible Label');
});
});