wrolpi/app/src/components/VideoPlayerChannel.test.js

61 lines
2.2 KiB
JavaScript
Raw Normal View History

import React from 'react';
import {render, screen, waitFor} from '../test-utils';
import VideoPage from './VideoPlayer';
import {getChannel} from '../api';
// The video response carries the Video's Channel (id and name); fetching the whole Channel only to
// show its name costs a statistics query that takes seconds on a large, cold Channel.
jest.mock('../api', () => {
const actual = jest.requireActual('../api');
return {
...actual,
getChannel: jest.fn(),
getVideoComments: jest.fn().mockResolvedValue([]),
getVideoDescription: jest.fn().mockResolvedValue(''),
getVideoCaptions: jest.fn().mockResolvedValue([]),
};
});
const videoFile = (channel) => ({
id: 3,
name: 'video.mp4',
title: 'A Video',
primary_path: 'channel/video.mp4',
files: [{path: 'channel/video.mp4', mimetype: 'video/mp4'}],
tags: [],
video: {
id: 3,
channel,
channel_id: channel ? channel.id : null,
video_path: 'channel/video.mp4',
caption_files: [],
codec_names: [],
codec_types: [],
},
});
describe('VideoPage channel', () => {
beforeEach(() => getChannel.mockReset());
test("shows the Video's Channel without fetching the Channel", async () => {
render(<VideoPage videoFile={videoFile({id: 5, name: 'Some Channel'})} fetchVideo={() => null}/>);
const link = await screen.findByRole('link', {name: 'Some Channel'});
expect(link).toHaveAttribute('href', '/videos/channel/5/video');
// Let any effects that would fetch settle.
await waitFor(() => expect(screen.getByText('A Video')).toBeInTheDocument());
expect(getChannel).not.toHaveBeenCalled();
});
test('a Video without a Channel shows no Channel link', async () => {
render(<VideoPage videoFile={videoFile(null)} fetchVideo={() => null}/>);
await waitFor(() => expect(screen.getByText('A Video')).toBeInTheDocument());
const channelLinks = screen.queryAllByRole('link')
.filter(i => (i.getAttribute('href') || '').startsWith('/videos/channel/'));
expect(channelLinks).toHaveLength(0);
expect(getChannel).not.toHaveBeenCalled();
});
});