Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACS-6812] Add new chip only when input value is valid #9358

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import { CardViewItemFloatValidator } from '../../validators/card-view-item-floa
import { CardViewItemIntValidator } from '../../validators/card-view-item-int.validator';
import { CardViewIntItemModel } from '../../models/card-view-intitem.model';
import { CardViewFloatItemModel } from '../../models/card-view-floatitem.model';
import { MatChipsModule } from '@angular/material/chips';
import { MatChipInputEvent, MatChipsModule } from '@angular/material/chips';
import { ClipboardService } from '../../../clipboard/clipboard.service';
import { SimpleChange } from '@angular/core';
import { DebugElement, SimpleChange } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { CardViewItemValidator } from '../../interfaces/card-view-item-validator.interface';
import { HarnessLoader } from '@angular/cdk/testing';
Expand Down Expand Up @@ -97,6 +97,10 @@ describe('CardViewTextItemComponent', () => {
expect(await valueChips[2].getText()).toBe(param3);
};

const getErrorElement = (key: string, includeItems = false): DebugElement[] => {
return fixture.debugElement.queryAll(By.css(`[data-automation-id="card-textitem-error-${key}"]${includeItems ? ' li' : ''}`));
};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [TranslateModule.forRoot(), CoreTestingModule, MatChipsModule]
Expand Down Expand Up @@ -208,7 +212,7 @@ describe('CardViewTextItemComponent', () => {
editable: true,
multivalued: true
};
renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, 'item1', 'item2', 'item3');
await renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, 'item1', 'item2', 'item3');
});

it('should render chips for multivalue integers when chips are enabled', async () => {
Expand All @@ -219,7 +223,7 @@ describe('CardViewTextItemComponent', () => {
editable: true,
multivalued: true
};
renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, '1', '2', '3');
await renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, '1', '2', '3');
});

it('should render chips for multivalue decimal numbers when chips are enabled', async () => {
Expand All @@ -230,7 +234,39 @@ describe('CardViewTextItemComponent', () => {
editable: true,
multivalued: true
};
renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, '1.1', '2.2', '3.3');
await renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, '1.1', '2.2', '3.3');
});

it('should only render new chip when provided value is valid for specified validators set', async () => {
const cardViewTextItemObject = {
label: 'Text label',
value: [1.1, 2.2, 3.3],
key: 'textkey',
editable: true,
multivalued: true,
type: 'float'
};
component.editable = true;
await renderChipsForMultiValuedProperties(cardViewTextItemObject, true, 3, '1.1', '2.2', '3.3');
const floatValidator: CardViewItemValidator = new CardViewItemFloatValidator();
component.property.validators = [floatValidator];
const inputElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-editchipinput-textkey"]')).nativeElement;
component.addValueToList({ value: 'abcd', chipInput: inputElement } as MatChipInputEvent);
fixture.detectChanges();

let error: HTMLLIElement = getErrorElement('textkey', true)[0].nativeElement;
expect(error.innerText).toBe('CORE.CARDVIEW.VALIDATORS.FLOAT_VALIDATION_ERROR');
let valueChips = await loader.getAllHarnesses(MatChipHarness);
expect(valueChips.length).toBe(3);

component.addValueToList({ value: '22.1', chipInput: inputElement } as MatChipInputEvent);
fixture.detectChanges();
await fixture.whenStable();

error = getErrorElement('textkey', true)[0]?.nativeElement;
expect(error).toBe(undefined);
valueChips = await loader.getAllHarnesses(MatChipHarness);
expect(valueChips.length).toBe(4);
});

it('should render string for multivalue properties when chips are disabled', async () => {
Expand Down Expand Up @@ -697,7 +733,7 @@ describe('CardViewTextItemComponent', () => {
await fixture.whenStable();
fixture.detectChanges();

const errorElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-error-textkey"]'));
const errorElement = getErrorElement('textkey');
expect(errorElement).toBeNull();
});

Expand All @@ -707,7 +743,7 @@ describe('CardViewTextItemComponent', () => {
fixture.detectChanges();

const inputElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-value-textkey"]'));
const errorElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-error-textkey"]'));
const errorElement = getErrorElement('textkey');

expect(inputElement.nativeElement.value).toBe('');
expect(errorElement).toBeNull();
Expand All @@ -727,7 +763,7 @@ describe('CardViewTextItemComponent', () => {
await fixture.whenStable();
fixture.detectChanges();

const errorElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-error-textkey"]'));
const errorElement = getErrorElement('textkey');
expect(errorElement).toBeNull();
});

Expand All @@ -737,8 +773,7 @@ describe('CardViewTextItemComponent', () => {
fixture.detectChanges();

const inputElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-value-textkey"]'));
const errorElement = fixture.debugElement.query(By.css('[data-automation-id="card-textitem-error-textkey"]'));

const errorElement = getErrorElement('textkey');
expect(inputElement.nativeElement.value).toBe('');
expect(errorElement).toBeNull();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,21 @@ export class CardViewTextItemComponent extends BaseCardView<CardViewTextItemMode
}

addValueToList(newListItem: MatChipInputEvent) {
const chipInput = newListItem.input;
const chipInput = newListItem.chipInput.inputElement;
const chipValue = newListItem.value.trim() || '';

if (typeof this.editedValue !== 'string') {
if (chipValue) {
this.editedValue.push(chipValue);
this.update();
}
if (this.property.isValid(chipValue)) {
if (chipValue) {
this.editedValue.push(chipValue);
this.update();
}

if (chipInput) {
chipInput.value = '';
if (chipInput) {
chipInput.value = '';
}
} else {
this.errors = this.property.getValidationErrors(chipValue);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/core/src/lib/pipes/decimal-number.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ describe('DecimalNumberPipe', () => {
expect(pipe.transform(1234.567)).toBe('1,234.57');
});

it('should properly transform array of values', () => {
expect(pipe.transform([1234.567, 22])).toEqual(['1,234.57', '22']);
});

it('should return number with at least the minimum of digints in the integer part', () => {
const decimalValues = {
minIntegerDigits: 6,
Expand Down
7 changes: 6 additions & 1 deletion lib/core/src/lib/pipes/decimal-number.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export class DecimalNumberPipe implements PipeTransform, OnDestroy {
const actualLocale = locale || this.defaultLocale;

const decimalPipe: DecimalPipe = new DecimalPipe(actualLocale);
return decimalPipe.transform(value, actualDigitsInfo);

if (value instanceof Array) {
return value.map((val) => decimalPipe.transform(val, actualDigitsInfo));
} else {
return decimalPipe.transform(value, actualDigitsInfo);
}
}

ngOnDestroy(): void {
Expand Down
Loading