@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.
Files changed (54) hide show
  1. package/.docs/classes/Change.md +11 -11
  2. package/.docs/classes/DataUnit.md +145 -115
  3. package/.docs/classes/ObjectUtils.md +48 -0
  4. package/.docs/classes/OverflowWatcher.md +310 -46
  5. package/.docs/classes/SelectionInfo.md +11 -11
  6. package/.docs/enumerations/ChangeOperation.md +4 -4
  7. package/.docs/enumerations/SelectionMode.md +2 -2
  8. package/.docs/globals.md +6 -0
  9. package/.docs/interfaces/DUActionInterceptor.md +1 -1
  10. package/.docs/interfaces/OverFlowWatcherParams.md +67 -0
  11. package/.docs/interfaces/PageRequest.md +3 -3
  12. package/.docs/interfaces/QuickFilter.md +3 -3
  13. package/.docs/interfaces/Record.md +4 -4
  14. package/.docs/interfaces/SavedRecord.md +5 -5
  15. package/.docs/interfaces/WaitingChange.md +3 -3
  16. package/.docs/type-aliases/DataUnitEventOptions.md +17 -0
  17. package/.docs/variables/OVERFLOWED_CLASS_NAME.md +13 -0
  18. package/dist/dataunit/DataUnit.d.ts +7 -2
  19. package/dist/dataunit/DataUnit.js +31 -20
  20. package/dist/dataunit/DataUnit.js.map +1 -1
  21. package/dist/dataunit/formatting/PrettyFormatter.js +7 -5
  22. package/dist/dataunit/formatting/PrettyFormatter.js.map +1 -1
  23. package/dist/dataunit/metadata/DataType.js +3 -0
  24. package/dist/dataunit/metadata/DataType.js.map +1 -1
  25. package/dist/dataunit/state/slice/SelectionSlice.js +1 -1
  26. package/dist/dataunit/state/slice/SelectionSlice.js.map +1 -1
  27. package/dist/index.d.ts +3 -3
  28. package/dist/index.js +2 -2
  29. package/dist/index.js.map +1 -1
  30. package/dist/utils/ElementUtils.d.ts +2 -0
  31. package/dist/utils/ElementUtils.js +9 -0
  32. package/dist/utils/ElementUtils.js.map +1 -0
  33. package/dist/utils/ObjectUtils.d.ts +14 -0
  34. package/dist/utils/ObjectUtils.js +18 -0
  35. package/dist/utils/ObjectUtils.js.map +1 -1
  36. package/dist/utils/OverflowWatcher/index.d.ts +35 -7
  37. package/dist/utils/OverflowWatcher/index.js +140 -59
  38. package/dist/utils/OverflowWatcher/index.js.map +1 -1
  39. package/jest.config.ts +2 -0
  40. package/package.json +2 -1
  41. package/reports/test-report.xml +151 -0
  42. package/setupTests.js +7 -0
  43. package/sonar-project.properties +6 -3
  44. package/src/dataunit/DataUnit.ts +35 -21
  45. package/src/dataunit/formatting/PrettyFormatter.ts +6 -5
  46. package/src/dataunit/metadata/DataType.ts +3 -0
  47. package/src/dataunit/state/slice/SelectionSlice.ts +1 -1
  48. package/src/index.ts +6 -3
  49. package/src/utils/ElementUtils.ts +10 -0
  50. package/src/utils/ObjectUtils.ts +20 -0
  51. package/src/utils/OverflowWatcher/index.ts +170 -78
  52. package/test/dataunit/formatting/PrettyFormatter.spec.ts +177 -0
  53. package/test/util/ElementUtils.spec.ts +34 -0
  54. package/test/util/OverflowWatcher.spec.ts +152 -118
