-
Notifications
You must be signed in to change notification settings - Fork 261
/
amount.widget.spec.ts
212 lines (182 loc) · 6.81 KB
/
amount.widget.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 { FormBaseModule } from '../../../form-base.module';
import { FormModel } from '../core';
import { CoreTestingModule } from '../../../../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core';
describe('AmountWidgetComponent', () => {
let widget: AmountWidgetComponent;
let fixture: ComponentFixture<AmountWidgetComponent>;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
]
});
beforeEach(() => {
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;
});
it('should setup currency from field', () => {
const currency = 'UAH';
widget.field = new FormFieldModel(null, {
currency: currency
});
widget.ngOnInit();
expect(widget.currency).toBe(currency);
});
it('should setup default currency', () => {
widget.field = null;
widget.ngOnInit();
expect(widget.currency).toBe(AmountWidgetComponent.DEFAULT_CURRENCY);
});
it('should setup empty placeholder in readOnly mode', () => {
widget.field = new FormFieldModel(null, {
readOnly: true,
placeholder: '1234'
});
widget.ngOnInit();
expect(widget.placeholder).toBe('');
});
it('should setup placeholder when readOnly is false', () => {
widget.field = new FormFieldModel(null, {
readOnly: false,
placeholder: '1234'
});
widget.ngOnInit();
expect(widget.placeholder).toBe('1234');
});
});
describe('AmountWidgetComponent - rendering', () => {
let widget: AmountWidgetComponent;
let fixture: ComponentFixture<AmountWidgetComponent>;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
]
});
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 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.trim()).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');
});
it('should display tooltip when tooltip is set', 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: '$',
tooltip: 'ammount widget'
});
fixture.detectChanges();
await fixture.whenStable();
const ammountElement: any = fixture.nativeElement.querySelector('#TestAmount1');
const tooltip = ammountElement.getAttribute('ng-reflect-message');
expect(tooltip).toEqual(widget.field.tooltip);
});
});
describe('AmountWidgetComponent settings', () => {
let widget: AmountWidgetComponent;
let fixture: ComponentFixture<AmountWidgetComponent>;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
],
providers: [
{
provide: ADF_AMOUNT_SETTINGS,
useValue: {
showReadonlyPlaceholder: true
}
}
]
});
beforeEach(() => {
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;
});
it('should display placeholder via injected settings', () => {
const field: any = {
readOnly: true,
placeholder: 'some placeholder'
};
widget.field = field;
expect(widget.placeholder).toBe('some placeholder');
});
});