@@ -9,35 +9,74 @@ define([
9
9
'use strict' ;
10
10
11
11
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
+ } ) ;
13
27
14
28
beforeEach ( function ( ) {
29
+ // Create DOM structure for widget
15
30
wdContainer = $ (
16
31
'<div>' +
17
32
'<div class="video-information uploader"><span></span></div>' +
18
33
'<div class="video-player-container">' +
19
34
'<div class="product-video"></div>' +
20
35
'</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 ) ;
31
38
39
+ video = wdContainer . find ( '.product-video' ) ;
32
40
video . videoYoutube ( ) ;
33
41
widget = video . data ( 'mageVideoYoutube' ) ;
42
+
43
+ // Set spies
34
44
widget . stop = jasmine . createSpy ( ) ;
35
45
widget . _player = {
36
46
destroy : jasmine . createSpy ( )
37
47
} ;
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
39
77
expect ( widget . _player ) . toBeUndefined ( ) ;
40
- widget . destroy ( ) ;
78
+
79
+ widget . destroy ( ) ; // Second call - should trigger stop
41
80
expect ( widget . stop ) . toHaveBeenCalledTimes ( 1 ) ;
42
81
} ) ;
43
82
} ) ;
0 commit comments