-
Notifications
You must be signed in to change notification settings - Fork 261
/
amount.widget.spec.ts
311 lines (260 loc) · 10.4 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*!
* @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 { FormFieldTypes } from '../core/form-field-types';
import { CoreTestingModule } from '../../../../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core';
import { FormModel } from '../core/form.model';
describe('AmountWidgetComponent', () => {
let widget: AmountWidgetComponent;
let fixture: ComponentFixture<AmountWidgetComponent>;
let element: HTMLElement;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
]
});
beforeEach(() => {
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;
element = fixture.nativeElement;
});
it('should setup currency from field', () => {
const currency = 'UAH';
widget.field = new FormFieldModel(null, {
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('when is required', () => {
beforeEach(() => {
widget.field = new FormFieldModel( new FormModel({ taskId: '<id>' }), {
type: FormFieldTypes.AMOUNT,
required: true
});
});
it('should be marked as invalid after interaction', async () => {
const amount = fixture.nativeElement.querySelector('input');
expect(element.querySelector('.adf-invalid')).toBeFalsy();
amount.dispatchEvent(new Event('blur'));
fixture.detectChanges();
await fixture.whenStable();
expect(fixture.nativeElement.querySelector('.adf-invalid')).toBeTruthy();
});
it('should be able to display label with asterisk', async () => {
fixture.detectChanges();
await fixture.whenStable();
const asterisk: HTMLElement = element.querySelector('.adf-asterisk');
expect(asterisk).toBeTruthy();
expect(asterisk.textContent).toEqual('*');
});
});
});
describe('AmountWidgetComponent - rendering', () => {
let widget: AmountWidgetComponent;
let fixture: ComponentFixture<AmountWidgetComponent>;
let element: HTMLElement;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule,
FormBaseModule
]
});
beforeEach(() => {
fixture = TestBed.createComponent(AmountWidgetComponent);
widget = fixture.componentInstance;
element = fixture.nativeElement;
});
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('when form model has left labels', () => {
it('should have left labels classes on leftLabels true', async () => {
widget.field = new FormFieldModel(new FormModel({ taskId: 'fake-task-id', leftLabels: true }), {
id: 'amount-id',
name: 'amount-name',
value: '',
type: FormFieldTypes.AMOUNT,
readOnly: false,
required: true
});
fixture.detectChanges();
await fixture.whenStable();
const widgetContainer = element.querySelector('.adf-left-label-input-container');
expect(widgetContainer).not.toBeNull();
const adfLeftLabel = element.querySelector('.adf-left-label');
expect(adfLeftLabel).not.toBeNull();
});
it('should not have left labels classes on leftLabels false', async () => {
widget.field = new FormFieldModel(new FormModel({ taskId: 'fake-task-id', leftLabels: false }), {
id: 'amount-id',
name: 'amount-name',
value: '',
type: FormFieldTypes.AMOUNT,
readOnly: false,
required: true
});
fixture.detectChanges();
await fixture.whenStable();
const widgetContainer = element.querySelector('.adf-left-label-input-container');
expect(widgetContainer).toBeNull();
const adfLeftLabel = element.querySelector('.adf-left-label');
expect(adfLeftLabel).toBeNull();
});
it('should not have left labels classes on leftLabels not present', async () => {
widget.field = new FormFieldModel(new FormModel({ taskId: 'fake-task-id' }), {
id: 'amount-id',
name: 'amount-name',
value: '',
type: FormFieldTypes.AMOUNT,
readOnly: false,
required: true
});
fixture.detectChanges();
await fixture.whenStable();
const widgetContainer = element.querySelector('.adf-left-label-input-container');
expect(widgetContainer).toBeNull();
const adfLeftLabel = element.querySelector('.adf-left-label');
expect(adfLeftLabel).toBeNull();
});
});
});
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');
});
});