[AAE-6354] [FE] radio button rest preselct from mapping (#7359)

* [AAE-6354] [FE] radio button rest preselct from mapping

* * revert

* * minor changes

* [ci:force] force test
This commit is contained in:
Dharan
2021-11-11 00:23:08 +05:30
committed by GitHub
parent ac6bdffa68
commit e441ad72ef
3 changed files with 119 additions and 109 deletions

View File

@@ -10,7 +10,7 @@
[id]="field.id + '-' + opt.id" [id]="field.id + '-' + opt.id"
[name]="field.id" [name]="field.id"
[value]="opt.id" [value]="opt.id"
[checked]="field.value === opt.id" [checked]="isChecked(opt)"
(change)="onOptionClick(opt.id)" (change)="onOptionClick(opt.id)"
color="primary" color="primary"
class="adf-radio-button" *ngFor="let opt of field.options" > class="adf-radio-button" *ngFor="let opt of field.options" >

View File

@@ -16,38 +16,42 @@
*/ */
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { MatIconModule } from '@angular/material/icon';
import { MatRadioModule } from '@angular/material/radio';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { AlfrescoApiService, CoreTestingModule, FormFieldModel, FormFieldOption, FormFieldTypes, FormModel, FormService, setupTestBed } from 'core'; import { FormFieldModel, FormFieldOption, FormFieldTypes, FormModel, setupTestBed } from '@alfresco/adf-core';
import { FormCloudService } from 'process-services-cloud'; import { FormCloudService } from '../../../services/form-cloud.service';
import { Observable } from 'rxjs';
import { RadioButtonsCloudWidgetComponent } from './radio-buttons-cloud.widget'; import { RadioButtonsCloudWidgetComponent } from './radio-buttons-cloud.widget';
import { ProcessServiceCloudTestingModule } from '../../../../testing/process-service-cloud.testing.module';
import { of } from 'rxjs';
describe('RadioButtonsCloudWidgetComponent', () => { describe('RadioButtonsCloudWidgetComponent', () => {
let fixture: ComponentFixture<RadioButtonsCloudWidgetComponent>;
let formService: FormService;
let formCloudService: FormCloudService;
let widget: RadioButtonsCloudWidgetComponent; let widget: RadioButtonsCloudWidgetComponent;
let alfrescoApiService: AlfrescoApiService; let formCloudService: FormCloudService;
let element: HTMLElement;
const restOption: FormFieldOption[] = [
{
id: 'opt-1',
name: 'opt-name-1'
},
{
id: 'opt-2',
name: 'opt-name-2'
}
];
setupTestBed({ setupTestBed({
imports: [ imports: [
TranslateModule.forRoot(), TranslateModule.forRoot(),
CoreTestingModule, ProcessServiceCloudTestingModule
MatRadioModule,
FormsModule,
MatIconModule
] ]
}); });
beforeEach(() => { beforeEach(() => {
alfrescoApiService = TestBed.inject(AlfrescoApiService); formCloudService = TestBed.inject(FormCloudService);
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of(restOption));
formService = new FormService(null, alfrescoApiService, null); fixture = TestBed.createComponent(RadioButtonsCloudWidgetComponent);
formCloudService = new FormCloudService(alfrescoApiService, null); widget = fixture.componentInstance;
widget = new RadioButtonsCloudWidgetComponent(formService, formCloudService, null); element = fixture.nativeElement;
widget.field = new FormFieldModel(new FormModel(), { restUrl: '<url>' }); widget.field = new FormFieldModel(new FormModel(), { restUrl: '<url>' });
}); });
@@ -66,10 +70,6 @@ describe('RadioButtonsCloudWidgetComponent', () => {
const field = widget.field; const field = widget.field;
spyOn(field, 'updateForm').and.stub(); spyOn(field, 'updateForm').and.stub();
spyOn(formCloudService, 'getRestWidgetData').and.returnValue(new Observable((observer) => {
observer.next(null);
observer.complete();
}));
widget.ngOnInit(); widget.ngOnInit();
expect(field.updateForm).toHaveBeenCalled(); expect(field.updateForm).toHaveBeenCalled();
}); });
@@ -81,97 +81,95 @@ describe('RadioButtonsCloudWidgetComponent', () => {
expect(widget.field.value).toEqual('fake-opt'); expect(widget.field.value).toEqual('fake-opt');
}); });
describe('when template is ready', () => { it('should show radio buttons as text when is readonly', async () => {
let radioButtonWidget: RadioButtonsCloudWidgetComponent; widget.field = new FormFieldModel(new FormModel({}), {
let fixture: ComponentFixture<RadioButtonsCloudWidgetComponent>; id: 'radio-id',
let element: HTMLElement; name: 'radio-name',
const restOption: FormFieldOption[] = [ type: FormFieldTypes.RADIO_BUTTONS,
{ readOnly: true
id: 'opt-1', });
name: 'opt-name-1' fixture.detectChanges();
}, await fixture.whenStable();
{ fixture.detectChanges();
id: 'opt-2', expect(element.querySelector('display-text-widget')).toBeDefined();
name: 'opt-name-2' });
}];
beforeEach(() => { it('should be able to set label property for Radio Button widget', () => {
fixture = TestBed.createComponent(RadioButtonsCloudWidgetComponent); widget.field = new FormFieldModel(new FormModel({}), {
radioButtonWidget = fixture.componentInstance; id: 'radio-id',
element = fixture.nativeElement; name: 'radio-name-label',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: true
});
fixture.detectChanges();
expect(element.querySelector('label').innerText).toBe('radio-name-label');
});
it('should be able to set a Radio Button widget as required', async () => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name-label',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: false,
required: true,
optionType: 'manual',
options: restOption,
restUrl: null
}); });
it('should show radio buttons as text when is readonly', async () => { fixture.detectChanges();
radioButtonWidget.field = new FormFieldModel(new FormModel({}), { await fixture.whenStable();
id: 'radio-id', fixture.detectChanges();
name: 'radio-name', const widgetLabel = element.querySelector('label');
type: FormFieldTypes.RADIO_BUTTONS, expect(widgetLabel.innerText).toBe('radio-name-label*');
readOnly: true expect(widget.field.isValid).toBe(false);
});
fixture.detectChanges(); const option: HTMLElement = <HTMLElement> element.querySelector('#radio-id-opt-1 label');
await fixture.whenStable(); option.click();
fixture.detectChanges();
expect(element.querySelector('display-text-widget')).toBeDefined(); fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
const selectedOption: HTMLElement = <HTMLElement> element.querySelector('[class*="mat-radio-checked"]');
expect(selectedOption.innerText).toBe('opt-name-1');
expect(widget.field.isValid).toBe(true);
});
it('should be able to set a Radio Button widget as required', () => {
widget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name-label',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: false,
required: true,
optionType: 'manual',
options: restOption,
restUrl: null,
value: 'opt-name-2'
}); });
it('should be able to set label property for Radio Button widget', () => { fixture.detectChanges();
radioButtonWidget.field = new FormFieldModel(new FormModel({}), { const selectedOption: HTMLElement = <HTMLElement> element.querySelector('[class*="mat-radio-checked"]');
id: 'radio-id', expect(selectedOption.innerText).toBe('opt-name-2');
name: 'radio-name-label', expect(widget.field.isValid).toBe(true);
type: FormFieldTypes.RADIO_BUTTONS, });
readOnly: true
}); it('should be able to set a Radio Button widget when rest option enabled', () => {
fixture.detectChanges(); widget.field = new FormFieldModel(new FormModel({}), {
expect(element.querySelector('label').innerText).toBe('radio-name-label'); id: 'radio-id',
name: 'radio-name-label',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: false,
required: false,
optionType: 'rest',
options: [],
restUrl: 'http://mocky.com/mocky-12344',
value: { id: 'opt-1' }
}); });
it('should be able to set a Radio Button widget as required', async () => { fixture.detectChanges();
radioButtonWidget.field = new FormFieldModel(new FormModel({}), { const selectedOption: HTMLElement = <HTMLElement> element.querySelector('[class*="mat-radio-checked"]');
id: 'radio-id', expect(selectedOption.innerText).toBe('opt-name-1');
name: 'radio-name-label', expect(widget.field.isValid).toBe(true);
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: false,
required: true,
optionType: 'manual',
options: restOption,
restUrl: null
});
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
const widgetLabel = element.querySelector('label');
expect(widgetLabel.innerText).toBe('radio-name-label*');
expect(radioButtonWidget.field.isValid).toBe(false);
const option: HTMLElement = <HTMLElement> element.querySelector('#radio-id-opt-1 label');
option.click();
fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
const selectedOption: HTMLElement = <HTMLElement> element.querySelector('[class*="mat-radio-checked"]');
expect(selectedOption.innerText).toBe('opt-name-1');
expect(radioButtonWidget.field.isValid).toBe(true);
});
it('should be able to set a Radio Button widget as required', () => {
radioButtonWidget.field = new FormFieldModel(new FormModel({}), {
id: 'radio-id',
name: 'radio-name-label',
type: FormFieldTypes.RADIO_BUTTONS,
readOnly: false,
required: true,
optionType: 'manual',
options: restOption,
restUrl: null,
value: 'opt-name-2'
});
fixture.detectChanges();
const selectedOption: HTMLElement = <HTMLElement> element.querySelector('[class*="mat-radio-checked"]');
expect(selectedOption.innerText).toBe('opt-name-2');
expect(radioButtonWidget.field.isValid).toBe(true);
});
}); });
}); });

View File

@@ -75,4 +75,16 @@ export class RadioButtonsCloudWidgetComponent extends WidgetComponent implements
this.logService.error(error); this.logService.error(error);
} }
isChecked(option: FormFieldOption): boolean {
if (this.field.value && typeof this.field.value === 'object') {
let id = 'id';
let name = 'name';
if (this.field.restUrl) {
id = this.field.restIdProperty ?? 'id';
name = this.field.restLabelProperty ?? 'name';
}
return this.field.value[id] === option.id || this.field.value[name] === option.name;
}
return this.field.value === option.id;
}
} }