Skip to content

Commit 7d3ac06

Browse files
WIP. check test for click subscription in events_engine (T1307313)"
This reverts commit d0d18c3.
1 parent 5f77a72 commit 7d3ac06

File tree

2 files changed

+48
-70
lines changed

2 files changed

+48
-70
lines changed
Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import eventsEngine from 'common/core/events/core/events_engine';
2-
import domAdapter from '__internal/core/m_dom_adapter';
3-
import { name as clickEventName } from 'common/core/events/click';
2+
import {
3+
subscribeNodesDisposing,
4+
unsubscribeNodesDisposing,
5+
} from '__internal/events/utils/m_event_nodes_disposing';
46

57
QUnit.testStart(function() {
68
const markup = '<button id="test-element">Test</button>';
@@ -10,48 +12,50 @@ QUnit.testStart(function() {
1012
}
1113
});
1214

13-
QUnit.module('event nodes disposing', {
14-
afterEach: function() {
15-
const document = domAdapter.getDocument();
16-
eventsEngine.off(document, clickEventName);
17-
}
18-
});
15+
QUnit.module('event nodes disposing');
1916

20-
QUnit.test('should not leak memory when subscribing to dxclick on document and clicking elements', function(assert) {
21-
const done = assert.async();
17+
QUnit.test('should not leave extra handlers in elementDataMap when using subscribeNodesDisposing and unsubscribeNodesDisposing for click on document', function(assert) {
18+
const clickEventName = 'click';
2219
const testElement = document.getElementById('test-element');
2320

24-
eventsEngine.on(document, clickEventName, () => {});
25-
26-
if(typeof globalThis !== 'undefined' && typeof globalThis.gc === 'function') {
27-
globalThis.gc();
28-
}
29-
30-
const initialMemory = performance.memory.usedJSHeapSize;
31-
32-
let i = 0;
33-
34-
const interval = setInterval(() => {
35-
testElement.click();
36-
i++;
37-
38-
if(i > 99) {
39-
setTimeout(() => {
40-
if(typeof globalThis !== 'undefined' && typeof globalThis.gc === 'function') {
41-
globalThis.gc();
42-
}
43-
44-
const finalMemory = performance.memory.usedJSHeapSize;
45-
const memoryDiff = finalMemory - initialMemory;
46-
47-
assert.ok(
48-
memoryDiff <= 0,
49-
`Memory should not leak. Memory diff: ${memoryDiff}B`
50-
);
51-
done();
52-
}, 50);
53-
54-
clearInterval(interval);
55-
}
56-
}, 50);
21+
// Get initial state of handleObjects for click event on document
22+
const initialElementData = eventsEngine.elementDataMap.get(document);
23+
const initialHandleObjectsCount = initialElementData && initialElementData[clickEventName]
24+
? initialElementData[clickEventName].handleObjects.length
25+
: 0;
26+
27+
// Create a click event by triggering a click
28+
const clickEvent = eventsEngine.Event('click', {
29+
target: testElement,
30+
currentTarget: document,
31+
delegateTarget: document.body
32+
});
33+
34+
// Callback for subscribeNodesDisposing
35+
const callback = function() {};
36+
37+
// Subscribe to nodes disposing using the click event
38+
const subscriptionData = subscribeNodesDisposing(clickEvent, callback);
39+
40+
// Get handleObjects count after subscribe
41+
const afterSubscribeElementData = eventsEngine.elementDataMap.get(document);
42+
const afterSubscribeHandleObjectsCount = afterSubscribeElementData && afterSubscribeElementData[clickEventName]
43+
? afterSubscribeElementData[clickEventName].handleObjects.length
44+
: 0;
45+
46+
// Unsubscribe
47+
unsubscribeNodesDisposing(clickEvent, callback, subscriptionData.nodes);
48+
49+
// Get final handleObjects count after unsubscribe
50+
const finalElementData = eventsEngine.elementDataMap.get(document);
51+
const finalHandleObjectsCount = finalElementData && finalElementData[clickEventName]
52+
? finalElementData[clickEventName].handleObjects.length
53+
: 0;
54+
55+
// Verify that handleObjects count for click event on document hasn't increased
56+
assert.equal(
57+
finalHandleObjectsCount,
58+
initialHandleObjectsCount,
59+
`HandleObjects count should return to initial state. Initial: ${initialHandleObjectsCount}, Final: ${finalHandleObjectsCount}`
60+
);
5761
});

packages/devextreme/testing/tests/DevExpress.ui.events/eventsEngine.tests.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ QUnit.test('removeEventListener should not be called if native handler is not ex
568568
delListener.restore();
569569
});
570570

571-
QUnit.module('Memory bugs');
571+
QUnit.module('Memory');
572572

573573
QUnit.test('removing subscriptions should remove data from elementDataMap', function(assert) {
574574
const div = document.createElement('div');
@@ -591,32 +591,6 @@ QUnit.test('removing subscriptions should not remove data from elementDataMap if
591591
assert.equal(eventsEngine.elementDataMap.has(div), hasData);
592592
});
593593

594-
QUnit.test('should not leak memory when subscribing on document and clicking elements (T1307313)', function(assert) {
595-
const clickEventName = 'dxclick';
596-
const fixture = document.getElementById('qunit-fixture');
597-
598-
fixture.innerHTML = '<button id="test-element">Test</button>';
599-
600-
const testElement = document.getElementById('test-element');
601-
602-
eventsEngine.on(document, clickEventName, () => {});
603-
604-
for(let i = 0; i < 100; i++) {
605-
testElement.click();
606-
}
607-
608-
const eventData = eventsEngine.elementDataMap.get(document);
609-
610-
const count = Object.keys(eventData).reduce((res, key) => res + eventData[key].handleObjects?.length, 0);
611-
612-
assert.ok(
613-
eventData?.length <= 5,
614-
`Memory should not leak. Element data: ${count}, ${Object.keys(eventData)}, ${eventsEngine.elementDataMap.size}`
615-
);
616-
617-
eventsEngine.off(document);
618-
});
619-
620594
QUnit.module('Strategy');
621595

622596
QUnit.test('it should be possible to set only one method for strategy', function(assert) {

0 commit comments

Comments
 (0)