Skip to content

Commit

Permalink
761-fix: Filter out private videos on mentorship page (#762)
Browse files Browse the repository at this point in the history
* fix: 761 - to filter out private videos

* refactor: 761 - add test case for private videos
  • Loading branch information
Quiddlee authored Jan 26, 2025
1 parent a52c93e commit e091d61
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/shared/ui/video-playlist-with-player/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const videoPrivacyStatus = {
private: 'private',
public: 'public',
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('VideoPlaylistWithPlayer Component', () => {
title: video.title,
thumbnails: { medium: { url: video.thumbnail } },
},
status: { privacyStatus: 'public' },
})),
}),
} as Response);
Expand All @@ -65,6 +66,7 @@ describe('VideoPlaylistWithPlayer Component', () => {
title: video.title,
thumbnails: { medium: { url: video.thumbnail } },
},
status: { privacyStatus: 'public' },
})),
}),
} as Response);
Expand Down Expand Up @@ -112,6 +114,7 @@ describe('VideoPlaylistWithPlayer Component', () => {
title: video.title,
thumbnails: { medium: { url: video.thumbnail } },
},
status: { privacyStatus: 'public' },
})),
}),
} as Response);
Expand Down Expand Up @@ -141,4 +144,38 @@ describe('VideoPlaylistWithPlayer Component', () => {
expect(playlistContainer).toHaveStyle({ maxHeight: '300px' });
});
});

it('should filter out private videos', async () => {
const publicVideos = MOCKED_VIDEOS.map((video) => ({
snippet: {
resourceId: { videoId: video.id },
title: video.title,
thumbnails: { medium: { url: video.thumbnail } },
},
status: { privacyStatus: 'public' },
}));
const privateVideos = MOCKED_VIDEOS.map((video) => ({
snippet: {
resourceId: { videoId: video.id },
title: video.title,
thumbnails: { medium: { url: video.thumbnail } },
},
status: { privacyStatus: 'private' },
}));

fetchMock.mockResolvedValue({
ok: true,
json: async () => ({ items: [...publicVideos, ...privateVideos] }),
} as Response);

render(<VideoPlaylistWithPlayer playlistId="testPlaylist" apiKey="testApiKey" />);

await waitFor(() => {
expect(screen.getByTestId('videos-container')).toBeVisible();
});

const videosContainer = screen.getByTestId('videos-container');

expect(videosContainer.children).toHaveLength(3);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames/bind';

import { videoPrivacyStatus } from './constants';
import { Playlist } from './playlist';
import type { Video } from '@/shared/types';
import { VideoPlayer } from '@/shared/ui/video-player';
Expand All @@ -29,7 +30,7 @@ export const VideoPlaylistWithPlayer = ({ playlistId, apiKey }: VideoPlaylistWit
setIsLoading(true);

try {
const apiUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=${playlistId}&key=${apiKey}`;
const apiUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,status&maxResults=50&playlistId=${playlistId}&key=${apiKey}`;
const response = await fetch(apiUrl);

if (!response.ok) {
Expand All @@ -39,7 +40,12 @@ export const VideoPlaylistWithPlayer = ({ playlistId, apiKey }: VideoPlaylistWit
}

const data = await response.json();
const videoItems: Video[] = data.items.map(
const publicVideos = data.items.filter(
(item: GoogleApiYouTubePlaylistItemResource) =>
item.status.privacyStatus === videoPrivacyStatus.public,
);

const videoItems: Video[] = publicVideos.map(
(item: GoogleApiYouTubePlaylistItemResource) => ({
id: item.snippet.resourceId.videoId,
title: item.snippet.title,
Expand Down

0 comments on commit e091d61

Please sign in to comment.