Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -12,9 +12,7 @@
<div class="adf-amount-widget-container">
<mat-form-field class="adf-amount-widget__input adf-form-field-input" [floatLabel]="placeholder ? 'always' : null">
@if ( (field.name || field?.required) && !field.leftLabels) { <mat-label class="adf-label" [attr.for]="field.id">{{field.name | translate }}</mat-label> }
@if(!enableDisplayBasedOnLocale) {
<span matTextPrefix class="adf-amount-widget__prefix-spacing">{{ currency }}&nbsp;</span>
}
<span matTextPrefix class="adf-amount-widget__prefix-spacing">{{ currency }}&nbsp;</span>
<input
matInput
[title]="field.tooltip"
Expand All @@ -23,13 +21,12 @@
[id]="field.id"
[required]="field.required && field.isVisible"
[placeholder]="placeholder"
[value]="amountWidgetValue"
[(ngModel)]="amountWidgetValue"
(ngModelChange)="onFieldChangedAmountWidget()"
[value]="field.value"
[(ngModel)]="field.value"
(ngModelChange)="onFieldChanged(field)"
[disabled]="field.readOnly"
(focus)="amountWidgetOnFocus()"
(blur)="amountWidgetOnBlur()"
/>
(blur)="markAsTouched()"
/>
</mat-form-field>
<div class="adf-error-messages-container">
<error-widget [error]="field.validationSummary" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { FormModel } from '../core/form.model';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { UnitTestingUtils } from '../../../../testing/unit-testing-utils';
import { of } from 'rxjs';

