@@ -9,35 +9,74 @@ define([
99 'use strict' ;
1010
1111 describe ( 'Testing Youtube player Widget' , function ( ) {
12- var wdContainer ;
12+ var wdContainer , video , widget ;
13+
14+ beforeAll ( function ( ) {
15+ // Global mocks to prevent browser-native errors
16+ spyOn ( window , 'open' ) . and . callFake ( ( ) => ( {
17+ focus : function ( ) { }
18+ } ) ) ;
19+
20+ if ( typeof navigator !== 'undefined' && ! navigator . share ) {
21+ Object . defineProperty ( navigator , 'share' , {
22+ value : ( ) => Promise . resolve ( ) ,
23+ writable : true
24+ } ) ;
25+ }
26+ } ) ;
1327
1428 beforeEach ( function ( ) {
29+ // Create DOM structure for widget
1530 wdContainer = $ (
1631 '<div>' +
1732 '<div class="video-information uploader"><span></span></div>' +
1833 '<div class="video-player-container">' +
1934 '<div class="product-video"></div>' +
2035 '</div>' +
21- '</div>' ) ;
22- } ) ;
23-
24- afterEach ( function ( ) {
25- $ ( wdContainer ) . remove ( ) ;
26- } ) ;
27-
28- it ( 'Widget does not stops player if player is no defined' , function ( ) {
29- var video = wdContainer . find ( '.video-player-container' ) . find ( '.product-video' ) ,
30- widget ;
36+ '</div>'
37+ ) . appendTo ( document . body ) ;
3138
39+ video = wdContainer . find ( '.product-video' ) ;
3240 video . videoYoutube ( ) ;
3341 widget = video . data ( 'mageVideoYoutube' ) ;
42+
43+ // Set spies
3444 widget . stop = jasmine . createSpy ( ) ;
3545 widget . _player = {
3646 destroy : jasmine . createSpy ( )
3747 } ;
38- widget . destroy ( ) ;
48+ } ) ;
49+
50+ afterEach ( function ( ) {
51+ // Properly destroy widget
52+ if ( widget && typeof widget . destroy === 'function' ) {
53+ widget . destroy ( ) ;
54+ }
55+
56+ // Remove leaked iframes
57+ document . querySelectorAll ( 'iframe' ) . forEach ( ( iframe ) => iframe . remove ( ) ) ;
58+
59+ // Clean up global YouTube API objects and scripts
60+ if ( window . YT && window . YT . Player ) {
61+ delete window . YT ;
62+ }
63+ if ( window . onYouTubeIframeAPIReady ) {
64+ delete window . onYouTubeIframeAPIReady ;
65+ }
66+ document . querySelectorAll ( 'script[src*="youtube.com"]' ) . forEach ( ( s ) => s . remove ( ) ) ;
67+
68+ // Clean up DOM and variables
69+ wdContainer . remove ( ) ;
70+ wdContainer = null ;
71+ video = null ;
72+ widget = null ;
73+ } ) ;
74+
75+ it ( 'Widget does not stop player if player is not defined' , function ( ) {
76+ widget . destroy ( ) ; // First destroy call - will clean _player
3977 expect ( widget . _player ) . toBeUndefined ( ) ;
40- widget . destroy ( ) ;
78+
79+ widget . destroy ( ) ; // Second call - should trigger stop
4180 expect ( widget . stop ) . toHaveBeenCalledTimes ( 1 ) ;
4281 } ) ;
4382 } ) ;
0 commit comments