@@ -5,35 +5,32 @@ import { vi } from 'vitest';
5
5
import LiteYouTubeEmbed from './index' ;
6
6
7
7
describe ( 'LiteYouTubeEmbed' , ( ) => {
8
- const defaultProps = {
9
- id : 'dQw4w9WgXcQ' ,
10
- title : 'Rick Astley - Never Gonna Give You Up' ,
11
- } ;
8
+ const defaultProps = { id : 'dQw4w9WgXcQ' , title : 'Rick Astley - Never Gonna Give You Up' } ;
12
9
13
10
test ( 'renders with default props' , ( ) => {
14
11
const { container } = render ( < LiteYouTubeEmbed { ...defaultProps } /> ) ;
15
-
12
+
16
13
// Check if the component renders with the correct title as data attribute
17
14
const article = container . querySelector ( 'article' ) ;
18
15
expect ( article ) . toHaveAttribute ( 'data-title' , defaultProps . title ) ;
19
-
16
+
20
17
// Check if the play button is rendered
21
18
const playButton = screen . getByRole ( 'button' ) ;
22
19
expect ( playButton ) . toBeInTheDocument ( ) ;
23
20
expect ( playButton ) . toHaveAttribute ( 'aria-label' , `Watch ${ defaultProps . title } ` ) ;
24
-
21
+
25
22
// Check if iframe is not rendered initially
26
23
const iframe = screen . queryByTitle ( defaultProps . title ) ;
27
24
expect ( iframe ) . not . toBeInTheDocument ( ) ;
28
25
} ) ;
29
26
30
27
test ( 'loads iframe when clicked' , ( ) => {
31
28
const { container } = render ( < LiteYouTubeEmbed { ...defaultProps } /> ) ;
32
-
29
+
33
30
// Click the play button
34
31
const playButton = screen . getByRole ( 'button' ) ;
35
32
fireEvent . click ( playButton ) ;
36
-
33
+
37
34
// Check if iframe is rendered after click
38
35
const iframe = container . querySelector ( 'iframe' ) ;
39
36
expect ( iframe ) . not . toBeNull ( ) ;
@@ -43,66 +40,51 @@ describe('LiteYouTubeEmbed', () => {
43
40
44
41
test ( 'preconnects when hovered' , ( ) => {
45
42
const { container } = render ( < LiteYouTubeEmbed { ...defaultProps } /> ) ;
46
-
43
+
47
44
// Hover over the container
48
45
const article = container . querySelector ( 'article' ) ;
49
46
expect ( article ) . not . toBeNull ( ) ;
50
47
if ( article ) {
51
48
fireEvent . pointerOver ( article ) ;
52
-
49
+
53
50
// Check if preconnect links are added
54
51
const preconnectLinks = document . querySelectorAll ( 'link[rel="preconnect"]' ) ;
55
52
expect ( preconnectLinks . length ) . toBeGreaterThan ( 0 ) ;
56
-
53
+
57
54
// Check if YouTube domain is preconnected
58
55
const ytPreconnect = document . querySelector ( 'link[rel="preconnect"][href="https://www.youtube-nocookie.com"]' ) ;
59
56
expect ( ytPreconnect ) . toBeInTheDocument ( ) ;
60
57
}
61
58
} ) ;
62
59
63
60
test ( 'renders with custom poster resolution' , ( ) => {
64
- const customProps = {
65
- ...defaultProps ,
66
- poster : 'maxresdefault' ,
67
- } ;
68
-
61
+ const customProps = { ...defaultProps , poster : 'maxresdefault' as const } ;
62
+
69
63
const { container } = render ( < LiteYouTubeEmbed { ...customProps } /> ) ;
70
-
64
+
71
65
// Check if the background image URL contains the custom resolution
72
66
const article = container . querySelector ( 'article' ) ;
73
- expect ( article ) . toHaveStyle ( {
74
- backgroundImage : expect . stringContaining ( 'maxresdefault.jpg' ) ,
75
- } ) ;
67
+ expect ( article ) . toHaveStyle ( { backgroundImage : expect . stringContaining ( 'maxresdefault.jpg' ) } ) ;
76
68
} ) ;
77
69
78
70
test ( 'renders with webp format' , ( ) => {
79
- const webpProps = {
80
- ...defaultProps ,
81
- webp : true ,
82
- } ;
83
-
71
+ const webpProps = { ...defaultProps , webp : true } ;
72
+
84
73
const { container } = render ( < LiteYouTubeEmbed { ...webpProps } /> ) ;
85
-
74
+
86
75
// Check if the background image URL uses webp format
87
76
const article = container . querySelector ( 'article' ) ;
88
- expect ( article ) . toHaveStyle ( {
89
- backgroundImage : expect . stringContaining ( '.webp' ) ,
90
- } ) ;
77
+ expect ( article ) . toHaveStyle ( { backgroundImage : expect . stringContaining ( '.webp' ) } ) ;
91
78
} ) ;
92
79
93
80
test ( 'renders with custom thumbnail' , ( ) => {
94
- const customThumbnailProps = {
95
- ...defaultProps ,
96
- thumbnail : 'https://example.com/custom-thumbnail.jpg' ,
97
- } ;
98
-
81
+ const customThumbnailProps = { ...defaultProps , thumbnail : 'https://example.com/custom-thumbnail.jpg' } ;
82
+
99
83
const { container } = render ( < LiteYouTubeEmbed { ...customThumbnailProps } /> ) ;
100
-
84
+
101
85
// Check if the background image URL is the custom thumbnail
102
86
const article = container . querySelector ( 'article' ) ;
103
- expect ( article ) . toHaveStyle ( {
104
- backgroundImage : 'url(https://example.com/custom-thumbnail.jpg)' ,
105
- } ) ;
87
+ expect ( article ) . toHaveStyle ( { backgroundImage : 'url(https://example.com/custom-thumbnail.jpg)' } ) ;
106
88
} ) ;
107
89
108
90
test ( 'renders with custom classes' , ( ) => {
@@ -113,77 +95,71 @@ describe('LiteYouTubeEmbed', () => {
113
95
iframeClass : 'custom-iframe' ,
114
96
activatedClass : 'custom-activated' ,
115
97
} ;
116
-
98
+
117
99
const { container } = render ( < LiteYouTubeEmbed { ...customClassProps } /> ) ;
118
-
100
+
119
101
// Check if custom wrapper class is applied
120
102
const wrapper = container . querySelector ( '.custom-wrapper' ) ;
121
103
expect ( wrapper ) . not . toBeNull ( ) ;
122
-
104
+
123
105
// Check if custom player class is applied
124
106
const player = container . querySelector ( '.custom-player' ) ;
125
107
expect ( player ) . not . toBeNull ( ) ;
126
-
108
+
127
109
if ( player ) {
128
110
// Click to load iframe
129
111
fireEvent . click ( player ) ;
130
-
112
+
131
113
// Check if custom iframe class is applied
132
114
const iframe = container . querySelector ( '.custom-iframe' ) ;
133
115
expect ( iframe ) . not . toBeNull ( ) ;
134
-
116
+
135
117
// Check if activated class is applied after click
136
118
expect ( wrapper ) . toHaveClass ( 'custom-activated' ) ;
137
119
}
138
120
} ) ;
139
121
140
122
test ( 'renders with custom aspect ratio' , ( ) => {
141
- const aspectRatioProps = {
142
- ...defaultProps ,
143
- aspectHeight : 4 ,
144
- aspectWidth : 3 ,
145
- } ;
146
-
123
+ const aspectRatioProps = { ...defaultProps , aspectHeight : 4 , aspectWidth : 3 } ;
124
+
147
125
const { container } = render ( < LiteYouTubeEmbed { ...aspectRatioProps } /> ) ;
148
-
126
+
149
127
// Check if custom aspect ratio is applied
150
128
const article = container . querySelector ( 'article' ) ;
151
- expect ( article ) . toHaveStyle ( {
152
- '--aspect-ratio' : '133.33333333333331%' ,
153
- } ) ;
129
+ expect ( article ) . toHaveStyle ( { '--aspect-ratio' : '133.33333333333331%' } ) ;
154
130
} ) ;
155
131
156
132
test ( 'renders with adNetwork enabled' , ( ) => {
157
133
const { container } = render ( < LiteYouTubeEmbed { ...defaultProps } adNetwork /> ) ;
158
-
134
+
159
135
// Trigger preconnect
160
136
const article = container . querySelector ( 'article' ) ;
161
137
expect ( article ) . not . toBeNull ( ) ;
162
138
if ( article ) {
163
139
fireEvent . pointerOver ( article ) ;
164
-
140
+
165
141
// Check if ad network domains are preconnected
166
- const doubleClickPreconnect = document . querySelector ( 'link[rel="preconnect"][href="https://static.doubleclick.net"]' ) ;
142
+ const doubleClickPreconnect = document . querySelector (
143
+ 'link[rel="preconnect"][href="https://static.doubleclick.net"]'
144
+ ) ;
167
145
expect ( doubleClickPreconnect ) . toBeInTheDocument ( ) ;
168
-
169
- const googleAdsPreconnect = document . querySelector ( 'link[rel="preconnect"][href="https://googleads.g.doubleclick.net"]' ) ;
146
+
147
+ const googleAdsPreconnect = document . querySelector (
148
+ 'link[rel="preconnect"][href="https://googleads.g.doubleclick.net"]'
149
+ ) ;
170
150
expect ( googleAdsPreconnect ) . toBeInTheDocument ( ) ;
171
151
}
172
152
} ) ;
173
153
174
154
test ( 'renders with playlist mode' , ( ) => {
175
- const playlistProps = {
176
- ...defaultProps ,
177
- playlist : true ,
178
- playlistCoverId : 'dQw4w9WgXcQ' ,
179
- } ;
180
-
155
+ const playlistProps = { ...defaultProps , playlist : true , playlistCoverId : 'dQw4w9WgXcQ' } ;
156
+
181
157
render ( < LiteYouTubeEmbed { ...playlistProps } /> ) ;
182
-
158
+
183
159
// Click to load iframe
184
160
const playButton = screen . getByRole ( 'button' ) ;
185
161
fireEvent . click ( playButton ) ;
186
-
162
+
187
163
// Check if iframe src contains videoseries
188
164
const iframe = screen . getByTitle ( playlistProps . title ) ;
189
165
expect ( iframe ) . toHaveAttribute ( 'src' , expect . stringContaining ( 'videoseries' ) ) ;
@@ -192,102 +168,96 @@ describe('LiteYouTubeEmbed', () => {
192
168
193
169
test ( 'renders with always load iframe' , ( ) => {
194
170
render ( < LiteYouTubeEmbed { ...defaultProps } alwaysLoadIframe /> ) ;
195
-
171
+
196
172
// Check if iframe is rendered initially without clicking
197
173
const iframe = screen . getByTitle ( defaultProps . title ) ;
198
174
expect ( iframe ) . toBeInTheDocument ( ) ;
199
-
175
+
200
176
// Check that autoplay is not set when alwaysLoadIframe is true
201
177
expect ( iframe ) . toHaveAttribute ( 'src' , expect . not . stringContaining ( 'autoplay=1' ) ) ;
202
178
} ) ;
203
179
204
180
test ( 'calls onIframeAdded callback when iframe is added' , ( ) => {
205
181
const onIframeAdded = vi . fn ( ) ;
206
182
render ( < LiteYouTubeEmbed { ...defaultProps } onIframeAdded = { onIframeAdded } /> ) ;
207
-
183
+
208
184
// Click to load iframe
209
185
const playButton = screen . getByRole ( 'button' ) ;
210
186
fireEvent . click ( playButton ) ;
211
-
187
+
212
188
// Check if callback was called
213
189
expect ( onIframeAdded ) . toHaveBeenCalledTimes ( 1 ) ;
214
190
} ) ;
215
191
216
192
test ( 'renders with custom container element' , ( ) => {
217
193
const { container } = render ( < LiteYouTubeEmbed { ...defaultProps } containerElement = "section" /> ) ;
218
-
194
+
219
195
// Check if section element is used instead of default article
220
196
const section = container . querySelector ( 'section' ) ;
221
197
expect ( section ) . toBeInTheDocument ( ) ;
222
198
} ) ;
223
199
224
200
test ( 'renders with custom style' , ( ) => {
225
- const customStyle = {
226
- border : '2px solid red' ,
227
- borderRadius : '10px' ,
228
- } ;
229
-
201
+ const customStyle = { border : '2px solid red' , borderRadius : '10px' } ;
202
+
230
203
const { container } = render ( < LiteYouTubeEmbed { ...defaultProps } style = { customStyle } /> ) ;
231
-
204
+
232
205
// Check if custom styles are applied
233
206
const article = container . querySelector ( 'article' ) ;
234
- expect ( article ) . toHaveStyle ( {
235
- border : '2px solid red' ,
236
- borderRadius : '10px' ,
237
- } ) ;
207
+ expect ( article ) . toHaveStyle ( { border : '2px solid red' , borderRadius : '10px' } ) ;
238
208
} ) ;
239
209
240
210
test ( 'renders with muted parameter' , ( ) => {
241
211
render ( < LiteYouTubeEmbed { ...defaultProps } muted /> ) ;
242
-
212
+
243
213
// Click to load iframe
244
214
const playButton = screen . getByRole ( 'button' ) ;
245
215
fireEvent . click ( playButton ) ;
246
-
216
+
247
217
// Check if iframe src contains mute=1
248
218
const iframe = screen . getByTitle ( defaultProps . title ) ;
249
219
expect ( iframe ) . toHaveAttribute ( 'src' , expect . stringContaining ( 'mute=1' ) ) ;
250
220
} ) ;
251
221
252
222
test ( 'renders with enableJsApi parameter' , ( ) => {
253
223
render ( < LiteYouTubeEmbed { ...defaultProps } enableJsApi /> ) ;
254
-
224
+
255
225
// Click to load iframe
256
226
const playButton = screen . getByRole ( 'button' ) ;
257
227
fireEvent . click ( playButton ) ;
258
-
228
+
259
229
// Check if iframe src contains enablejsapi=1
260
230
const iframe = screen . getByTitle ( defaultProps . title ) ;
261
231
expect ( iframe ) . toHaveAttribute ( 'src' , expect . stringContaining ( 'enablejsapi=1' ) ) ;
262
232
} ) ;
263
233
264
234
test ( 'renders with cookie parameter' , ( ) => {
265
235
render ( < LiteYouTubeEmbed { ...defaultProps } cookie /> ) ;
266
-
236
+
267
237
// Click to load iframe
268
238
const playButton = screen . getByRole ( 'button' ) ;
269
239
fireEvent . click ( playButton ) ;
270
-
240
+
271
241
// Check if iframe src uses youtube.com instead of youtube-nocookie.com
272
242
const iframe = screen . getByTitle ( defaultProps . title ) ;
273
243
expect ( iframe ) . toHaveAttribute ( 'src' , expect . stringContaining ( 'https://www.youtube.com/embed/' ) ) ;
274
244
} ) ;
275
245
276
246
test ( 'renders with noCookie parameter' , ( ) => {
277
247
render ( < LiteYouTubeEmbed { ...defaultProps } noCookie /> ) ;
278
-
248
+
279
249
// Click to load iframe
280
250
const playButton = screen . getByRole ( 'button' ) ;
281
251
fireEvent . click ( playButton ) ;
282
-
252
+
283
253
// Check if iframe src uses youtube-nocookie.com
284
254
const iframe = screen . getByTitle ( defaultProps . title ) ;
285
255
expect ( iframe ) . toHaveAttribute ( 'src' , expect . stringContaining ( 'https://www.youtube-nocookie.com/embed/' ) ) ;
286
256
} ) ;
287
257
288
258
test ( 'renders with custom announce text' , ( ) => {
289
259
render ( < LiteYouTubeEmbed { ...defaultProps } announce = "Play" /> ) ;
290
-
260
+
291
261
// Check if play button has custom announce text
292
262
const playButton = screen . getByRole ( 'button' ) ;
293
263
expect ( playButton ) . toHaveAttribute ( 'aria-label' , `Play ${ defaultProps . title } ` ) ;
@@ -296,16 +266,33 @@ describe('LiteYouTubeEmbed', () => {
296
266
test ( 'forwards ref to iframe element' , ( ) => {
297
267
const ref = React . createRef < HTMLIFrameElement > ( ) ;
298
268
render ( < LiteYouTubeEmbed { ...defaultProps } ref = { ref } /> ) ;
299
-
300
269
// Initially ref should be null as iframe is not rendered
301
270
expect ( ref . current ) . toBeNull ( ) ;
302
-
271
+
303
272
// Click to load iframe
304
273
const playButton = screen . getByRole ( 'button' ) ;
305
274
fireEvent . click ( playButton ) ;
306
-
275
+
307
276
// Now ref should point to iframe
308
277
expect ( ref . current ) . not . toBeNull ( ) ;
309
- expect ( ref . current . tagName ) . toBe ( 'IFRAME' ) ;
278
+ expect ( ref . current ?. tagName ) . toBe ( 'IFRAME' ) ;
279
+ } ) ;
280
+
281
+ test ( 'renders iframe with params from props.params' , ( ) => {
282
+ const props = { ...defaultProps , params : '&other=value&another=value' } ;
283
+ const { container } = render ( < LiteYouTubeEmbed { ...props } /> ) ;
284
+
285
+ // Click the play button to load the iframe
286
+ const playButton = screen . getByRole ( 'button' ) ;
287
+ fireEvent . click ( playButton ) ;
288
+
289
+ // Check if iframe is rendered
290
+ const iframe = container . querySelector ( 'iframe' ) ;
291
+ expect ( iframe ) . not . toBeNull ( ) ;
292
+
293
+ // Check if iframe src contains the params from props.params
294
+ const iframeSrc = iframe ?. getAttribute ( 'src' ) ;
295
+ expect ( iframeSrc ) . toContain ( 'other=value' ) ;
296
+ expect ( iframeSrc ) . toContain ( 'another=value' ) ;
310
297
} ) ;
311
- } ) ;
298
+ } ) ;
0 commit comments