@@ -1,118 +1,152 @@
1
- import OverFlowWatcher, { OnOverflowCallBack, OverflowDirection } from '../../src/utils/OverflowWatcher';
2
-
3
- describe('OverflowWatcher', function () {
4
-
5
- let element: HTMLElement;
6
- let callbackHorizontalDirection: OnOverflowCallBack;
7
- let watcherHorizontalDirection: OverFlowWatcher;
8
- let callbackVerticalDirection: OnOverflowCallBack;
9
- let watcherVerticallDirection: OverFlowWatcher;
10
- let child1:HTMLDivElement;
11
- let child2:HTMLDivElement;
12
-
13
- beforeEach(() => {
14
-
15
- global.ResizeObserver = jest.fn().mockImplementation(() => ({
16
- observe: jest.fn(),
17
- unobserve: jest.fn(),
18
- disconnect: jest.fn(),
19
- }))
20
-
21
- element = document.createElement('div');
22
-
23
- //Horizontal
24
- callbackHorizontalDirection = jest.fn();
25
- watcherHorizontalDirection = new OverFlowWatcher(element, callbackHorizontalDirection);
26
-
27
-
28
- //Vertical
29
- callbackVerticalDirection = jest.fn();
30
- watcherVerticallDirection = new OverFlowWatcher(element, callbackVerticalDirection, OverflowDirection.VERTICAL);
31
-
32
- child1 = document.createElement('div');
33
- child2 = document.createElement('div');
34
-
35
- child1.style.width = '50px';
36
- child2.style.width = '100px';
37
- child1.style.height = '50px';
38
- child2.style.height = '100px';
39
-
40
- //TODO: No futuro utilizar JSDOM para não precisar fazer mock dessas coisas
41
- child1['getBoundingClientRect'] = () => {
42
- return {
43
- width: 50,
44
- height: 50,
45
- } as DOMRect;
46
- }
47
-
48
- child2['getBoundingClientRect'] = () => {
49
- return {
50
- width: 100,
51
- height: 100,
52
- } as DOMRect;
53
- }
54
-
55
- element.appendChild(child1);
56
- element.appendChild(child2);
57
- });
58
-
59
- afterEach(() => {
60
- watcherHorizontalDirection.destroy();
61
- watcherVerticallDirection.destroy();
62
- });
63
-
64
- test('constructor initializes properties correctly', () => {
65
- expect(watcherHorizontalDirection['_onResize']).toBe(callbackHorizontalDirection);
66
- expect(watcherHorizontalDirection['_scrollDirection']).toBe(OverflowDirection.HORIZONTAL);
67
- expect(watcherHorizontalDirection['_deltaSize']).toBe(10);
68
- });
69
-
70
- test('handleResize horizontal calls callback with hidden items', () => {
71
- const entries: any[] = [{
72
- contentRect: {
73
- width: 120
74
- },
75
- target: element
76
- }];
77
-
78
- watcherHorizontalDirection['handleResize'](entries);
79
-
80
- expect(callbackHorizontalDirection).toHaveBeenCalledWith([child2]);
81
-
82
- entries[0].contentRect.width = 100;
83
- watcherHorizontalDirection['handleResize'](entries);
84
-
85
- expect(callbackHorizontalDirection).toHaveBeenCalledWith([child2]);
86
- });
87
-
88
- test('handleResize vertical calls callback with hidden items', () => {
89
- const entries: any[] = [{
90
- contentRect: {
91
- height: 120
92
- },
93
- target: element
94
- }];
95
-
96
- watcherVerticallDirection['handleResize'](entries);
97
-
98
- expect(callbackVerticalDirection).toHaveBeenCalledWith([child2]);
99
- });
100
-
101
- test('isChangedSize returns true if no previous size', () => {
102
- const newContentRect: DOMRectReadOnly = { width: 100 } as DOMRectReadOnly;
103
- watcherHorizontalDirection['_lastContentRect'] = undefined;
104
-
105
- const result = watcherHorizontalDirection['isChangedSize'](newContentRect);
106
-
107
- expect(result).toBe(true);
108
- });
109
-
110
- test('prevent resize without entries', () => {
111
- const entries: any[] = [];
112
-
113
- watcherHorizontalDirection['handleResize'](entries);
114
-
115
- expect(callbackHorizontalDirection).toHaveBeenCalledTimes(0);
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
+ const notOverflowElement2 = document.createElement('div');
112
+ notOverflowElement2.setAttribute('data-element-id', 'testId2') ;
113
+ notOverflowElement2.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(50));
114
+
115
+ mockElement.appendChild(notOverflowElement);
116
+ mockElement.appendChild(notOverflowElement2);
117
+
118
+ watcherInstance.addNotOverFlowElement('testId');
119
+ watcherInstance.addNotOverFlowElement('testId2');
120
+
121
+ watcherInstance.forceUpdate();
122
+
123
+ expect(mockCallback).not.toHaveBeenCalledWith(expect.arrayContaining([notOverflowElement]));
124
+ });
125
+
126
+ it('Should not call callback on forceUpdate', () => {
127
+ watcherInstance.forceUpdate();
128
+ expect(mockCallback).not.toHaveBeenCalled();
129
+ });
130
+
131
+ });
132
+
133
+ function appendMockedChildren(mockElement: HTMLElement) {
134
+ const chilldButton = document.createElement('button');
135
+ const childSpan = document.createElement('span');
136
+
137
+ chilldButton.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(50));
138
+ childSpan.getBoundingClientRect = jest.fn().mockReturnValue(getMockedRectClientValue(50));
139
+
140
+ mockElement.appendChild(chilldButton);
141
+ mockElement.appendChild(childSpan);
142
+ }
143
+
144
+ function getMockedRectClientValue(size: number) {
145
+ return {
146
+ x: 0, y: 0, width: size, height: 10, top: 0, right: 0, bottom: 0, left: 0, toJSON: () => {}
147
+ };
148
+ }
149
+
150
+ function setPrivateField<T>(instance: T, fieldName: string, value: any): void {
151
+ (instance as any)[fieldName] = value;
152
+ }