Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstankiewicz committed Jul 30, 2024
1 parent d26831f commit 42aa483
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/components/video/data/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useTranscripts({ player, customOptions }) {
}
};
fetchFn();
}, [customOptions.transcriptUrls, player, shouldUseTranscripts]);
}, [customOptions?.transcriptUrls, player, shouldUseTranscripts]);

return {
textTracks,
Expand Down
9 changes: 2 additions & 7 deletions src/components/video/data/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import { convertToWebVtt, createWebVttFile } from './utils';
const fetchAndAddTranscripts = async (transcriptUrls) => {
const data = {};
const transcriptPromises = Object.entries(transcriptUrls).map(([lang, url]) => fetch(url)
.then(response => {
if (!response.ok) {
logError(`Failed to fetch transcript for ${lang}`);
}
return response.json();
})
.then(transcriptData => {
.then(response => response.json())
.then((transcriptData) => {
const webVttData = convertToWebVtt(transcriptData);
const webVttFileUrl = createWebVttFile(webVttData);
data[lang] = webVttFileUrl;
Expand Down
98 changes: 22 additions & 76 deletions src/components/video/data/tests/service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,115 +20,61 @@ describe('fetchAndAddTranscripts', () => {
const mockTranscriptUrls = {
en: 'https://example.com/en-transcript.json',
};

const mockTranscriptData = {
items: ['example'],
};

const mockWebVttData = 'WEBVTT\n\n1\n00:00:00.000 --> 00:00:05.000\nExample subtitle';
const mockWebVttFileUrl = 'https://example.com/en-transcript.vtt';

global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockTranscriptData),
});

fetch.mockResponseOnce(JSON.stringify(mockTranscriptData));
convertToWebVtt.mockReturnValue(mockWebVttData);
createWebVttFile.mockReturnValue(mockWebVttFileUrl);

const player = {
addRemoteTextTrack: jest.fn(),
vjstranscribe: jest.fn(),
};

await fetchAndAddTranscripts(mockTranscriptUrls, player);
const result = await fetchAndAddTranscripts(mockTranscriptUrls);

expect(global.fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(convertToWebVtt).toHaveBeenCalledWith(mockTranscriptData);
expect(createWebVttFile).toHaveBeenCalledWith(mockWebVttData);
expect(player.addRemoteTextTrack).toHaveBeenCalledWith(
{
kind: 'subtitles',
src: mockWebVttFileUrl,
srclang: 'en',
label: 'en',
},
false,
);
expect(player.vjstranscribe).toHaveBeenCalledWith({
urls: [mockWebVttFileUrl],
});
});

it('should log an error if the transcript fetch fails', async () => {
const mockTranscriptUrls = {
en: 'https://example.com/en-transcript.json',
};

global.fetch = jest.fn().mockResolvedValue({
ok: false,
expect(result).toEqual({
en: mockWebVttFileUrl,
});

const player = {
addRemoteTextTrack: jest.fn(),
vjstranscribe: jest.fn(),
};

await fetchAndAddTranscripts(mockTranscriptUrls, player);

expect(global.fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(logError).toHaveBeenCalledWith('Failed to fetch transcript for en');
});

it('should log an error if JSON parsing or file creation fails', async () => {
it('should log an error if the transcript fetch, JSON parsing, or file creation fails', async () => {
const mockTranscriptUrls = {
en: 'https://example.com/en-transcript.json',
};
const error = new Error('failed to fetch!');
fetch.mockRejectOnce(error);

global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () => Promise.reject(new Error('Parsing error')),
});

const player = {
addRemoteTextTrack: jest.fn(),
vjstranscribe: jest.fn(),
};
const result = await fetchAndAddTranscripts(mockTranscriptUrls);

await fetchAndAddTranscripts(mockTranscriptUrls, player);
expect(fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(logError).toHaveBeenCalledWith(`Error fetching or processing transcript for en: ${error}`);

expect(global.fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(logError).toHaveBeenCalledWith(
'Error fetching or processing transcript for en:',
expect.any(Error),
);
expect(result).toEqual({});
});

it('should log an error if there is an error during Promise.all', async () => {
const mockTranscriptUrls = {
en: 'https://example.com/en-transcript.json',
};

global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve({ items: [] }),
});

const player = {
addRemoteTextTrack: jest.fn(),
vjstranscribe: jest.fn(),
const mockTranscriptData = {
items: ['example'],
};

fetch.mockResponseOnce(JSON.stringify(mockTranscriptData));
const error = new Error('File creation error');
createWebVttFile.mockImplementation(() => {
throw new Error('File creation error');
throw error;
});

await fetchAndAddTranscripts(mockTranscriptUrls, player);
const result = await fetchAndAddTranscripts(mockTranscriptUrls);

expect(global.fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(fetch).toHaveBeenCalledWith(mockTranscriptUrls.en);
expect(logError).toHaveBeenCalledWith(
'Error fetching or processing transcript for en:',
expect.any(Error),
`Error fetching or processing transcript for en: ${error}`,
);

expect(result).toEqual({});
});
});
4 changes: 2 additions & 2 deletions src/components/video/tests/VideoJS.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const YoutubeVideoOptions = {
sources: [{ src: ytUrl, type: 'video/youtube' }],
};

describe('Videon JS component', () => {
it('Renders VideoJS components correctly.', () => {
describe('VideoJS', () => {
it('Renders VideoJS components correctly for HLS videos.', () => {
const { container } = renderWithRouter(<VideoJS options={HLSVideoOptions} />);
expect(container.querySelector('.video-js-wrapper')).toBeTruthy();
expect(container.querySelector('.vjs-big-play-centered')).toBeTruthy();
Expand Down

0 comments on commit 42aa483

Please sign in to comment.