11import 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
57QUnit . 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} ) ;
0 commit comments