Skip to content

Commit

Permalink
fix: actively playing video restarts due to query re-fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
Maham Akif authored and Maham Akif committed Sep 5, 2024
1 parent 272f6dd commit ea3c267
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/components/video/VideoJS.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ const VideoJS = ({ options, onReady, customOptions }) => {
videoRef.current.appendChild(videoElement);

playerRef.current = videojs(videoElement, playerOptions, handlePlayerReady);
} else {
} else if (playerOptions?.sources[0]?.src !== playerRef?.current?.currentSrc()) {
// Only update player if the source changes
playerRef.current.autoplay(playerOptions.autoplay);
playerRef.current.src(playerOptions.sources);

Expand Down
74 changes: 73 additions & 1 deletion src/components/video/tests/VideoJS.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { waitFor } from '@testing-library/react';
import { renderWithRouter } from '../../../utils/tests';
import VideoJS from '../VideoJS';
import { useTranscripts } from '../data';
import { useTranscripts, usePlayerOptions } from '../data';

// Mocking the 'videojs-vjstranscribe' and 'useTranscripts' hook
jest.mock('videojs-vjstranscribe');
Expand Down Expand Up @@ -185,4 +185,76 @@ describe('VideoJS', () => {
expect(container.querySelector('video-js')).toBeFalsy();
});
});

it('Updates player source when src changes', () => {
// Mock video.js
jest.mock('video.js', () => {
const actualVideoJs = jest.requireActual('video.js');
return {
...actualVideoJs,
videojs: jest.fn().mockImplementation(() => ({
playbackRates: jest.fn(),
src: jest.fn(),
dispose: jest.fn(),
autoplay: jest.fn(),
on: jest.fn(),
off: jest.fn(),
ready: jest.fn(),
isDisposed: jest.fn(),
currentSrc: jest.fn(),
})),
};
});
const mockPlayerInstance = {
autoplay: jest.fn(),
src: jest.fn(),
currentSrc: jest.fn(),
};

// Mock the implementation of videojs
// eslint-disable-next-line global-require
require('video.js').videojs.mockImplementation(() => mockPlayerInstance);

// Initial mock return value for currentSrc
mockPlayerInstance.currentSrc.mockReturnValue('https://initial-domain.com/initial.m3u8');

// Mock the initial playerOptions
const initialOptions = {
autoplay: true,
responsive: true,
fluid: true,
controls: true,
sources: [{ src: 'https://initial-domain.com/initial.m3u8', type: 'application/x-mpegURL' }],
};

// Mock the updated playerOptions
const updatedOptions = {
autoplay: true,
responsive: true,
fluid: true,
controls: true,
sources: [{ src: 'https://test-domain.com/test-prefix/id.m3u8', type: 'application/x-mpegURL' }],
};

// Set mock return value for usePlayerOptions
usePlayerOptions.mockReturnValueOnce(initialOptions).mockReturnValueOnce(updatedOptions);

// Simulate the logic that checks and updates the player source
const mockUpdatePlayerSource = (playerOptions) => {
if (playerOptions?.sources[0]?.src !== mockPlayerInstance.currentSrc()) {
// Only update player if the source changes
mockPlayerInstance.autoplay(playerOptions.autoplay);
mockPlayerInstance.src(playerOptions.sources);
}
};

// Simulate the initial state
mockUpdatePlayerSource(initialOptions);

// Simulate the source change
mockUpdatePlayerSource(updatedOptions);

// Verify src has been called with updated options
expect(mockPlayerInstance.src).toHaveBeenCalledWith([{ src: 'https://test-domain.com/test-prefix/id.m3u8', type: 'application/x-mpegURL' }]);
});
});

0 comments on commit ea3c267

Please sign in to comment.