mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
AAE-24018 Readonly dropdown with the rest config don't fetch options when form rule change it to not readonly (#9959)
* AAE-24018 Update dropdown-cloud.widget * AAE-24018 Upadate dropdown.widget * AAE-24018 Small update
This commit is contained in:
committed by
GitHub
parent
9cb01e2085
commit
72ea94e928
@@ -30,7 +30,7 @@
|
||||
<mat-option id="readonlyOption" *ngIf="isReadOnlyType()" [value]="field.value">{{field.value}}</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
<div *ngIf="!previewState">
|
||||
<div *ngIf="!previewState && !isReadOnlyField">
|
||||
<error-widget [error]="field.validationSummary"></error-widget>
|
||||
<error-widget class="adf-dropdown-required-message" *ngIf="showRequiredMessage()"
|
||||
required="{{ 'FORM.FIELD.REQUIRED' | translate }}"></error-widget>
|
||||
|
@@ -37,6 +37,7 @@ import { HarnessLoader } from '@angular/cdk/testing';
|
||||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
|
||||
import { MatSelectHarness } from '@angular/material/select/testing';
|
||||
import { MatFormFieldHarness } from '@angular/material/form-field/testing';
|
||||
import { DebugElement } from '@angular/core';
|
||||
|
||||
describe('DropdownCloudWidgetComponent', () => {
|
||||
let formService: FormService;
|
||||
@@ -118,35 +119,79 @@ describe('DropdownCloudWidgetComponent', () => {
|
||||
expect(await allOptions[2].getText()).toEqual('option_3');
|
||||
});
|
||||
|
||||
it('should NOT load data from restUrl when field is readonly', () => {
|
||||
it('should NOT load data from restUrl when form is readonly', () => {
|
||||
spyOn(formCloudService, 'getRestWidgetData');
|
||||
widget.field.readOnly = true;
|
||||
widget.field.readOnly = false;
|
||||
widget.field.restUrl = 'https://fake-rest-url';
|
||||
widget.field.optionType = 'rest';
|
||||
widget.field.restIdProperty = 'name';
|
||||
widget.field.form.readOnly = true;
|
||||
|
||||
widget.ngOnInit();
|
||||
|
||||
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should show error message if the restUrl failed to fetch options', async () => {
|
||||
const jsonDataSpy = spyOn(formCloudService, 'getRestWidgetData').and.returnValue(throwError('Failed to fetch options'));
|
||||
describe('should load data from restUrl when form is NOT readonly', () => {
|
||||
beforeEach(() => {
|
||||
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of([]));
|
||||
|
||||
widget.field.restUrl = 'https://fake-rest-url';
|
||||
widget.field.optionType = 'rest';
|
||||
widget.field.restIdProperty = 'name';
|
||||
widget.field.form.readOnly = false;
|
||||
});
|
||||
|
||||
it('when widget is NOT readonly', () => {
|
||||
widget.field.readOnly = false;
|
||||
widget.ngOnInit();
|
||||
|
||||
expect(formCloudService.getRestWidgetData).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('when widget is readonly', () => {
|
||||
widget.field.readOnly = true;
|
||||
widget.ngOnInit();
|
||||
|
||||
expect(formCloudService.getRestWidgetData).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when failed on loading options from restUrl', () => {
|
||||
let getRestWidgetDataSpy: jasmine.Spy;
|
||||
const getErrorMessageElement = (): DebugElement => fixture.debugElement.query(By.css('.adf-dropdown-failed-message'));
|
||||
const errorIcon: string = 'error_outline';
|
||||
widget.field.restUrl = 'https://fake-rest-url';
|
||||
widget.field.optionType = 'rest';
|
||||
widget.field.restIdProperty = 'name';
|
||||
|
||||
widget.ngOnInit();
|
||||
beforeEach(() => {
|
||||
getRestWidgetDataSpy = spyOn(formCloudService, 'getRestWidgetData').and.returnValue(throwError('Failed to fetch options'));
|
||||
widget.field.restUrl = 'https://fake-rest-url';
|
||||
widget.field.optionType = 'rest';
|
||||
});
|
||||
|
||||
const dropdown = await loader.getHarness(MatSelectHarness.with({ selector: '.adf-select' }));
|
||||
await dropdown.open();
|
||||
it('should show error message when widget is NOT readonly', () => {
|
||||
widget.field.readOnly = false;
|
||||
|
||||
const failedErrorMsgElement = fixture.debugElement.query(By.css('.adf-dropdown-failed-message'));
|
||||
expect(jsonDataSpy).toHaveBeenCalled();
|
||||
expect(widget.isRestApiFailed).toBe(true);
|
||||
expect(widget.field.options.length).toEqual(0);
|
||||
expect(failedErrorMsgElement.nativeElement.textContent.trim()).toBe(errorIcon + 'FORM.FIELD.REST_API_FAILED');
|
||||
widget.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
|
||||
const errorMessageElement = getErrorMessageElement();
|
||||
expect(getRestWidgetDataSpy).toHaveBeenCalled();
|
||||
expect(widget.isRestApiFailed).toBe(true);
|
||||
expect(widget.field.options.length).toEqual(0);
|
||||
expect(errorMessageElement.nativeElement.textContent.trim()).toBe(errorIcon + 'FORM.FIELD.REST_API_FAILED');
|
||||
});
|
||||
|
||||
it('should NOT show error message when widget is readonly', async () => {
|
||||
widget.field.readOnly = true;
|
||||
|
||||
widget.ngOnInit();
|
||||
fixture.detectChanges();
|
||||
|
||||
const errorMessageElement = getErrorMessageElement();
|
||||
expect(getRestWidgetDataSpy).toHaveBeenCalled();
|
||||
expect(widget.isRestApiFailed).toBe(true);
|
||||
expect(widget.field.options.length).toEqual(0);
|
||||
expect(errorMessageElement).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
it('should preselect dropdown widget value when Json (rest call) passed', async () => {
|
||||
|
@@ -84,7 +84,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
|
||||
private checkFieldOptionsSource(): void {
|
||||
switch (true) {
|
||||
case this.isReadOnly():
|
||||
case this.isReadOnlyForm():
|
||||
break;
|
||||
case this.hasRestUrl() && !this.isLinkedWidget():
|
||||
this.persistFieldOptionsFromRestApi();
|
||||
@@ -392,7 +392,11 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
}
|
||||
}
|
||||
|
||||
private isReadOnly(): boolean {
|
||||
private isReadOnlyForm(): boolean {
|
||||
return !!this.field?.form?.readOnly;
|
||||
}
|
||||
|
||||
get isReadOnlyField(): boolean {
|
||||
return this.field.readOnly;
|
||||
}
|
||||
|
||||
@@ -410,7 +414,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
|
||||
}
|
||||
|
||||
updateOptions(): void {
|
||||
if (this.isReadOnly()) {
|
||||
if (this.isReadOnlyForm()) {
|
||||
this.list$ = of(this.field.options);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user