@sankhyalabs/core 5.20.0-dev.14 → 5.20.0-dev.16
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/OverflowWatcher.md +292 -46
- package/.docs/globals.md +5 -0
- package/.docs/interfaces/OverFlowWatcherParams.md +67 -0
- package/.docs/variables/OVERFLOWED_CLASS_NAME.md +13 -0
- package/dist/index.d.ts +2 -2
- 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/OverflowWatcher/index.d.ts +34 -7
- package/dist/utils/OverflowWatcher/index.js +135 -59
- package/dist/utils/OverflowWatcher/index.js.map +1 -1
- package/jest.config.ts +2 -0
- package/package.json +3 -2
- package/reports/test-report.xml +86 -77
- package/setupTests.js +1 -0
- package/sonar-project.properties +1 -0
- package/src/index.ts +4 -2
- package/src/utils/ElementUtils.ts +10 -0
- package/src/utils/OverflowWatcher/index.ts +163 -77
- package/test/util/ElementUtils.spec.ts +34 -0
- package/test/util/OverflowWatcher.spec.ts +143 -118
package/src/index.ts
CHANGED
|
@@ -39,7 +39,7 @@ import { IRepositoryIndex } from "./repository/indexeddb/IRepositoryIndex.js"
|
|
|
39
39
|
import { FieldComparator } from "./dataunit/sorting/FieldComparator.js";
|
|
40
40
|
import { KeyboardManager } from "./utils/KeyboardManager/index.js";
|
|
41
41
|
import { SearchUtils } from "./utils/SearchUtils.js";
|
|
42
|
-
import OverflowWatcher, { OnOverflowCallBack, OverflowDirection } from "./utils/OverflowWatcher/index.js";
|
|
42
|
+
import OverflowWatcher, { OnOverflowCallBack, OverflowDirection, OverFlowWatcherParams, OVERFLOWED_CLASS_NAME } from "./utils/OverflowWatcher/index.js";
|
|
43
43
|
|
|
44
44
|
/*Classes públicas no pacote*/
|
|
45
45
|
export {
|
|
@@ -109,5 +109,7 @@ export {
|
|
|
109
109
|
SearchUtils,
|
|
110
110
|
OverflowWatcher,
|
|
111
111
|
OnOverflowCallBack,
|
|
112
|
-
OverflowDirection
|
|
112
|
+
OverflowDirection,
|
|
113
|
+
OverFlowWatcherParams,
|
|
114
|
+
OVERFLOWED_CLASS_NAME
|
|
113
115
|
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { OverflowDirection } from './OverflowWatcher/types/overflow-direction.js';
|
|
2
|
+
|
|
3
|
+
export function calcMarginSize(el:Element, scrollDirection: OverflowDirection){
|
|
4
|
+
const computedStyle = getComputedStyle(el);
|
|
5
|
+
if(OverflowDirection.HORIZONTAL === scrollDirection){
|
|
6
|
+
return (parseInt(computedStyle.marginLeft || '0') + parseInt(computedStyle.marginRight || '0'));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return (parseInt(computedStyle.marginTop || '0') + parseInt(computedStyle.marginBottom || '0'));
|
|
10
|
+
}
|
|
@@ -1,151 +1,237 @@
|
|
|
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;
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
public destroy(){
|
|
38
53
|
this._resizeObserver.disconnect();
|
|
39
54
|
}
|
|
40
55
|
|
|
56
|
+
public forceUpdate(){
|
|
57
|
+
if(this._lastContainerSize && this._lastContainerInstance){
|
|
58
|
+
this.updateOverFlowedItems(this._lastContainerInstance, this._lastContainerSize);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
41
62
|
private handleResize(entries: ResizeObserverEntry[]){
|
|
63
|
+
if(!entries || entries.length === 0) return;
|
|
42
64
|
|
|
43
|
-
|
|
44
|
-
|
|
65
|
+
const container = entries[0];
|
|
66
|
+
const containerSize:number = (container.contentRect as any)[this._propSize];
|
|
67
|
+
if(!containerSize) return;
|
|
68
|
+
|
|
69
|
+
if(this.hasChangedSize(containerSize)){
|
|
70
|
+
this.updateOverFlowedItems(container.target as HTMLElement, containerSize);
|
|
45
71
|
}
|
|
72
|
+
}
|
|
46
73
|
|
|
47
|
-
|
|
48
|
-
const
|
|
74
|
+
private updateOverFlowedItems(container: HTMLElement, containerSize: number){
|
|
75
|
+
const children:Element[] = Array.from(container.children);
|
|
76
|
+
this.registerNotOverflowProps(children);
|
|
77
|
+
this.proccessElements(containerSize, children);
|
|
78
|
+
this._lastContainerSize = containerSize;
|
|
79
|
+
this._lastContainerInstance = container;
|
|
80
|
+
}
|
|
49
81
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
this.
|
|
53
|
-
this.
|
|
54
|
-
|
|
82
|
+
private registerNotOverflowProps(children: Element[]) {
|
|
83
|
+
children.forEach(childElement => {
|
|
84
|
+
const id = childElement.id || this.getDataElementId(childElement);
|
|
85
|
+
if (this.canNotRegisterNotOverFlow(id)) return;
|
|
86
|
+
this._notOverFlowPros.set(id, this.getElementSizeProps(childElement));
|
|
87
|
+
});
|
|
55
88
|
}
|
|
56
89
|
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
return true;
|
|
60
|
-
}
|
|
61
|
-
return Math.abs((newContentRect as any)[this._propSize] - (this._lastContentRect as any)[this._propSize]) >= this._deltaSize;
|
|
90
|
+
private canNotRegisterNotOverFlow(id: string) {
|
|
91
|
+
return !id || !this._notOverFlow.includes(id) || this._notOverFlowPros.has(id);
|
|
62
92
|
}
|
|
63
93
|
|
|
94
|
+
private hasChangedSize(elementSize: number):boolean{
|
|
95
|
+
if(!this._lastContainerSize) return true;
|
|
96
|
+
const variation = elementSize - this._lastContainerSize;
|
|
64
97
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return
|
|
98
|
+
if(variation < 0){
|
|
99
|
+
const absoluteVariation = Math.abs(variation);
|
|
100
|
+
return (absoluteVariation > this._deltaSize);
|
|
68
101
|
}
|
|
69
102
|
|
|
70
|
-
return
|
|
103
|
+
return variation > 0;
|
|
71
104
|
}
|
|
72
105
|
|
|
73
106
|
private proccessElements(elementSize:number, children:Element[]){
|
|
74
|
-
|
|
75
|
-
if(children.length === 0){
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
107
|
+
if(children.length === 0) return;
|
|
78
108
|
|
|
79
109
|
const childrenSize = this.calcChildrenSize(children);
|
|
80
|
-
|
|
81
110
|
let diff = Number((elementSize - childrenSize).toFixed(4));
|
|
82
|
-
|
|
111
|
+
|
|
83
112
|
if(diff > 0){
|
|
84
|
-
this.
|
|
113
|
+
this.clearOverFlow();
|
|
85
114
|
return;
|
|
86
115
|
}
|
|
87
116
|
|
|
88
|
-
this.proccessElementsOverFlow(children,
|
|
117
|
+
this.proccessElementsOverFlow(children, elementSize);
|
|
89
118
|
}
|
|
90
119
|
|
|
91
|
-
private
|
|
92
|
-
this.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
120
|
+
private clearOverFlow(){
|
|
121
|
+
this._hiddenItemsProps = new Map();
|
|
122
|
+
this._onResize([]);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private proccessElementsOverFlow(allElements:Element[], avaliableSize:number){
|
|
126
|
+
const elementsThatFit: Element[] = [];
|
|
127
|
+
const avaliableSizeConsideringDelta = (avaliableSize - this._deltaSize);
|
|
128
|
+
|
|
129
|
+
let sumElementsSize = 0;
|
|
130
|
+
for (const element of allElements) {
|
|
131
|
+
sumElementsSize += this.calcElementSize(element);
|
|
132
|
+
if(this.exceedsAvaliableSize(sumElementsSize, elementsThatFit, avaliableSizeConsideringDelta)) break;
|
|
133
|
+
elementsThatFit.push(element);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const overFlowedElements = allElements.filter(element => this.isElementOverFlowing(elementsThatFit, element));
|
|
137
|
+
|
|
138
|
+
overFlowedElements.forEach(overFlowed => {
|
|
139
|
+
if(!this._hiddenItemsProps.has(overFlowed)){
|
|
140
|
+
this.registerElementSize(overFlowed);
|
|
96
141
|
}
|
|
97
|
-
|
|
98
142
|
});
|
|
99
143
|
|
|
100
|
-
this._onResize(
|
|
144
|
+
this._onResize(overFlowedElements);
|
|
101
145
|
}
|
|
102
146
|
|
|
103
|
-
private
|
|
104
|
-
|
|
105
|
-
|
|
147
|
+
private isElementOverFlowing(elementsThatFit: Element[], element: Element) {
|
|
148
|
+
return !elementsThatFit.includes(element) && this.canOverFlowElement(element);
|
|
149
|
+
}
|
|
106
150
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
151
|
+
private canOverFlowElement(element: Element) {
|
|
152
|
+
return !this._notOverFlow.includes(element.id)
|
|
153
|
+
&& !this._notOverFlow.includes(this.getDataElementId(element));
|
|
154
|
+
}
|
|
113
155
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
}
|
|
156
|
+
private getDataElementId(element: Element): string {
|
|
157
|
+
return (element as HTMLElement).getAttribute('data-element-id') ?? "";
|
|
158
|
+
}
|
|
119
159
|
|
|
160
|
+
private exceedsAvaliableSize(sumElementsSize: number, elements: Element[], avaliableSize: number): boolean {
|
|
161
|
+
if(!this._notOverFlow.length) return sumElementsSize > avaliableSize
|
|
120
162
|
|
|
121
|
-
|
|
122
|
-
|
|
163
|
+
const elementIdsToCalculate = this.canNotOverFlowNotIncludedIds(elements);
|
|
164
|
+
if(!elementIdsToCalculate.length) return sumElementsSize > avaliableSize
|
|
123
165
|
|
|
166
|
+
const variation = this.calculateVariation(elementIdsToCalculate);
|
|
167
|
+
const occupiedSize = sumElementsSize + variation;
|
|
168
|
+
return occupiedSize > avaliableSize;
|
|
124
169
|
}
|
|
125
170
|
|
|
126
|
-
private
|
|
127
|
-
let
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
171
|
+
private calculateVariation(elementIdsToCalculate: string[]) {
|
|
172
|
+
let variation = 0
|
|
173
|
+
elementIdsToCalculate.forEach(id => {
|
|
174
|
+
const sizeProps = this._notOverFlowPros.get(id);
|
|
175
|
+
variation += sizeProps?.size ?? 0;
|
|
176
|
+
variation += sizeProps?.margin ?? 0;
|
|
132
177
|
});
|
|
178
|
+
return variation;
|
|
179
|
+
}
|
|
133
180
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
181
|
+
private canNotOverFlowNotIncludedIds(elements: Element[]): string[]{
|
|
182
|
+
const elementsIdList = elements.map(el => el.id || this.getDataElementId(el)).filter(id => !!id);
|
|
183
|
+
return this._notOverFlow.filter(id => !elementsIdList.includes(id));
|
|
184
|
+
}
|
|
137
185
|
|
|
138
|
-
|
|
186
|
+
private registerElementSize(element: Element) {
|
|
187
|
+
const sizeProps = this.getElementSizeProps(element);
|
|
188
|
+
this._hiddenItemsProps.set(element, sizeProps);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private getElementSizeProps(element: Element) {
|
|
192
|
+
const sizeProps: SizeProps = {
|
|
193
|
+
size: (element.getBoundingClientRect() as any)[this._propSize],
|
|
194
|
+
margin: calcMarginSize(element, this._scrollDirection),
|
|
195
|
+
};
|
|
196
|
+
return sizeProps;
|
|
139
197
|
}
|
|
140
198
|
|
|
141
|
-
private
|
|
142
|
-
|
|
199
|
+
private calcChildrenSize(children:Element[]):number{
|
|
200
|
+
let sumChildren = 0;
|
|
201
|
+
sumChildren += this._deltaSize;
|
|
202
|
+
Array.from(children).forEach(el => sumChildren += this.calcElementSize(el));
|
|
203
|
+
return sumChildren;
|
|
204
|
+
}
|
|
143
205
|
|
|
144
|
-
|
|
145
|
-
|
|
206
|
+
private calcElementSize(el: Element) {
|
|
207
|
+
let size = 0
|
|
208
|
+
if (this.isOverFlowed(el)) {
|
|
209
|
+
const sizeProps = this._hiddenItemsProps.get(el);
|
|
210
|
+
size += sizeProps?.size ?? 0;
|
|
211
|
+
size += sizeProps?.margin ?? 0;
|
|
212
|
+
return size;
|
|
146
213
|
}
|
|
147
214
|
|
|
148
|
-
|
|
215
|
+
size += (el.getBoundingClientRect() as any)[this._propSize];
|
|
216
|
+
size += calcMarginSize(el, this._scrollDirection);
|
|
217
|
+
return size;
|
|
149
218
|
}
|
|
150
219
|
|
|
220
|
+
private isOverFlowed(el: Element) {
|
|
221
|
+
return el.classList.contains(OVERFLOWED_CLASS_NAME);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface OverFlowWatcherParams {
|
|
226
|
+
element:HTMLElement,
|
|
227
|
+
callback:OnOverflowCallBack,
|
|
228
|
+
overFlowDirection?:OverflowDirection,
|
|
229
|
+
deltaSize?:number,
|
|
230
|
+
debounce?: number,
|
|
231
|
+
notOverFlow?: string[]
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
interface SizeProps {
|
|
235
|
+
size: number,
|
|
236
|
+
margin: number,
|
|
151
237
|
}
|
|
@@ -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
|
+
});
|
|
@@ -1,118 +1,143 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
describe('OverflowWatcher',
|
|
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
|
-
|
|
1
|
+
import OverflowWatcher, { OverFlowWatcherParams, OverflowDirection, OVERFLOWED_CLASS_NAME } from '../../src/utils/OverflowWatcher';
|
|
2
|
+
|
|
3
|
+
describe('OverflowWatcher', () => {
|
|
4
|
+
let mockElement: HTMLElement;
|
|
5
|
+
let mockCallback: jest.Mock;
|
|
6
|
+
let watcherInstance: OverflowWatcher;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
mockElement = document.createElement('div');
|
|
10
|
+
Object.defineProperty(mockElement, 'clientWidth', { value: 90, configurable: true });
|
|
11
|
+
document.body.appendChild(mockElement);
|
|
12
|
+
mockCallback = jest.fn();
|
|
13
|
+
|
|
14
|
+
const params: OverFlowWatcherParams = {
|
|
15
|
+
element: mockElement,
|
|
16
|
+
callback: mockCallback,
|
|
17
|
+
overFlowDirection: OverflowDirection.HORIZONTAL,
|
|
18
|
+
debounce: 1,
|
|
19
|
+
deltaSize: 1,
|
|
20
|
+
notOverFlow: ['testId'],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
watcherInstance = new OverflowWatcher(params);
|
|
24
|
+
mockElement.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(200));
|
|
25
|
+
setPrivateField(watcherInstance, '_lastContainerSize', 100);
|
|
26
|
+
setPrivateField(watcherInstance, '_lastContainerInstance', mockElement);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
watcherInstance.destroy();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should initialize with provided parameters', () => {
|
|
34
|
+
expect(watcherInstance).toBeDefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should disconect ResizeObserver when destroy is called', () => {
|
|
38
|
+
const disconnectSpy = jest.spyOn(ResizeObserver.prototype, 'disconnect');
|
|
39
|
+
watcherInstance.destroy();
|
|
40
|
+
expect(disconnectSpy).toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('Should call callback on forceUpdate', () => {
|
|
44
|
+
appendMockedChildren(mockElement);
|
|
45
|
+
watcherInstance.forceUpdate();
|
|
46
|
+
expect(mockCallback).toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('Should call callback on forceUpdate with childSpan', () => {
|
|
50
|
+
appendMockedChildren(mockElement);
|
|
51
|
+
watcherInstance.forceUpdate();
|
|
52
|
+
|
|
53
|
+
const childSpan = mockElement.children[1];
|
|
54
|
+
expect(mockCallback).toHaveBeenCalledWith(expect.arrayContaining([childSpan]));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('Should call callback on forceUpdate with childSpan when notOverFlow is empty', () => {
|
|
58
|
+
appendMockedChildren(mockElement);
|
|
59
|
+
|
|
60
|
+
const params: OverFlowWatcherParams = {
|
|
61
|
+
element: mockElement,
|
|
62
|
+
callback: mockCallback,
|
|
63
|
+
overFlowDirection: OverflowDirection.HORIZONTAL,
|
|
64
|
+
debounce: 1,
|
|
65
|
+
deltaSize: 1,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const withNoOverFlow = new OverflowWatcher(params);
|
|
69
|
+
setPrivateField(withNoOverFlow, '_lastContainerSize', 100);
|
|
70
|
+
setPrivateField(withNoOverFlow, '_lastContainerInstance', mockElement);
|
|
71
|
+
|
|
72
|
+
withNoOverFlow.forceUpdate();
|
|
73
|
+
|
|
74
|
+
const childSpan = mockElement.children[1];
|
|
75
|
+
expect(mockCallback).toHaveBeenCalledWith(expect.arrayContaining([childSpan]));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('Should call callback on forceUpdate with childSpan considering overflowed elements', () => {
|
|
79
|
+
appendMockedChildren(mockElement);
|
|
80
|
+
const childButton = mockElement.children[0];
|
|
81
|
+
const childSpan = mockElement.children[1];
|
|
82
|
+
childSpan.classList.add(OVERFLOWED_CLASS_NAME);
|
|
83
|
+
|
|
84
|
+
const hiddemItemsMock: Map<Element, {size: number, margin: number}> = new Map();
|
|
85
|
+
hiddemItemsMock.set(childSpan, {size: 50, margin: 0});
|
|
86
|
+
hiddemItemsMock.set(childButton, {size: 0, margin: 50});
|
|
87
|
+
setPrivateField(watcherInstance, '_hiddenItemsProps', hiddemItemsMock);
|
|
88
|
+
|
|
89
|
+
watcherInstance.forceUpdate();
|
|
90
|
+
|
|
91
|
+
expect(mockCallback).toHaveBeenCalledWith(expect.arrayContaining([childSpan]));
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('Should call callback on forceUpdate with empty list', () => {
|
|
95
|
+
appendMockedChildren(mockElement);
|
|
96
|
+
setPrivateField(watcherInstance, '_lastContainerSize', 200);
|
|
97
|
+
watcherInstance.forceUpdate();
|
|
98
|
+
const childButton = mockElement.children[0];
|
|
99
|
+
const childSpan = mockElement.children[1];
|
|
100
|
+
|
|
101
|
+
expect(mockCallback).not.toHaveBeenCalledWith(expect.arrayContaining([childButton, childSpan]));
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should ignore elements that can not overflow', () => {
|
|
105
|
+
appendMockedChildren(mockElement);
|
|
106
|
+
|
|
107
|
+
const notOverflowElement = document.createElement('div');
|
|
108
|
+
notOverflowElement.setAttribute('data-element-id', 'testId') ;
|
|
109
|
+
notOverflowElement.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(50));
|
|
110
|
+
|
|
111
|
+
mockElement.appendChild(notOverflowElement);
|
|
112
|
+
watcherInstance.forceUpdate();
|
|
113
|
+
|
|
114
|
+
expect(mockCallback).not.toHaveBeenCalledWith(expect.arrayContaining([notOverflowElement]));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('Should not call callback on forceUpdate', () => {
|
|
118
|
+
watcherInstance.forceUpdate();
|
|
119
|
+
expect(mockCallback).not.toHaveBeenCalled();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
function appendMockedChildren(mockElement: HTMLElement) {
|
|
125
|
+
const chilldButton = document.createElement('button');
|
|
126
|
+
const childSpan = document.createElement('span');
|
|
127
|
+
|
|
128
|
+
chilldButton.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(50));
|
|
129
|
+
childSpan.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(50));
|
|
130
|
+
|
|
131
|
+
mockElement.appendChild(chilldButton);
|
|
132
|
+
mockElement.appendChild(childSpan);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function getMockedRectClientValue(size: number) {
|
|
136
|
+
return {
|
|
137
|
+
x: 0, y: 0, width: size, height: 10, top: 0, right: 0, bottom: 0, left: 0, toJSON: () => {}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function setPrivateField<T>(instance: T, fieldName: string, value: any): void {
|
|
142
|
+
(instance as any)[fieldName] = value;
|
|
143
|
+
}
|