AAE-22948 Radio buttons should not display errors / call apis when they're in readOnly state (#9946)

* AAE-22948 Update form-field.model

* AAE-22948 Update radio-buttons.widget

* AAE-22948 Update radio-buttons-cloud.widget

* AAE-22948 Update radio-buttons-cloud

* AAE-22948 Update radio-buttons

* AAE-22948 Improvements

* AAE-22948 Small fix

* AAE-22948 Fix unit
This commit is contained in:
Wiktor Danielewski
2024-07-25 16:19:52 +02:00
committed by GitHub
parent 63d017350d
commit 5d969ccca5
7 changed files with 435 additions and 152 deletions

View File

@@ -81,31 +81,73 @@ describe('RadioButtonsCloudWidgetComponent', () => {
expect(widget.field.value).toEqual('fake-opt');
});
it('should show radio buttons as text when is readonly', async () => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: true
describe('when widget is readonly', () => {
beforeEach(() => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: true
});
fixture.detectChanges();
});
it('should show radio buttons as text', () => {
expect(element.querySelector('display-text-widget')).toBeDefined();
});
it('should set label property', () => {
expect(element.querySelector('label').innerText).toBe('radio-name');
});
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
expect(element.querySelector('display-text-widget')).toBeDefined();
});
it('should be able to set label property for Radio Button widget', () => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name-label',
describe('fetching options from rest api', () => {
const getRadioButtonsWidgetConfig = (readOnly: boolean) => ({
id: 'rest-radio-id',
name: 'Rest Radio Buttons',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: true
readOnly,
optionType: 'rest',
restUrl: '<url>'
});
beforeEach(() => {
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of(restOption));
});
describe('when widget is readonly', () => {
it('should call rest api when form is NOT readonly', () => {
widget.field = new FormFieldModel(new FormModel({}, undefined, false), getRadioButtonsWidgetConfig(true));
fixture.detectChanges();
expect(formCloudService.getRestWidgetData).toHaveBeenCalled();
});
it('should NOT call rest api when form is readonly', () => {
widget.field = new FormFieldModel(new FormModel({}, undefined, true), getRadioButtonsWidgetConfig(true));
fixture.detectChanges();
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
});
});
describe('when widget is NOT readonly', () => {
it('should call rest api when form is NOT readonly', () => {
widget.field = new FormFieldModel(new FormModel({}, undefined, false), getRadioButtonsWidgetConfig(false));
fixture.detectChanges();
expect(formCloudService.getRestWidgetData).toHaveBeenCalled();
});
it('should NOT call rest api when form is readonly', () => {
widget.field = new FormFieldModel(new FormModel({}, undefined, true), getRadioButtonsWidgetConfig(false));
fixture.detectChanges();
expect(formCloudService.getRestWidgetData).not.toHaveBeenCalled();
});
});
fixture.detectChanges();
expect(element.querySelector('label').innerText).toBe('radio-name-label');
});
it('should be able to set a Radio Button widget as required', async () => {
it('should be able to set a Radio Buttons widget as required', async () => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name-label',
@@ -131,7 +173,7 @@ describe('RadioButtonsCloudWidgetComponent', () => {
expect(widget.field.isValid).toBe(true);
});
it('should set Radio Button as valid when required and not empty', async () => {
it('should set Radio Buttons widget as valid when required and not empty', async () => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name-label',
@@ -149,7 +191,7 @@ describe('RadioButtonsCloudWidgetComponent', () => {
expect(widget.field.isValid).toBe(true);
});
it('should be able to set a Radio Button widget when rest option enabled', () => {
it('should be able to set a Radio Buttons widget when rest option enabled', () => {
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of(restOption));
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',

View File

@@ -52,7 +52,7 @@ export class RadioButtonsCloudWidgetComponent extends WidgetComponent implements
}
ngOnInit() {
if (this.field?.restUrl) {
if (this.field?.restUrl && !this.field?.form?.readOnly) {
this.getValuesFromRestApi();
}
}