@sankhyalabs/core 5.20.0-dev.9 → 5.20.0-rc.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/classes/Change.md +11 -11
- package/.docs/classes/DataUnit.md +145 -115
- package/.docs/classes/ObjectUtils.md +48 -0
- package/.docs/classes/OverflowWatcher.md +310 -46
- package/.docs/classes/SelectionInfo.md +11 -11
- package/.docs/enumerations/ChangeOperation.md +4 -4
- package/.docs/enumerations/SelectionMode.md +2 -2
- package/.docs/globals.md +6 -0
- package/.docs/interfaces/DUActionInterceptor.md +1 -1
- package/.docs/interfaces/OverFlowWatcherParams.md +67 -0
- package/.docs/interfaces/PageRequest.md +3 -3
- package/.docs/interfaces/QuickFilter.md +3 -3
- package/.docs/interfaces/Record.md +4 -4
- package/.docs/interfaces/SavedRecord.md +5 -5
- package/.docs/interfaces/WaitingChange.md +3 -3
- package/.docs/type-aliases/DataUnitEventOptions.md +17 -0
- package/.docs/variables/OVERFLOWED_CLASS_NAME.md +13 -0
- package/dist/dataunit/DataUnit.d.ts +7 -2
- package/dist/dataunit/DataUnit.js +31 -20
- package/dist/dataunit/DataUnit.js.map +1 -1
- package/dist/dataunit/formatting/PrettyFormatter.js +7 -5
- package/dist/dataunit/formatting/PrettyFormatter.js.map +1 -1
- package/dist/dataunit/metadata/DataType.js +3 -0
- package/dist/dataunit/metadata/DataType.js.map +1 -1
- package/dist/dataunit/state/slice/SelectionSlice.js +1 -1
- package/dist/dataunit/state/slice/SelectionSlice.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/utils/ElementUtils.d.ts +2 -0
- package/dist/utils/ElementUtils.js +9 -0
- package/dist/utils/ElementUtils.js.map +1 -0
- package/dist/utils/ObjectUtils.d.ts +14 -0
- package/dist/utils/ObjectUtils.js +18 -0
- package/dist/utils/ObjectUtils.js.map +1 -1
- package/dist/utils/OverflowWatcher/index.d.ts +35 -7
- package/dist/utils/OverflowWatcher/index.js +140 -59
- package/dist/utils/OverflowWatcher/index.js.map +1 -1
- package/jest.config.ts +2 -0
- package/package.json +2 -1
- package/reports/test-report.xml +151 -0
- package/setupTests.js +7 -0
- package/sonar-project.properties +6 -3
- package/src/dataunit/DataUnit.ts +35 -21
- package/src/dataunit/formatting/PrettyFormatter.ts +6 -5
- package/src/dataunit/metadata/DataType.ts +3 -0
- package/src/dataunit/state/slice/SelectionSlice.ts +1 -1
- package/src/index.ts +6 -3
- package/src/utils/ElementUtils.ts +10 -0
- package/src/utils/ObjectUtils.ts +20 -0
- package/src/utils/OverflowWatcher/index.ts +170 -78
- package/test/dataunit/formatting/PrettyFormatter.spec.ts +177 -0
- package/test/util/ElementUtils.spec.ts +34 -0
- package/test/util/OverflowWatcher.spec.ts +152 -118
|
@@ -1,151 +1,243 @@
|
|
|
1
1
|
import { JSUtils } from "../JSUtils.js";
|
|
2
2
|
import { OverflowDirection } from "./types/overflow-direction.js"
|
|
3
3
|
import { OnOverflowCallBack } from "./types/overflow-callback.js"
|
|
4
|
+
import { calcMarginSize } from '../ElementUtils.js';
|
|
4
5
|
|
|
5
6
|
export * from "./types/overflow-direction.js";
|
|
6
7
|
export * from "./types/overflow-callback.js";
|
|
7
8
|
|
|
9
|
+
export const OVERFLOWED_CLASS_NAME = 'overflowed';
|
|
10
|
+
|
|
8
11
|
export default class OverflowWatcher {
|
|
9
12
|
private _onResize:OnOverflowCallBack;
|
|
10
13
|
private _resizeObserver:ResizeObserver;
|
|
11
|
-
private
|
|
14
|
+
private _lastContainerSize:number|undefined = undefined;
|
|
15
|
+
private _lastContainerInstance: HTMLElement | undefined = undefined;
|
|
12
16
|
private _scrollDirection = OverflowDirection.HORIZONTAL;
|
|
13
17
|
private _propSize:string;
|
|
14
|
-
private
|
|
18
|
+
private _hiddenItemsProps:Map<Element, SizeProps> = new Map();
|
|
19
|
+
private _notOverFlowPros:Map<string, SizeProps> = new Map();
|
|
15
20
|
private _deltaSize:number;
|
|
21
|
+
private _notOverFlow: string[] = [];
|
|
22
|
+
|
|
23
|
+
readonly DATA_ELEMENT_ID = 'data-element-id';
|
|
16
24
|
|
|
17
25
|
/**
|
|
18
|
-
* Cria uma instancia do OverflowWatcher
|
|
19
|
-
*
|
|
26
|
+
* Cria uma instancia do OverflowWatcher
|
|
27
|
+
*
|
|
20
28
|
* @param element - Elemento HTML que o overflow será observado.
|
|
21
29
|
* @param callback - Função que sera chamada quando ocorrer overflow no elemento.
|
|
22
30
|
* @param overFlowDirection - Indica direção que o overflow será monitorado.
|
|
23
31
|
* @param deltaSize - Variação de tamanho que será considerada como overflow.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
* @param debounce - Tempo até execução do callback em milissegundos.
|
|
33
|
+
* @param notOverFlow - Lista de ids ou data-element-ids dos elementos que não devem sofrer overFlow.
|
|
34
|
+
*/
|
|
35
|
+
constructor({
|
|
36
|
+
element,
|
|
37
|
+
callback,
|
|
38
|
+
overFlowDirection = OverflowDirection.HORIZONTAL,
|
|
39
|
+
debounce = 200,
|
|
40
|
+
deltaSize = 0,
|
|
41
|
+
notOverFlow = []
|
|
42
|
+
}: OverFlowWatcherParams){
|
|
29
43
|
this._onResize = callback;
|
|
30
44
|
this._scrollDirection = overFlowDirection;
|
|
31
|
-
this.
|
|
32
|
-
this._resizeObserver.
|
|
33
|
-
this.
|
|
34
|
-
this._deltaSize = deltaSize;
|
|
45
|
+
this._propSize = (OverflowDirection.HORIZONTAL === overFlowDirection) ? "width" : "height";
|
|
46
|
+
this._resizeObserver = new ResizeObserver(JSUtils.debounce((entries: ResizeObserverEntry[]) => this.handleResize(entries), debounce));
|
|
47
|
+
this._resizeObserver.observe(element);
|
|
48
|
+
this._deltaSize = deltaSize;
|
|
49
|
+
this._notOverFlow = notOverFlow;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public addNotOverFlowElement(elementId: string){
|
|
53
|
+
if(!this._notOverFlow.includes(elementId)){
|
|
54
|
+
this._notOverFlow.push(elementId);
|
|
55
|
+
}
|
|
35
56
|
}
|
|
36
57
|
|
|
37
58
|
public destroy(){
|
|
38
59
|
this._resizeObserver.disconnect();
|
|
39
60
|
}
|
|
40
61
|
|
|
62
|
+
public forceUpdate(){
|
|
63
|
+
if(this._lastContainerSize && this._lastContainerInstance){
|
|
64
|
+
this.updateOverFlowedItems(this._lastContainerInstance, this._lastContainerSize);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
41
68
|
private handleResize(entries: ResizeObserverEntry[]){
|
|
69
|
+
if(!entries || entries.length === 0) return;
|
|
42
70
|
|
|
43
|
-
|
|
44
|
-
|
|
71
|
+
const container = entries[0];
|
|
72
|
+
const containerSize:number = (container.contentRect as any)[this._propSize];
|
|
73
|
+
if(!containerSize) return;
|
|
74
|
+
|
|
75
|
+
if(this.hasChangedSize(containerSize)){
|
|
76
|
+
this.updateOverFlowedItems(container.target as HTMLElement, containerSize);
|
|
45
77
|
}
|
|
78
|
+
}
|
|
46
79
|
|
|
47
|
-
|
|
48
|
-
const
|
|
80
|
+
private updateOverFlowedItems(container: HTMLElement, containerSize: number){
|
|
81
|
+
const children:Element[] = Array.from(container.children);
|
|
82
|
+
this.registerNotOverflowProps(children);
|
|
83
|
+
this.proccessElements(containerSize, children);
|
|
84
|
+
this._lastContainerSize = containerSize;
|
|
85
|
+
this._lastContainerInstance = container;
|
|
86
|
+
}
|
|
49
87
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this.
|
|
53
|
-
this.
|
|
54
|
-
|
|
88
|
+
private registerNotOverflowProps(children: Element[]) {
|
|
89
|
+
children.forEach(childElement => {
|
|
90
|
+
const id = childElement.id || this.getDataElementId(childElement);
|
|
91
|
+
if (this.canNotRegisterNotOverFlow(id)) return;
|
|
92
|
+
this._notOverFlowPros.set(id, this.getElementSizeProps(childElement));
|
|
93
|
+
});
|
|
55
94
|
}
|
|
56
95
|
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
return Math.abs((newContentRect as any)[this._propSize] - (this._lastContentRect as any)[this._propSize]) >= this._deltaSize;
|
|
96
|
+
private canNotRegisterNotOverFlow(id: string) {
|
|
97
|
+
return !id || !this._notOverFlow.includes(id) || this._notOverFlowPros.has(id);
|
|
62
98
|
}
|
|
63
99
|
|
|
100
|
+
private hasChangedSize(elementSize: number):boolean{
|
|
101
|
+
if(!this._lastContainerSize) return true;
|
|
102
|
+
const variation = elementSize - this._lastContainerSize;
|
|
64
103
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return
|
|
104
|
+
if(variation < 0){
|
|
105
|
+
const absoluteVariation = Math.abs(variation);
|
|
106
|
+
return (absoluteVariation > this._deltaSize);
|
|
68
107
|
}
|
|
69
108
|
|
|
70
|
-
return
|
|
109
|
+
return variation > 0;
|
|
71
110
|
}
|
|
72
111
|
|
|
73
112
|
private proccessElements(elementSize:number, children:Element[]){
|
|
74
|
-
|
|
75
|
-
if(children.length === 0){
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
113
|
+
if(children.length === 0) return;
|
|
78
114
|
|
|
79
115
|
const childrenSize = this.calcChildrenSize(children);
|
|
80
|
-
|
|
81
116
|
let diff = Number((elementSize - childrenSize).toFixed(4));
|
|
82
|
-
|
|
117
|
+
|
|
83
118
|
if(diff > 0){
|
|
84
|
-
this.
|
|
119
|
+
this.clearOverFlow();
|
|
85
120
|
return;
|
|
86
121
|
}
|
|
87
122
|
|
|
88
|
-
this.proccessElementsOverFlow(children,
|
|
123
|
+
this.proccessElementsOverFlow(children, elementSize);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private clearOverFlow(){
|
|
127
|
+
this._hiddenItemsProps = new Map();
|
|
128
|
+
this._onResize([]);
|
|
89
129
|
}
|
|
90
130
|
|
|
91
|
-
private
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
131
|
+
private proccessElementsOverFlow(allElements:Element[], avaliableSize:number){
|
|
132
|
+
const elementsThatFit: Element[] = [];
|
|
133
|
+
const avaliableSizeConsideringDelta = (avaliableSize - this._deltaSize);
|
|
134
|
+
|
|
135
|
+
let sumElementsSize = 0;
|
|
136
|
+
for (const element of allElements) {
|
|
137
|
+
sumElementsSize += this.calcElementSize(element);
|
|
138
|
+
if(this.exceedsAvaliableSize(sumElementsSize, elementsThatFit, avaliableSizeConsideringDelta)) break;
|
|
139
|
+
elementsThatFit.push(element);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const overFlowedElements = allElements.filter(element => this.isElementOverFlowing(elementsThatFit, element));
|
|
143
|
+
|
|
144
|
+
overFlowedElements.forEach(overFlowed => {
|
|
145
|
+
if(!this._hiddenItemsProps.has(overFlowed)){
|
|
146
|
+
this.registerElementSize(overFlowed);
|
|
96
147
|
}
|
|
97
|
-
|
|
98
148
|
});
|
|
99
149
|
|
|
100
|
-
this._onResize(
|
|
150
|
+
this._onResize(overFlowedElements);
|
|
101
151
|
}
|
|
102
152
|
|
|
103
|
-
private
|
|
104
|
-
|
|
105
|
-
|
|
153
|
+
private isElementOverFlowing(elementsThatFit: Element[], element: Element) {
|
|
154
|
+
return !elementsThatFit.includes(element) && this.canOverFlowElement(element);
|
|
155
|
+
}
|
|
106
156
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
157
|
+
private canOverFlowElement(element: Element) {
|
|
158
|
+
return !this._notOverFlow.includes(element.id)
|
|
159
|
+
&& !this._notOverFlow.includes(this.getDataElementId(element));
|
|
160
|
+
}
|
|
113
161
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
}
|
|
162
|
+
private getDataElementId(element: Element): string {
|
|
163
|
+
return (element as HTMLElement).getAttribute('data-element-id') ?? "";
|
|
164
|
+
}
|
|
119
165
|
|
|
120
|
-
|
|
121
|
-
this.
|
|
122
|
-
this._onResize(elementsOverflow);
|
|
166
|
+
private exceedsAvaliableSize(sumElementsSize: number, elements: Element[], avaliableSize: number): boolean {
|
|
167
|
+
if(!this._notOverFlow.length) return sumElementsSize > avaliableSize
|
|
123
168
|
|
|
124
|
-
|
|
169
|
+
const elementIdsToCalculate = this.canNotOverFlowNotIncludedIds(elements);
|
|
170
|
+
if(!elementIdsToCalculate.length) return sumElementsSize > avaliableSize
|
|
125
171
|
|
|
126
|
-
|
|
127
|
-
|
|
172
|
+
const variation = this.calculateVariation(elementIdsToCalculate);
|
|
173
|
+
const occupiedSize = sumElementsSize + variation;
|
|
174
|
+
return occupiedSize > avaliableSize;
|
|
175
|
+
}
|
|
128
176
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
177
|
+
private calculateVariation(elementIdsToCalculate: string[]) {
|
|
178
|
+
let variation = 0
|
|
179
|
+
elementIdsToCalculate.forEach(id => {
|
|
180
|
+
const sizeProps = this._notOverFlowPros.get(id);
|
|
181
|
+
variation += sizeProps?.size ?? 0;
|
|
182
|
+
variation += sizeProps?.margin ?? 0;
|
|
132
183
|
});
|
|
184
|
+
return variation;
|
|
185
|
+
}
|
|
133
186
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
187
|
+
private canNotOverFlowNotIncludedIds(elements: Element[]): string[]{
|
|
188
|
+
const elementsIdList = elements.map(el => el.id || this.getDataElementId(el)).filter(id => !!id);
|
|
189
|
+
return this._notOverFlow.filter(id => !elementsIdList.includes(id));
|
|
190
|
+
}
|
|
137
191
|
|
|
138
|
-
|
|
192
|
+
private registerElementSize(element: Element) {
|
|
193
|
+
const sizeProps = this.getElementSizeProps(element);
|
|
194
|
+
this._hiddenItemsProps.set(element, sizeProps);
|
|
139
195
|
}
|
|
140
196
|
|
|
141
|
-
private
|
|
142
|
-
const
|
|
197
|
+
private getElementSizeProps(element: Element) {
|
|
198
|
+
const sizeProps: SizeProps = {
|
|
199
|
+
size: (element.getBoundingClientRect() as any)[this._propSize],
|
|
200
|
+
margin: calcMarginSize(element, this._scrollDirection),
|
|
201
|
+
};
|
|
202
|
+
return sizeProps;
|
|
203
|
+
}
|
|
143
204
|
|
|
144
|
-
|
|
145
|
-
|
|
205
|
+
private calcChildrenSize(children:Element[]):number{
|
|
206
|
+
let sumChildren = 0;
|
|
207
|
+
sumChildren += this._deltaSize;
|
|
208
|
+
Array.from(children).forEach(el => sumChildren += this.calcElementSize(el));
|
|
209
|
+
return sumChildren;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private calcElementSize(el: Element) {
|
|
213
|
+
let size = 0
|
|
214
|
+
if (this.isOverFlowed(el)) {
|
|
215
|
+
const sizeProps = this._hiddenItemsProps.get(el);
|
|
216
|
+
size += sizeProps?.size ?? 0;
|
|
217
|
+
size += sizeProps?.margin ?? 0;
|
|
218
|
+
return size;
|
|
146
219
|
}
|
|
147
220
|
|
|
148
|
-
|
|
221
|
+
size += (el.getBoundingClientRect() as any)[this._propSize];
|
|
222
|
+
size += calcMarginSize(el, this._scrollDirection);
|
|
223
|
+
return size;
|
|
149
224
|
}
|
|
150
225
|
|
|
226
|
+
private isOverFlowed(el: Element) {
|
|
227
|
+
return el.classList.contains(OVERFLOWED_CLASS_NAME);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export interface OverFlowWatcherParams {
|
|
232
|
+
element:HTMLElement,
|
|
233
|
+
callback:OnOverflowCallBack,
|
|
234
|
+
overFlowDirection?:OverflowDirection,
|
|
235
|
+
deltaSize?:number,
|
|
236
|
+
debounce?: number,
|
|
237
|
+
notOverFlow?: string[]
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
interface SizeProps {
|
|
241
|
+
size: number,
|
|
242
|
+
margin: number,
|
|
151
243
|
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { getFormattedValue } from '../../../src/dataunit/formatting/PrettyFormatter';
|
|
2
|
+
import { DataType, FieldDescriptor, UserInterface } from '../../../src';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* É preciso importart o utilitário DateUtils para que seus métodos
|
|
6
|
+
* estáticos sejam executados no ambiente de testes
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { DateUtils } from '../../../src';
|
|
10
|
+
|
|
11
|
+
describe('getFormattedValue', () => {
|
|
12
|
+
it('should return empty string when value is null', () => {
|
|
13
|
+
const value = null;
|
|
14
|
+
const descriptor = getFileFieldDescriptor();
|
|
15
|
+
expect(getFormattedValue(value, descriptor)).toBe('');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should return empty string when value is not an array', () => {
|
|
19
|
+
const value = 'not an array';
|
|
20
|
+
const descriptor = getFileFieldDescriptor();
|
|
21
|
+
expect(getFormattedValue(value, descriptor)).toBe('');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('should return empty string when value is an empty array', () => {
|
|
25
|
+
const value: any[] = [];
|
|
26
|
+
const descriptor = getFileFieldDescriptor();
|
|
27
|
+
expect(getFormattedValue(value, descriptor)).toBe('0 arquivos');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should return file name when value is an array with one file object', () => {
|
|
31
|
+
const value = [{ name: 'file1.txt' }];
|
|
32
|
+
const descriptor = getFileFieldDescriptor();
|
|
33
|
+
expect(getFormattedValue(value, descriptor)).toBe('file1.txt');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should return file count when value is an array with multiple file objects', () => {
|
|
37
|
+
const value = [{ name: 'file1.txt' }, { name: 'file2.txt' }];
|
|
38
|
+
const descriptor = getFileFieldDescriptor();
|
|
39
|
+
expect(getFormattedValue(value, descriptor)).toBe('2 arquivos');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should return empty string for undefined file', () => {
|
|
43
|
+
const value = undefined;
|
|
44
|
+
const descriptor = { userInterface: UserInterface.FILE } as FieldDescriptor;
|
|
45
|
+
expect(getFormattedValue(value, descriptor)).toBe('');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should handle OBJECT type with value field', () => {
|
|
49
|
+
const value = { value: 'code', label: 'label' };
|
|
50
|
+
const descriptor = { dataType: DataType.OBJECT, userInterface: UserInterface.SEARCH } as FieldDescriptor;
|
|
51
|
+
expect(getFormattedValue(value, descriptor)).toBe('code - label');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should return empty string when value is undefined', () => {
|
|
55
|
+
const value = undefined;
|
|
56
|
+
const descriptor = getObjectFieldDescriptor();
|
|
57
|
+
expect(getFormattedValue(value, descriptor)).toBe('');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('should return value as string when value is not an object', () => {
|
|
61
|
+
const value = 12345;
|
|
62
|
+
const descriptor = getObjectFieldDescriptor();
|
|
63
|
+
expect(getFormattedValue(value, descriptor)).toBe('12345');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should return value.toString() when value is an object without value property', () => {
|
|
67
|
+
const value = { someProperty: 'someValue' };
|
|
68
|
+
const descriptor = getObjectFieldDescriptor();
|
|
69
|
+
expect(getFormattedValue(value, descriptor)).toBe(value.toString());
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should handle BOOLEAN type', () => {
|
|
73
|
+
expect(getFormattedValue(true, { dataType: DataType.BOOLEAN } as FieldDescriptor)).toBe('Sim');
|
|
74
|
+
expect(getFormattedValue(false, { dataType: DataType.BOOLEAN } as FieldDescriptor)).toBe('Não');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should format numbers correctly', () => {
|
|
78
|
+
const value = 1234.567;
|
|
79
|
+
const descriptor = {
|
|
80
|
+
dataType: DataType.NUMBER,
|
|
81
|
+
properties: { precision: 2, prettyPrecision: 2 },
|
|
82
|
+
} as FieldDescriptor;
|
|
83
|
+
expect(getFormattedValue(value, descriptor)).toBe('1.234,57');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should format dates correctly', () => {
|
|
87
|
+
const value = new Date('2023-07-01T14:30:00');
|
|
88
|
+
const descriptor = { userInterface: UserInterface.DATE } as FieldDescriptor;
|
|
89
|
+
expect(getFormattedValue(value, descriptor)).toBe('01/07/2023');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should format times correctly', () => {
|
|
93
|
+
const value = new Date('2023-07-01T14:30:00');
|
|
94
|
+
const descriptor = { userInterface: UserInterface.TIME } as FieldDescriptor;
|
|
95
|
+
expect(getFormattedValue(value, descriptor)).toBe('14:30');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should format datetime correctly', () => {
|
|
99
|
+
const value = new Date('2023-07-01T14:30:00');
|
|
100
|
+
const descriptor = { userInterface: UserInterface.DATETIME } as FieldDescriptor;
|
|
101
|
+
expect(getFormattedValue(value, descriptor)).toBe('01/07/2023 14:30');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should call toString with correct parameters', () => {
|
|
105
|
+
const value = 123;
|
|
106
|
+
const descriptor = { dataType: DataType.NUMBER } as FieldDescriptor;
|
|
107
|
+
expect(getFormattedValue(value, descriptor)).toBe('123,00');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should handle undefined descriptor gracefully', () => {
|
|
111
|
+
const value = 123;
|
|
112
|
+
expect(getFormattedValue(value)).toBe('123');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should format value correctly with OPTIONSELECTOR and matching option', () => {
|
|
116
|
+
const value = 'option1';
|
|
117
|
+
const descriptor = buildOptionSelectorFieldDescriptor();
|
|
118
|
+
expect(getFormattedValue(value, descriptor)).toBe('option1');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('should return value with OPTIONSELECTOR and no matching option', () => {
|
|
122
|
+
const value = 'option2';
|
|
123
|
+
const descriptor = buildOptionSelectorFieldDescriptor();
|
|
124
|
+
expect(getFormattedValue(value, descriptor)).toBe('option2');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should format value correctly with OPTIONSELECTOR and value as object', () => {
|
|
128
|
+
const value = { value: 'option1' };
|
|
129
|
+
const descriptor = buildOptionSelectorFieldDescriptor();
|
|
130
|
+
expect(getFormattedValue(value, descriptor)).toBe('option1');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should return empty string with OPTIONSELECTOR and value as null', () => {
|
|
134
|
+
const value = null;
|
|
135
|
+
const descriptor = buildOptionSelectorFieldDescriptor();
|
|
136
|
+
expect(getFormattedValue(value, descriptor)).toBe('');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should format value correctly with OPTIONSELECTOR and options as JSON string', () => {
|
|
140
|
+
const value = 'option1';
|
|
141
|
+
const descriptor: FieldDescriptor = {
|
|
142
|
+
userInterface: UserInterface.OPTIONSELECTOR,
|
|
143
|
+
properties: { options: JSON.stringify([{ label: 'Option 1', value: 'option1' }]) },
|
|
144
|
+
} as FieldDescriptor;
|
|
145
|
+
expect(getFormattedValue(value, descriptor)).toBe('option1');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should return value with OPTIONSELECTOR and options as empty array', () => {
|
|
149
|
+
const value = 'option1';
|
|
150
|
+
const descriptor: FieldDescriptor = {
|
|
151
|
+
userInterface: UserInterface.OPTIONSELECTOR,
|
|
152
|
+
properties: { options: [] },
|
|
153
|
+
} as FieldDescriptor;
|
|
154
|
+
expect(getFormattedValue(value, descriptor)).toBe('option1');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should format masked values correctly', () => {
|
|
158
|
+
const value = '12345678901';
|
|
159
|
+
const descriptor = { properties: { mask: 'cpf' } } as FieldDescriptor;
|
|
160
|
+
expect(getFormattedValue(value, descriptor)).toBe('123.456.789-01');
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
function buildOptionSelectorFieldDescriptor(): FieldDescriptor {
|
|
165
|
+
return {
|
|
166
|
+
userInterface: UserInterface.OPTIONSELECTOR,
|
|
167
|
+
properties: { options: [{ label: 'Option 1', value: 'option1' }] },
|
|
168
|
+
} as FieldDescriptor;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getFileFieldDescriptor(): FieldDescriptor{
|
|
172
|
+
return { userInterface: UserInterface.FILE } as FieldDescriptor;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function getObjectFieldDescriptor(): FieldDescriptor{
|
|
176
|
+
return { dataType: DataType.OBJECT } as FieldDescriptor;
|
|
177
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { calcMarginSize } from '../../src/utils/ElementUtils';
|
|
2
|
+
import { OverflowDirection } from '../../src';
|
|
3
|
+
|
|
4
|
+
describe('calcMarginSize', () => {
|
|
5
|
+
it('should calculate correctly the size of horizontal margin', () => {
|
|
6
|
+
const element = document.createElement('div');
|
|
7
|
+
element.style.marginLeft = '10px';
|
|
8
|
+
element.style.marginRight = '15px';
|
|
9
|
+
document.body.appendChild(element);
|
|
10
|
+
|
|
11
|
+
const size = calcMarginSize(element, OverflowDirection.HORIZONTAL);
|
|
12
|
+
expect(size).toBe(25);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should calculate correctly the size of vertical margin', () => {
|
|
16
|
+
const element = document.createElement('div');
|
|
17
|
+
element.style.marginTop = '5px';
|
|
18
|
+
element.style.marginBottom = '20px';
|
|
19
|
+
document.body.appendChild(element);
|
|
20
|
+
|
|
21
|
+
const size = calcMarginSize(element, OverflowDirection.VERTICAL);
|
|
22
|
+
expect(size).toBe(25);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should threat values defined as zero', () => {
|
|
26
|
+
const element = document.createElement('div');
|
|
27
|
+
document.body.appendChild(element);
|
|
28
|
+
|
|
29
|
+
const horizontalSize = calcMarginSize(element, OverflowDirection.HORIZONTAL);
|
|
30
|
+
const verticalSize = calcMarginSize(element, OverflowDirection.VERTICAL);
|
|
31
|
+
expect(horizontalSize).toBe(0);
|
|
32
|
+
expect(verticalSize).toBe(0);
|
|
33
|
+
});
|
|
34
|
+
});
|