Skip to content

Commit 418a65f

Browse files
committed
fix: make sure props.params is taken into account, add test
1 parent fbe3aa8 commit 418a65f

File tree

2 files changed

+116
-146
lines changed

2 files changed

+116
-146
lines changed

β€Žsrc/lib/index.test.tsx

Lines changed: 84 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,32 @@ import { vi } from 'vitest';
55
import LiteYouTubeEmbed from './index';
66

77
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' };
129

1310
test('renders with default props', () => {
1411
const { container } = render(<LiteYouTubeEmbed {...defaultProps} />);
15-
12+
1613
// Check if the component renders with the correct title as data attribute
1714
const article = container.querySelector('article');
1815
expect(article).toHaveAttribute('data-title', defaultProps.title);
19-
16+
2017
// Check if the play button is rendered
2118
const playButton = screen.getByRole('button');
2219
expect(playButton).toBeInTheDocument();
2320
expect(playButton).toHaveAttribute('aria-label', `Watch ${defaultProps.title}`);
24-
21+
2522
// Check if iframe is not rendered initially
2623
const iframe = screen.queryByTitle(defaultProps.title);
2724
expect(iframe).not.toBeInTheDocument();
2825
});
2926

3027
test('loads iframe when clicked', () => {
3128
const { container } = render(<LiteYouTubeEmbed {...defaultProps} />);
32-
29+
3330
// Click the play button
3431
const playButton = screen.getByRole('button');
3532
fireEvent.click(playButton);
36-
33+
3734
// Check if iframe is rendered after click
3835
const iframe = container.querySelector('iframe');
3936
expect(iframe).not.toBeNull();
@@ -43,66 +40,51 @@ describe('LiteYouTubeEmbed', () => {
4340

4441
test('preconnects when hovered', () => {
4542
const { container } = render(<LiteYouTubeEmbed {...defaultProps} />);
46-
43+
4744
// Hover over the container
4845
const article = container.querySelector('article');
4946
expect(article).not.toBeNull();
5047
if (article) {
5148
fireEvent.pointerOver(article);
52-
49+
5350
// Check if preconnect links are added
5451
const preconnectLinks = document.querySelectorAll('link[rel="preconnect"]');
5552
expect(preconnectLinks.length).toBeGreaterThan(0);
56-
53+
5754
// Check if YouTube domain is preconnected
5855
const ytPreconnect = document.querySelector('link[rel="preconnect"][href="https://www.youtube-nocookie.com"]');
5956
expect(ytPreconnect).toBeInTheDocument();
6057
}
6158
});
6259

6360
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+
6963
const { container } = render(<LiteYouTubeEmbed {...customProps} />);
70-
64+
7165
// Check if the background image URL contains the custom resolution
7266
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') });
7668
});
7769

7870
test('renders with webp format', () => {
79-
const webpProps = {
80-
...defaultProps,
81-
webp: true,
82-
};
83-
71+
const webpProps = { ...defaultProps, webp: true };
72+
8473
const { container } = render(<LiteYouTubeEmbed {...webpProps} />);
85-
74+
8675
// Check if the background image URL uses webp format
8776
const article = container.querySelector('article');
88-
expect(article).toHaveStyle({
89-
backgroundImage: expect.stringContaining('.webp'),
90-
});
77+
expect(article).toHaveStyle({ backgroundImage: expect.stringContaining('.webp') });
9178
});
9279

9380
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+
9983
const { container } = render(<LiteYouTubeEmbed {...customThumbnailProps} />);
100-
84+
10185
// Check if the background image URL is the custom thumbnail
10286
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)' });
10688
});
10789

10890
test('renders with custom classes', () => {
@@ -113,77 +95,71 @@ describe('LiteYouTubeEmbed', () => {
11395
iframeClass: 'custom-iframe',
11496
activatedClass: 'custom-activated',
11597
};
116-
98+
11799
const { container } = render(<LiteYouTubeEmbed {...customClassProps} />);
118-
100+
119101
// Check if custom wrapper class is applied
120102
const wrapper = container.querySelector('.custom-wrapper');
121103
expect(wrapper).not.toBeNull();
122-
104+
123105
// Check if custom player class is applied
124106
const player = container.querySelector('.custom-player');
125107
expect(player).not.toBeNull();
126-
108+
127109
if (player) {
128110
// Click to load iframe
129111
fireEvent.click(player);
130-
112+
131113
// Check if custom iframe class is applied
132114
const iframe = container.querySelector('.custom-iframe');
133115
expect(iframe).not.toBeNull();
134-
116+
135117
// Check if activated class is applied after click
136118
expect(wrapper).toHaveClass('custom-activated');
137119
}
138120
});
139121

140122
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+
147125
const { container } = render(<LiteYouTubeEmbed {...aspectRatioProps} />);
148-
126+
149127
// Check if custom aspect ratio is applied
150128
const article = container.querySelector('article');
151-
expect(article).toHaveStyle({
152-
'--aspect-ratio': '133.33333333333331%',
153-
});
129+
expect(article).toHaveStyle({ '--aspect-ratio': '133.33333333333331%' });
154130
});
155131

156132
test('renders with adNetwork enabled', () => {
157133
const { container } = render(<LiteYouTubeEmbed {...defaultProps} adNetwork />);
158-
134+
159135
// Trigger preconnect
160136
const article = container.querySelector('article');
161137
expect(article).not.toBeNull();
162138
if (article) {
163139
fireEvent.pointerOver(article);
164-
140+
165141
// 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+
);
167145
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+
);
170150
expect(googleAdsPreconnect).toBeInTheDocument();
171151
}
172152
});
173153

