Skip to content

Commit

Permalink
convert stream player to stateless, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
dmamills committed Dec 19, 2019
1 parent bce9feb commit cdd0e32
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 66 deletions.
13 changes: 7 additions & 6 deletions client/src/Chat/Chat.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import { render, fireEvent, wait } from '@testing-library/react';
import { render, act } from '@testing-library/react';
import Chat from './index';
import api from '../api';

jest.mock('socket.io-client');
jest.mock('../api', () => {
Expand All @@ -16,7 +15,7 @@ beforeEach(() => {
});

describe('Chat Component', () => {
xit('renders without crashing', () => {
it('renders without crashing', async () => {
const onSpy = jest.fn();
const offSpy = jest.fn();
const emitSpy = jest.fn();
Expand All @@ -28,10 +27,12 @@ describe('Chat Component', () => {
once: onceSpy,
};

//TODO: fix act error, caused by getHistory
wait(() => {
render(<Chat socket={socket} />);
await act(async () => {
await render(<Chat socket={socket} />);
});

expect(onSpy).toBeCalledTimes(2);
expect(emitSpy).toBeCalledTimes(1);
expect(onceSpy).toBeCalledTimes(1);
});
});
4 changes: 1 addition & 3 deletions client/src/StreamPlayer/StreamPlayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ describe('StreamPlayer', () => {


it('should create player on mount', () => {
const component = render(<StreamPlayer />);
render(<StreamPlayer />);
expect(MediaPlayer).toHaveBeenCalled();
expect(mockAttach).toHaveBeenCalled();
expect(mockLoad).toHaveBeenCalled();
expect(mockPlay).toHaveBeenCalled();
});
});
102 changes: 45 additions & 57 deletions client/src/StreamPlayer/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { useState, useEffect, useRef } from 'react';
import stylish from '@dmamills/stylish';
import Controls from './Controls';
import cn from 'classnames';
Expand All @@ -8,76 +8,64 @@ import MediaPlayer from './MediaPlayer';
const STREAM_URL = process.env.REACT_APP_STREAM_URL;
const playerContainer = stylish({ height: '400px' });

class StreamPlayer extends Component {
state = {
playing: false
}
componentDidMount() {
this.player = new MediaPlayer(STREAM_URL);
this.player.attach(this.videoElement);

this.videoElement.addEventListener('ended', () => {
this.setState({
playing: false
}, this.start);
});

this.start();
}
const StreamPlayer = () => {
const [player, setPlayer] = useState(null);
const [playing, setPlaying] = useState(false);
const videoEl = useRef(null);

start = () => {
const { playing } = this.state;
const start = () => {
if(playing) return;

this.player.unload();
this.player.load();
this.player.play();
this.setState({ playing: true });
player.unload();
player.load();
player.play();
setPlaying(true);
}

componentWillUnmount() {
this.player.destroy();
}
const onStop = () => {
if(!playing) return;

getVideoReference = el => {
this.videoElement = el;
player.pause();
setPlaying(false);
}

onPlay = () => {
this.start();
const onVolumeChange = volume => {
videoEl.current.volume = volume;
}

onStop = () => {
if(!this.state.playing) return;
const onFullScreen = () => videoEl.current.requestFullscreen()

this.player.pause();
this.setState({ playing: false });
}
useEffect(() => {
const mediaPlayer = new MediaPlayer(STREAM_URL);
mediaPlayer.attach(videoEl.current);
setPlayer(mediaPlayer);

onVolumeChange = volume => {
this.videoElement.volume = volume;
}
videoEl.current.addEventListener('ended', () => {
setPlaying(true);
start();
});

onFullScreen = () => this.videoElement.requestFullscreen()
return function() {
mediaPlayer.destroy();
}
}, [false]);

render() {
return (
<div className={cn(ml1, flex, column)}>
<video
ref={this.getVideoReference}
className={playerContainer}
>
</video>
<Controls
playing={this.state.playing}
onPlay={this.onPlay}
onStop={this.onStop}
onVolumeChange={this.onVolumeChange}
onFullScreen={this.onFullScreen}
/>
</div>
);
}
return (
<div className={cn(ml1, flex, column)}>
<video
ref={videoEl}
className={playerContainer}
>
</video>
<Controls
playing={playing}
onPlay={start}
onStop={onStop}
onVolumeChange={onVolumeChange}
onFullScreen={onFullScreen}
/>
</div>
);
}

export default StreamPlayer;

0 comments on commit cdd0e32

Please sign in to comment.