[AAE-9348] Fix validate for people and group widgets (#7717)

* [AAE-9348] Fix validat for people and group widgets

* [AAE-9348] remove onInit call from unit test

* Trigger travis
This commit is contained in:
Tomasz Gnyp
2022-08-01 14:14:18 +02:00
committed by GitHub
parent 8aa52cd3d9
commit 2e94f50d31
6 changed files with 81 additions and 14 deletions

View File

@@ -8,6 +8,7 @@
<adf-cloud-group [mode]="mode" <adf-cloud-group [mode]="mode"
[title]="title" [title]="title"
[readOnly]="field.readOnly" [readOnly]="field.readOnly"
[validate]="validate"
[roles]="roles" [roles]="roles"
[searchGroupsControl]="search" [searchGroupsControl]="search"
[required]="isRequired()" [required]="isRequired()"

View File

@@ -46,6 +46,21 @@ describe('GroupCloudWidgetComponent', () => {
element = fixture.nativeElement; element = fixture.nativeElement;
}); });
afterEach(() => {
fixture.destroy();
});
it('should have enabled validation if field is NOT readOnly', () => {
const readOnly = false;
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>' }, null, readOnly), {
type: FormFieldTypes.GROUP,
value: []
});
fixture.detectChanges();
expect(widget.validate).toBeTruthy();
});
describe('when is required', () => { describe('when is required', () => {
beforeEach(() => { beforeEach(() => {
@@ -83,13 +98,15 @@ describe('GroupCloudWidgetComponent', () => {
describe('when is readOnly', () => { describe('when is readOnly', () => {
const readOnly = true;
it('should single chip be disabled', async () => { it('should single chip be disabled', async () => {
const mockSpaghetti: IdentityGroupModel[] = [{ const mockSpaghetti: IdentityGroupModel[] = [{
id: 'bolognese', id: 'bolognese',
name: 'Bolognese' name: 'Bolognese'
}]; }];
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, true), { widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.GROUP, type: FormFieldTypes.GROUP,
value: mockSpaghetti value: mockSpaghetti
}); });
@@ -113,7 +130,7 @@ describe('GroupCloudWidgetComponent', () => {
{ id: 'carbonara', name: 'Carbonara' } { id: 'carbonara', name: 'Carbonara' }
]; ];
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, true), { widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.GROUP, type: FormFieldTypes.GROUP,
value: mockSpaghetti value: mockSpaghetti
}); });
@@ -131,6 +148,16 @@ describe('GroupCloudWidgetComponent', () => {
expect(disabledGroupChips.item(0)).toBeTruthy(); expect(disabledGroupChips.item(0)).toBeTruthy();
expect(disabledGroupChips.item(1)).toBeTruthy(); expect(disabledGroupChips.item(1)).toBeTruthy();
}); });
it('should have disabled validation', () => {
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.GROUP,
value: []
});
fixture.detectChanges();
expect(widget.validate).toBeFalsy();
});
}); });
describe('when form model has left labels', () => { describe('when form model has left labels', () => {

View File

@@ -51,6 +51,7 @@ export class GroupCloudWidgetComponent extends WidgetComponent implements OnInit
title: string; title: string;
preSelectGroup: IdentityGroupModel[]; preSelectGroup: IdentityGroupModel[];
search: FormControl; search: FormControl;
validate = false;
constructor(formService: FormService) { constructor(formService: FormService) {
super(formService); super(formService);
@@ -62,6 +63,7 @@ export class GroupCloudWidgetComponent extends WidgetComponent implements OnInit
this.mode = this.field.optionType as ComponentSelectionMode; this.mode = this.field.optionType as ComponentSelectionMode;
this.title = this.field.placeholder; this.title = this.field.placeholder;
this.preSelectGroup = this.field.value ? this.field.value : []; this.preSelectGroup = this.field.value ? this.field.value : [];
this.validate = this.field.readOnly ? false : true;
} }
// eslint-disable-next-line @typescript-eslint/no-unused-expressions // eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.search = new FormControl({value: '', disabled: this.field.readOnly}, []), this.search = new FormControl({value: '', disabled: this.field.readOnly}, []),

View File

@@ -6,10 +6,10 @@
<div> <div>
<adf-cloud-people <adf-cloud-people
[preSelectUsers]="preSelectUsers" [preSelectUsers]="preSelectUsers"
[validate]="true"
[appName]="appName" [appName]="appName"
[title]="title" [title]="title"
[readOnly]="field.readOnly" [readOnly]="field.readOnly"
[validate]="validate"
[searchUserCtrl]="search" [searchUserCtrl]="search"
[required]="isRequired()" [required]="isRequired()"
(changedUsers)="onChangedUser($event)" (changedUsers)="onChangedUser($event)"

View File

@@ -51,20 +51,43 @@ describe('PeopleCloudWidgetComponent', () => {
spyOn(identityUserService, 'getCurrentUserInfo').and.returnValue(mockShepherdsPie); spyOn(identityUserService, 'getCurrentUserInfo').and.returnValue(mockShepherdsPie);
}); });
afterEach(() => {
fixture.destroy();
});
it('should preselect the current user', () => { it('should preselect the current user', () => {
widget.field = new FormFieldModel(new FormModel(), { value: null, selectLoggedUser: true }); widget.field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.PEOPLE,
value: null,
selectLoggedUser: true
});
fixture.detectChanges(); fixture.detectChanges();
expect(widget.preSelectUsers).toEqual([mockShepherdsPie]); expect(widget.preSelectUsers).toEqual([mockShepherdsPie]);
expect(identityUserService.getCurrentUserInfo).toHaveBeenCalled(); expect(identityUserService.getCurrentUserInfo).toHaveBeenCalled();
}); });
it('should not preselect the current user if value exist', () => { it('should not preselect the current user if value exist', () => {
widget.field = new FormFieldModel(new FormModel(), { value: [mockYorkshirePudding], selectLoggedUser: true }); widget.field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.PEOPLE,
value: [mockYorkshirePudding],
selectLoggedUser: true
});
fixture.detectChanges(); fixture.detectChanges();
expect(widget.preSelectUsers).toEqual([mockYorkshirePudding]); expect(widget.preSelectUsers).toEqual([mockYorkshirePudding]);
expect(identityUserService.getCurrentUserInfo).not.toHaveBeenCalled(); expect(identityUserService.getCurrentUserInfo).not.toHaveBeenCalled();
}); });
it('should have enabled validation if field is NOT readOnly', () => {
const readOnly = false;
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.PEOPLE,
value: []
});
fixture.detectChanges();
expect(widget.validate).toBeTruthy();
});
describe('when is required', () => { describe('when is required', () => {
beforeEach(() => { beforeEach(() => {
@@ -102,6 +125,8 @@ describe('PeopleCloudWidgetComponent', () => {
describe('when is readOnly', () => { describe('when is readOnly', () => {
const readOnly = true;
it('should single chip be disabled', async () => { it('should single chip be disabled', async () => {
const mockSpaghetti: IdentityUserModel[] = [{ const mockSpaghetti: IdentityUserModel[] = [{
id: 'bolognese', id: 'bolognese',
@@ -109,8 +134,8 @@ describe('PeopleCloudWidgetComponent', () => {
email: 'bolognese@example.com' email: 'bolognese@example.com'
}]; }];
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, true), { widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.GROUP, type: FormFieldTypes.PEOPLE,
value: mockSpaghetti value: mockSpaghetti
}); });
@@ -123,8 +148,8 @@ describe('PeopleCloudWidgetComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
await fixture.whenStable(); await fixture.whenStable();
const disabledGroupChip: HTMLElement = element.querySelector('.mat-chip-disabled'); const disabledPeopleChip: HTMLElement = element.querySelector('.mat-chip-disabled');
expect(disabledGroupChip).toBeTruthy(); expect(disabledPeopleChip).toBeTruthy();
}); });
it('should multi chips be disabled', async () => { it('should multi chips be disabled', async () => {
@@ -133,8 +158,8 @@ describe('PeopleCloudWidgetComponent', () => {
{ id: 'carbonara', username: 'Carbonara', email: 'carbonara@example.com' } { id: 'carbonara', username: 'Carbonara', email: 'carbonara@example.com' }
]; ];
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, true), { widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.GROUP, type: FormFieldTypes.PEOPLE,
value: mockSpaghetti value: mockSpaghetti
}); });
@@ -147,9 +172,19 @@ describe('PeopleCloudWidgetComponent', () => {
fixture.detectChanges(); fixture.detectChanges();
await fixture.whenStable(); await fixture.whenStable();
const disabledGroupChips = element.querySelectorAll('.mat-chip-disabled'); const disabledPeopleChips = element.querySelectorAll('.mat-chip-disabled');
expect(disabledGroupChips.item(0)).toBeTruthy(); expect(disabledPeopleChips.item(0)).toBeTruthy();
expect(disabledGroupChips.item(1)).toBeTruthy(); expect(disabledPeopleChips.item(1)).toBeTruthy();
});
it('should have disabled validation', () => {
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.PEOPLE,
value: []
});
fixture.detectChanges();
expect(widget.validate).toBeFalsy();
}); });
}); });

View File

@@ -54,6 +54,7 @@ export class PeopleCloudWidgetComponent extends WidgetComponent implements OnIni
preSelectUsers: IdentityUserModel[]; preSelectUsers: IdentityUserModel[];
search: FormControl; search: FormControl;
groupsRestriction: string[]; groupsRestriction: string[];
validate = false;
constructor(formService: FormService, private identityUserService: IdentityUserService) { constructor(formService: FormService, private identityUserService: IdentityUserService) {
super(formService); super(formService);
@@ -66,6 +67,7 @@ export class PeopleCloudWidgetComponent extends WidgetComponent implements OnIni
this.title = this.field.placeholder; this.title = this.field.placeholder;
this.preSelectUsers = this.field.value ? this.field.value : []; this.preSelectUsers = this.field.value ? this.field.value : [];
this.groupsRestriction = this.field.groupsRestriction; this.groupsRestriction = this.field.groupsRestriction;
this.validate = this.field.readOnly ? false : true;
} }
// eslint-disable-next-line @typescript-eslint/no-unused-expressions // eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.search = new FormControl({value: '', disabled: this.field.readOnly}, []), this.search = new FormControl({value: '', disabled: this.field.readOnly}, []),