174154
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+
181157
render(<LiteYouTubeEmbed {...playlistProps} />);
182-
158+
183159
// Click to load iframe
184160
const playButton = screen.getByRole('button');
185161
fireEvent.click(playButton);
186-
162+
187163
// Check if iframe src contains videoseries
188164
const iframe = screen.getByTitle(playlistProps.title);
189165
expect(iframe).toHaveAttribute('src', expect.stringContaining('videoseries'));
@@ -192,102 +168,96 @@ describe('LiteYouTubeEmbed', () => {
192168

193169
test('renders with always load iframe', () => {
194170
render(<LiteYouTubeEmbed {...defaultProps} alwaysLoadIframe />);
195-
171+
196172
// Check if iframe is rendered initially without clicking
197173
const iframe = screen.getByTitle(defaultProps.title);
198174
expect(iframe).toBeInTheDocument();
199-
175+
200176
// Check that autoplay is not set when alwaysLoadIframe is true
201177
expect(iframe).toHaveAttribute('src', expect.not.stringContaining('autoplay=1'));
202178
});
203179

204180
test('calls onIframeAdded callback when iframe is added', () => {
205181
const onIframeAdded = vi.fn();
206182
render(<LiteYouTubeEmbed {...defaultProps} onIframeAdded={onIframeAdded} />);
207-
183+
208184
// Click to load iframe
209185
const playButton = screen.getByRole('button');
210186
fireEvent.click(playButton);
211-
187+
212188
// Check if callback was called
213189
expect(onIframeAdded).toHaveBeenCalledTimes(1);
214190
});
215191

216192
test('renders with custom container element', () => {
217193
const { container } = render(<LiteYouTubeEmbed {...defaultProps} containerElement="section" />);
218-
194+
219195
// Check if section element is used instead of default article
220196
const section = container.querySelector('section');
221197
expect(section).toBeInTheDocument();
222198
});
223199

224200
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+
230203
const { container } = render(<LiteYouTubeEmbed {...defaultProps} style={customStyle} />);
231-
204+
232205
// Check if custom styles are applied
233206
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' });
238208
});
239209

240210
test('renders with muted parameter', () => {
241211
render(<LiteYouTubeEmbed {...defaultProps} muted />);
242-
212+
243213
// Click to load iframe
244214
const playButton = screen.getByRole('button');
245215
fireEvent.click(playButton);
246-
216+
247217
// Check if iframe src contains mute=1
248218
const iframe = screen.getByTitle(defaultProps.title);
249219
expect(iframe).toHaveAttribute('src', expect.stringContaining('mute=1'));
250220
});
251221

252222
test('renders with enableJsApi parameter', () => {
253223
render(<LiteYouTubeEmbed {...defaultProps} enableJsApi />);
254-
224+
255225
// Click to load iframe
256226
const playButton = screen.getByRole('button');
257227
fireEvent.click(playButton);
258-
228+
259229
// Check if iframe src contains enablejsapi=1
260230
const iframe = screen.getByTitle(defaultProps.title);
261231
expect(iframe).toHaveAttribute('src', expect.stringContaining('enablejsapi=1'));
262232
});
263233

264234
test('renders with cookie parameter', () => {
265235
render(<LiteYouTubeEmbed {...defaultProps} cookie />);
266-
236+
267237
// Click to load iframe
268238
const playButton = screen.getByRole('button');
269239
fireEvent.click(playButton);
270-
240+
271241
// Check if iframe src uses youtube.com instead of youtube-nocookie.com
272242
const iframe = screen.getByTitle(defaultProps.title);
273243
expect(iframe).toHaveAttribute('src', expect.stringContaining('https://www.youtube.com/embed/'));
274244
});
275245

276246
test('renders with noCookie parameter', () => {
277247
render(<LiteYouTubeEmbed {...defaultProps} noCookie />);
278-
248+
279249
// Click to load iframe
280250
const playButton = screen.getByRole('button');
281251
fireEvent.click(playButton);
282-
252+
283253
// Check if iframe src uses youtube-nocookie.com
284254
const iframe = screen.getByTitle(defaultProps.title);
285255
expect(iframe).toHaveAttribute('src', expect.stringContaining('https://www.youtube-nocookie.com/embed/'));
286256
});
287257

288258
test('renders with custom announce text', () => {
289259
render(<LiteYouTubeEmbed {...defaultProps} announce="Play" />);
290-
260+
291261
// Check if play button has custom announce text
292262
const playButton = screen.getByRole('button');
293263
expect(playButton).toHaveAttribute('aria-label', `Play ${defaultProps.title}`);
@@ -296,16 +266,33 @@ describe('LiteYouTubeEmbed', () => {
296266
test('forwards ref to iframe element', () => {
297267
const ref = React.createRef<HTMLIFrameElement>();
298268
render(<LiteYouTubeEmbed {...defaultProps} ref={ref} />);
299-
300269
// Initially ref should be null as iframe is not rendered
301270
expect(ref.current).toBeNull();
302-
271+
303272
// Click to load iframe
304273
const playButton = screen.getByRole('button');
305274
fireEvent.click(playButton);
306-
275+
307276
// Now ref should point to iframe
308277
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');
310297
});
311-
});
298+
});

0 commit comments

Comments
Β (0)