describe('AmountWidgetComponent', () => {
let loader: HarnessLoader;
Expand Down Expand Up @@ -77,137 +76,6 @@ describe('AmountWidgetComponent', () => {
expect(widget.placeholder).toBe('1234');
});

it('it should return locale based on browser', () => {
const returnedLanguages: string[] = ['en-GB', 'en-US', 'en', 'de-DE', 'pl'];
const mockLanguages = spyOnProperty(window, 'navigator').and.returnValue({
languages: returnedLanguages
} as any);
const locale = widget.getLocale();

expect(locale).toBe(returnedLanguages[0]);
expect(mockLanguages).toHaveBeenCalled();
});

it('it should return default locale if browser does not return valid value', () => {
const defaultLocale = 'en-US';
spyOnProperty(window, 'navigator').and.returnValue({
languages: undefined
} as any);
const locale = widget.getLocale();

expect(locale).toBe(defaultLocale);
});

it('should set initial values when enableDisplayBasedOnLocale is enabled', () => {
const returnedLanguages: string[] = ['en-GB'];
spyOnProperty(window, 'navigator').and.returnValue({
languages: returnedLanguages
} as any);
widget.field = new FormFieldModel(null, { id: 1, name: 'test', value: 25, currency: 'USD' });
widget.enableDisplayBasedOnLocale = true;
widget.currency = 'USD';
widget.setInitialValues();

expect(widget.amountWidgetValue).toBe('$25');
expect(widget.decimalProperty).toBe('1.0-0');
expect(widget.locale).toBe(returnedLanguages[0]);
expect(widget.valueAsNumber).toBe(25);
});

it('should set initial values with correct currency', () => {
const returnedLanguages: string[] = ['en-GB'];
spyOnProperty(window, 'navigator').and.returnValue({
languages: returnedLanguages
} as any);
widget.field = new FormFieldModel(null, { id: 2, name: 'test', value: 25, currency: 'GBP' });
widget.enableDisplayBasedOnLocale = true;
widget.currency = 'GBP';
widget.setInitialValues();

expect(widget.amountWidgetValue).toBe('£25');
expect(widget.decimalProperty).toBe('1.0-0');
});

it('should set initial values with correct currency icon', () => {
const returnedLanguages: string[] = ['en-GB'];
spyOnProperty(window, 'navigator').and.returnValue({
languages: returnedLanguages
} as any);
widget.field = new FormFieldModel(null, { id: 2, name: 'test', value: 25, currency: '¥' });
widget.enableDisplayBasedOnLocale = true;
widget.currency = '¥';
widget.setInitialValues();

expect(widget.amountWidgetValue).toBe('¥25');
expect(widget.decimalProperty).toBe('1.0-0');
});

it('should set initial values without currency', () => {
const returnedLanguages: string[] = ['en-GB'];
spyOnProperty(window, 'navigator').and.returnValue({
languages: returnedLanguages
} as any);
widget.field = new FormFieldModel(null, { id: 3, name: 'test', value: 25, currency: '' });
widget.enableDisplayBasedOnLocale = true;
widget.currency = '';
widget.currencyDisplay = '';
widget.setInitialValues();

expect(widget.amountWidgetValue).toBe('25');
expect(widget.decimalProperty).toBe('1.0-0');
});

it('should set initial values when enableDisplayBasedOnLocale is disabled', () => {
widget.field = new FormFieldModel(null, { id: 4, name: 'test', value: 25, enableFractions: false, className: '' });
widget.enableDisplayBasedOnLocale = false;
widget.setInitialValues();

expect(widget.amountWidgetValue.toString()).toBe('25');
});

it('should transform value from number to string', () => {
widget.enableDisplayBasedOnLocale = true;
widget.valueAsNumber = 123456;
widget.amountWidgetOnFocus();
expect(widget.amountWidgetValue).toBe('123456');

widget.valueAsNumber = 123456.11;
widget.amountWidgetOnFocus();
expect(widget.amountWidgetValue).toBe('123456.11');

widget.valueAsNumber = 0;
widget.amountWidgetOnFocus();
expect(widget.amountWidgetValue).toBe('0');

widget.valueAsNumber = undefined;
widget.amountWidgetOnFocus();
expect(widget.amountWidgetValue).toBe(null);
});

it('should update field.value on change', () => {
widget.field = new FormFieldModel(null, { id: 5, name: 'test', value: 25 });
const mockValue = '1234.12';
widget.amountWidgetValue = mockValue;
widget.onFieldChangedAmountWidget();

expect(widget.field.value).toBe(mockValue);
});

it('should transform values on blur', () => {
widget.enableDisplayBasedOnLocale = true;
widget.amountWidgetValue = '1234.56';
widget.amountWidgetOnBlur();

expect(widget.valueAsNumber).toBe(1234.56);
expect(widget.amountWidgetValue).toBe('$1,234.56');

widget.amountWidgetValue = '';
widget.amountWidgetOnBlur();

expect(widget.valueAsNumber).toBe(null);
expect(widget.amountWidgetValue).toBe(null);
});

describe('when tooltip is set', () => {
beforeEach(() => {
widget.field = new FormFieldModel(new FormModel({ taskId: '<id>' }), {
Expand Down Expand Up @@ -459,189 +327,6 @@ describe('AmountWidgetComponent - rendering', () => {
expect(asterisk.textContent).toEqual('*');
});
});

describe('Test widget with different setting for enableDisplayBasedOnLocale', () => {
beforeEach(() => {
TestBed.resetTestingModule();
});

describe('set module for enableDisplayBasedOnLocale = true', () => {
const mockField = new FormFieldModel(new FormModel(), {
id: 'TestAmount1',
name: 'Test Amount',
type: 'amount',
currency: 'USD',
enableFractions: true,
value: '1234.55'
});
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [AmountWidgetComponent],
providers: [{ provide: ADF_AMOUNT_SETTINGS, useValue: { enableDisplayBasedOnLocale: true } }]
});
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;

fixture.componentRef.setInput('field', mockField);
loader = TestbedHarnessEnvironment.loader(fixture);
testingUtils = new UnitTestingUtils(fixture.debugElement, loader);
fixture.detectChanges();
});

it('should not display prefix with currency when enableDisplayBasedOnLocale = true', async () => {
const field = await testingUtils.getMatFormField();
expect(await field.getPrefixText()).toBe('');
});

it('should call method on focus and change input value', async () => {
const focusSpy = spyOn(widget, 'amountWidgetOnFocus').and.callThrough();
fixture.detectChanges();

const field = await testingUtils.getMatInput();
const fieldValueBeforeFocus = await field.getValue();
await field.focus();
const fieldValue = await field.getValue();

expect(field).toBeDefined();
expect(widget.field.value).toBe('1234.55');
expect(fieldValueBeforeFocus).toBe('$1,234.55');
expect(focusSpy).toHaveBeenCalled();
expect(fieldValue).toBe('1234.55');
});

it('should transform value on blur', async () => {
const newValue = '456789';
const blurSpy = spyOn(widget, 'amountWidgetOnBlur').and.callThrough();
fixture.detectChanges();

const field = await testingUtils.getMatInput();
const fieldValueBeforeBlur = await field.getValue();
await field.setValue(newValue);
await field.blur();
const fieldValue = await field.getValue();

expect(field).toBeDefined();
expect(widget.field.value).toBe(newValue);
expect(fieldValueBeforeBlur).toBe('$1,234.55');
expect(blurSpy).toHaveBeenCalled();
expect(widget.valueAsNumber).toBe(parseFloat(newValue));
expect(widget.amountWidgetValue).toBe('$456,789.00');
expect(fieldValue).toBe('$456,789.00');
});
});
describe('set module for enableDisplayBasedOnLocale = false', () => {
const mockField = new FormFieldModel(new FormModel(), {
id: 'TestAmount1',
name: 'Test Amount',
type: 'amount',
currency: 'USD',
enableFractions: true,
value: '1234.55'
});
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [AmountWidgetComponent],
providers: [{ provide: ADF_AMOUNT_SETTINGS, useValue: { enableDisplayBasedOnLocale: false } }]
});
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;

fixture.componentRef.setInput('field', mockField);
loader = TestbedHarnessEnvironment.loader(fixture);
testingUtils = new UnitTestingUtils(fixture.debugElement, loader);
fixture.detectChanges();
});

it('should display prefix with currency when enableDisplayBasedOnLocale = true', async () => {
const field = await testingUtils.getMatFormField();
expect(await field.getPrefixText()).toBe('USD');
});

it('should call method on focus and not change input value', async () => {
const focusSpy = spyOn(widget, 'amountWidgetOnFocus').and.callThrough();
fixture.detectChanges();

const field = await testingUtils.getMatInput();
const fieldValueBeforeFocus = await field.getValue();
await field.focus();
const fieldValue = await field.getValue();

expect(field).toBeDefined();
expect(widget.field.value).toBe('1234.55');
expect(widget.valueAsNumber).toBeUndefined();
expect(fieldValueBeforeFocus).toBe('1234.55');
expect(focusSpy).toHaveBeenCalled();
expect(fieldValue).toBe('1234.55');
});

it('should call method on blur and not change input value', async () => {
const newValue = '456789';
const blurSpy = spyOn(widget, 'amountWidgetOnBlur').and.callThrough();
fixture.detectChanges();

const field = await testingUtils.getMatInput();
const fieldValueBeforeBlur = await field.getValue();
await field.setValue(newValue);
await field.blur();
const fieldValue = await field.getValue();

expect(field).toBeDefined();
expect(widget.field.value).toBe(newValue);
expect(widget.valueAsNumber).toBeUndefined();
expect(fieldValueBeforeBlur).toBe('1234.55');
expect(blurSpy).toHaveBeenCalled();
expect(widget.valueAsNumber).toBeUndefined();
expect(widget.amountWidgetValue).toBe('456789');
expect(fieldValue).toBe('456789');
});
});
});

