[AAE-14699] Improved card view text item component update (#8597)

* AAE-14699: Improved card view text item component update

* AAE-14699: updated unit tests
This commit is contained in:
Ehsan Rezaei 2023-06-06 07:28:26 +02:00 committed by GitHub
parent 9bf30a6b66
commit 6ec394da5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 6 deletions

View File

@ -509,15 +509,26 @@ describe('CardViewTextItemComponent', () => {
expect(cardViewUpdateService.update).toHaveBeenCalledWith(property, 'updated-value');
});
it('should NOT trigger the update event if the editedValue is invalid', async () => {
it('should trigger the update event if the editedValue is NOT invalid', async () => {
const cardViewUpdateService = TestBed.inject(CardViewUpdateService);
spyOn(cardViewUpdateService, 'update');
component.property.isValid = () => false;
updateTextField(component.property.key, 'updated-value');
updateTextField(component.property.key, '@invalid-value');
await fixture.whenStable();
expect(cardViewUpdateService.update).not.toHaveBeenCalled();
expect(cardViewUpdateService.update).toHaveBeenCalled();
});
it('should trigger the update event if the editedValue is valid', async () => {
const cardViewUpdateService = TestBed.inject(CardViewUpdateService);
spyOn(cardViewUpdateService, 'update');
component.property.isValid = () => true;
updateTextField(component.property.key, 'valid-value');
await fixture.whenStable();
expect(cardViewUpdateService.update).toHaveBeenCalled();
});
it('should set the errorMessages properly if the editedValue is invalid', async () => {
@ -530,6 +541,15 @@ describe('CardViewTextItemComponent', () => {
expect(component.errors).toBe(expectedErrorMessages);
});
it('should set the errorMessages properly if the editedValue is valid', async () => {
component.property.isValid = () => true;
updateTextField(component.property.key, 'updated-value');
await fixture.whenStable();
expect(component.errors).toEqual([]);
});
it('should render the error', () => {
component.errors = expectedErrorMessages;
component.editable = true;
@ -645,7 +665,7 @@ describe('CardViewTextItemComponent', () => {
editable: true
});
component.editable = true;
component.property.validators.push(new CardViewItemIntValidator());
component.property.validators?.push(new CardViewItemIntValidator());
component.ngOnChanges({ property: new SimpleChange(null, null, true) });
fixture.detectChanges();
});
@ -776,7 +796,7 @@ describe('CardViewTextItemComponent', () => {
editable: true
});
component.editable = true;
component.property.validators.push(new CardViewItemFloatValidator());
component.property.validators?.push(new CardViewItemFloatValidator());
component.ngOnChanges({ property: new SimpleChange(null, null, true) });
fixture.detectChanges();
});

View File

@ -128,12 +128,13 @@ export class CardViewTextItemComponent extends BaseCardView<CardViewTextItemMode
}
update(): void {
this.resetErrorMessages();
if (this.property.isValid(this.editedValue)) {
this.property.value = this.prepareValueForUpload(this.property, this.editedValue);
this.cardViewUpdateService.update({ ...this.property } as CardViewTextItemModel, this.property.value);
this.resetErrorMessages();
} else {
this.errors = this.property.getValidationErrors(this.editedValue);
this.cardViewUpdateService.update({ ...this.property } as CardViewTextItemModel, this.editedValue);
}
}