From 6bf8c17771615af4ee02167e761bc2de3ed192de Mon Sep 17 00:00:00 2001 From: Vito Date: Fri, 28 Feb 2020 14:04:26 +0000 Subject: [PATCH] [AAE-1944] - Amount Widget Unit test (#5504) * AAE-1944 - added unit test for amount widget * AAE-1944 - fixed wrong header and lint problem --- .../widgets/amount/amount.widget.spec.ts | 87 ++++++++++++++++++- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/lib/core/form/components/widgets/amount/amount.widget.spec.ts b/lib/core/form/components/widgets/amount/amount.widget.spec.ts index 12fce28517..788d82c2e0 100644 --- a/lib/core/form/components/widgets/amount/amount.widget.spec.ts +++ b/lib/core/form/components/widgets/amount/amount.widget.spec.ts @@ -19,8 +19,12 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { FormFieldModel } from './../core/form-field.model'; import { AmountWidgetComponent, ADF_AMOUNT_SETTINGS } from './amount.widget'; import { setupTestBed } from '../../../../testing/setup-test-bed'; -import { CoreModule } from '../../../../core.module'; import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { FormBaseModule } from '../../../form-base.module'; +import { TranslateStore } from '@ngx-translate/core'; +import { TranslationService } from '../../../../services/translation.service'; +import { TranslationMock } from '../../../../mock/translation.service.mock'; +import { FormModel } from '../core'; describe('AmountWidgetComponent', () => { @@ -30,7 +34,11 @@ describe('AmountWidgetComponent', () => { setupTestBed({ imports: [ NoopAnimationsModule, - CoreModule.forRoot() + FormBaseModule + ], + providers: [ + { provide: TranslationService, useClass: TranslationMock }, + TranslateStore ] }); @@ -77,6 +85,77 @@ describe('AmountWidgetComponent', () => { }); }); +describe('AmountWidgetComponent - rendering', () => { + + let widget: AmountWidgetComponent; + let fixture: ComponentFixture; + + setupTestBed({ + imports: [ + NoopAnimationsModule, + FormBaseModule + ], + providers: [ + { provide: TranslationService, useClass: TranslationMock }, + TranslateStore + ] + }); + + beforeEach(() => { + fixture = TestBed.createComponent(AmountWidgetComponent); + widget = fixture.componentInstance; + }); + + it('[C309692] - Should be possible to set the General Properties for Amount Widget', async () => { + widget.field = new FormFieldModel(new FormModel(), { + id: 'TestAmount1', + name: 'Test Amount', + type: 'amount', + required: true, + colspan: 2, + placeholder: 'Check Placeholder Text', + minValue: null, + maxValue: null, + visibilityCondition: null, + params: { + existingColspan: 1, + maxColspan: 2 + }, + enableFractions: false, + currency: '$' + }); + fixture.detectChanges(); + await fixture.whenStable(); + + const requiredAsteriscElement = fixture.nativeElement.querySelector('.mat-placeholder-required'); + expect(requiredAsteriscElement.textContent).toContain('*'); + const widgetPlaceholder = fixture.nativeElement.querySelector('label.mat-form-field-label'); + expect(widgetPlaceholder.textContent).toBe('Check Placeholder Text *'); + const widgetLabel = fixture.nativeElement.querySelector('label.adf-label'); + expect(widgetLabel.textContent).toBe('Test Amount*'); + const widgetPrefix = fixture.nativeElement.querySelector('div.mat-form-field-prefix'); + expect(widgetPrefix.textContent).toBe('$'); + expect(widget.field.isValid).toBe(false); + const widgetById: HTMLInputElement = fixture.nativeElement.querySelector('#TestAmount1'); + expect(widgetById).toBeDefined(); + expect(widgetById).not.toBeNull(); + + widgetById.value = '90'; + widgetById.dispatchEvent(new Event('input')); + fixture.detectChanges(); + await fixture.whenStable(); + expect(widget.field.isValid).toBe(true, 'amount widget with a valid field'); + + widgetById.value = 'gdfgdf'; + widgetById.dispatchEvent(new Event('input')); + fixture.detectChanges(); + await fixture.whenStable(); + expect(widget.field.isValid).toBe(false, 'amount widget with an invalid field'); + const errorWidget = fixture.nativeElement.querySelector('error-widget .adf-error-text'); + expect(errorWidget.textContent).toBe('FORM.FIELD.VALIDATOR.INVALID_NUMBER'); + }); +}); + describe('AmountWidgetComponent settings', () => { let widget: AmountWidgetComponent; let fixture: ComponentFixture; @@ -84,9 +163,11 @@ describe('AmountWidgetComponent settings', () => { setupTestBed({ imports: [ NoopAnimationsModule, - CoreModule.forRoot() + FormBaseModule ], providers: [ + { provide: TranslationService, useClass: TranslationMock }, + TranslateStore, { provide: ADF_AMOUNT_SETTINGS, useValue: {