describe('Test widget with ADF_AMOUNT_SETTINGS as observable', () => {
beforeEach(() => {
TestBed.resetTestingModule();
});

describe('set module for enableDisplayBasedOnLocale = true', () => {
const mockField = new FormFieldModel(new FormModel(), {
id: 'TestAmount1',
name: 'Test Amount',
type: 'amount',
currency: 'USD',
enableFractions: true,
value: '1234.55'
});
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [AmountWidgetComponent],
providers: [{ provide: ADF_AMOUNT_SETTINGS, useValue: of({ enableDisplayBasedOnLocale: true }) }]
});
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;
const returnedLanguages: string[] = ['en-GB', 'en-US', 'en', 'de-DE', 'pl'];
spyOnProperty(window, 'navigator').and.returnValue({
languages: returnedLanguages
} as any);
fixture.componentRef.setInput('field', mockField);
loader = TestbedHarnessEnvironment.loader(fixture);
testingUtils = new UnitTestingUtils(fixture.debugElement, loader);
fixture.detectChanges();
});

it('should set enableDisplayBasedOnLocale to true', () => {
expect(widget.enableDisplayBasedOnLocale).toBeTrue();
expect(widget.decimalProperty).toBe('1.2-2');
expect(widget.locale).toBe('en-GB');
expect(widget.valueAsNumber).toBe('1234.55');
expect(widget.amountWidgetValue).toBe('$1,234.55');
});

it('should not display prefix with currency when enableDisplayBasedOnLocale = true', async () => {
const field = await testingUtils.getMatFormField();
expect(await field.getPrefixText()).toBe('');
});
});
});
});

describe('AmountWidgetComponent settings', () => {
Expand Down
Loading
Loading