[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"
[title]="title"
[readOnly]="field.readOnly"
[validate]="validate"
[roles]="roles"
[searchGroupsControl]="search"
[required]="isRequired()"

View File

@@ -46,6 +46,21 @@ describe('GroupCloudWidgetComponent', () => {
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', () => {
beforeEach(() => {
@@ -83,13 +98,15 @@ describe('GroupCloudWidgetComponent', () => {
describe('when is readOnly', () => {
const readOnly = true;
it('should single chip be disabled', async () => {
const mockSpaghetti: IdentityGroupModel[] = [{
id: '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,
value: mockSpaghetti
});
@@ -113,7 +130,7 @@ describe('GroupCloudWidgetComponent', () => {
{ 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,
value: mockSpaghetti
});
@@ -131,6 +148,16 @@ describe('GroupCloudWidgetComponent', () => {
expect(disabledGroupChips.item(0)).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', () => {

View File

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

View File

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

View File

@@ -51,20 +51,43 @@ describe('PeopleCloudWidgetComponent', () => {
spyOn(identityUserService, 'getCurrentUserInfo').and.returnValue(mockShepherdsPie);
});
afterEach(() => {
fixture.destroy();
});
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();
expect(widget.preSelectUsers).toEqual([mockShepherdsPie]);
expect(identityUserService.getCurrentUserInfo).toHaveBeenCalled();
});
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();
expect(widget.preSelectUsers).toEqual([mockYorkshirePudding]);
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', () => {
beforeEach(() => {
@@ -102,6 +125,8 @@ describe('PeopleCloudWidgetComponent', () => {
describe('when is readOnly', () => {
const readOnly = true;
it('should single chip be disabled', async () => {
const mockSpaghetti: IdentityUserModel[] = [{
id: 'bolognese',
@@ -109,8 +134,8 @@ describe('PeopleCloudWidgetComponent', () => {
email: 'bolognese@example.com'
}];
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, true), {
type: FormFieldTypes.GROUP,
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.PEOPLE,
value: mockSpaghetti
});
@@ -123,8 +148,8 @@ describe('PeopleCloudWidgetComponent', () => {
fixture.detectChanges();
await fixture.whenStable();
const disabledGroupChip: HTMLElement = element.querySelector('.mat-chip-disabled');
expect(disabledGroupChip).toBeTruthy();
const disabledPeopleChip: HTMLElement = element.querySelector('.mat-chip-disabled');
expect(disabledPeopleChip).toBeTruthy();
});
it('should multi chips be disabled', async () => {
@@ -133,8 +158,8 @@ describe('PeopleCloudWidgetComponent', () => {
{ id: 'carbonara', username: 'Carbonara', email: 'carbonara@example.com' }
];
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, true), {
type: FormFieldTypes.GROUP,
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>'}, null, readOnly), {
type: FormFieldTypes.PEOPLE,
value: mockSpaghetti
});
@@ -147,9 +172,19 @@ describe('PeopleCloudWidgetComponent', () => {
fixture.detectChanges();
await fixture.whenStable();
const disabledGroupChips = element.querySelectorAll('.mat-chip-disabled');
expect(disabledGroupChips.item(0)).toBeTruthy();
expect(disabledGroupChips.item(1)).toBeTruthy();
const disabledPeopleChips = element.querySelectorAll('.mat-chip-disabled');
expect(disabledPeopleChips.item(0)).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[];
search: FormControl;
groupsRestriction: string[];
validate = false;
constructor(formService: FormService, private identityUserService: IdentityUserService) {
super(formService);
@@ -66,6 +67,7 @@ export class PeopleCloudWidgetComponent extends WidgetComponent implements OnIni
this.title = this.field.placeholder;
this.preSelectUsers = this.field.value ? this.field.value : [];
this.groupsRestriction = this.field.groupsRestriction;
this.validate = this.field.readOnly ? false : true;
}
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
this.search = new FormControl({value: '', disabled: this.field.readOnly}, []),