Skip to content

Commit 910c696

Browse files
committed
Testing and adding in store to player mapping
1 parent f45b403 commit 910c696

File tree

4 files changed

+140
-4
lines changed

4 files changed

+140
-4
lines changed

app/Player.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,27 @@
22

33
import React, { Component } from 'react';
44
import { Provider } from 'react-redux';
5+
56
import configureStore from './store/configurePlayerStore';
67
import PlaybackControls from './containers/PlaybackControls.jsx';
8+
import mapStoreToPlayer from './store/mapStoreToPlayer';
79

810
const store = configureStore();
911

1012
class Player extends Component {
13+
componentDidMount() {
14+
mapStoreToPlayer(store, this.refs.video);
15+
}
16+
1117
render() {
12-
return <Provider store={store}>
18+
return (<Provider store={store}>
1319
<div>
14-
<video>
20+
<video ref="video">
1521
<source src={this.props.src} />
1622
</video>
1723
<PlaybackControls />
1824
</div>
19-
</Provider>;
25+
</Provider>);
2026
}
2127
}
2228

app/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Player from './Player.jsx';
66

77
ReactDOM.render(
88
React.createElement(Player, {
9-
src: './SampleVideo_640x360_10mb.mp4'
9+
src: './SampleVideo_720x480_1mb.mp4'
1010
}),
1111
document.getElementById('app')
1212
);

app/store/mapStoreToPlayer.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
function setupPlayer(player) {
4+
return {
5+
play: (isPlaying) => {
6+
if (isPlaying) player.play();
7+
else player.pause();
8+
},
9+
stop: (hasStopped) => {
10+
if (hasStopped) {
11+
player.pause();
12+
player.currentTime = 0;
13+
}
14+
}
15+
};
16+
}
17+
18+
function isPlayingChanged(previousState, currentState) {
19+
return (previousState.playback.isPlaying !== currentState.playback.isPlaying);
20+
}
21+
22+
function hasStoppedChanged(previousState, currentState) {
23+
return (previousState.playback.hasStopped !== currentState.playback.hasStopped);
24+
}
25+
26+
export default (store, video) => {
27+
const player = setupPlayer(video);
28+
let currentState = store.getState();
29+
30+
return store.subscribe(() => {
31+
const previousState = currentState;
32+
currentState = store.getState();
33+
34+
if (isPlayingChanged(previousState, currentState)) player.play(currentState.playback.isPlaying);
35+
if (hasStoppedChanged(previousState, currentState)) player.stop(currentState.playback.hasStopped);
36+
});
37+
};

test/store/mapStoreToPlayer.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
'use strict';
2+
3+
import assert from 'assert';
4+
import sinon from 'sinon';
5+
6+
import configureStore from '../../app/store/configurePlayerStore';
7+
import mapStoreToPlayer from '../../app/store/mapStoreToPlayer';
8+
9+
const store = configureStore();
10+
const sandbox = sinon.sandbox.create();
11+
12+
function setupPlayer() {
13+
const playSpy = sinon.spy();
14+
const pauseSpy = sinon.spy();
15+
16+
return {
17+
play: playSpy,
18+
pause: pauseSpy,
19+
playSpy: playSpy,
20+
pauseSpy: pauseSpy
21+
};
22+
}
23+
24+
describe('mapStoreToPlayer', () => {
25+
beforeEach(() => {
26+
sandbox.stub(store, 'getState');
27+
});
28+
29+
afterEach(() => {
30+
sandbox.restore();
31+
});
32+
33+
it('triggers the pause method when the dispatching isPlaying: false', () => {
34+
const player = setupPlayer();
35+
store.getState.onCall(0).returns({
36+
playback: {
37+
isPlaying: true
38+
}
39+
});
40+
store.getState.onCall(1).returns({
41+
playback: {
42+
isPlaying: false
43+
}
44+
});
45+
const unsubscribe = mapStoreToPlayer(store, player);
46+
47+
store.dispatch({type: 'FAKE_DISPATCH'});
48+
assert(player.pauseSpy.calledOnce);
49+
50+
unsubscribe();
51+
});
52+
53+
it('triggers the play method when the dispatching isPlaying: true', () => {
54+
const player = setupPlayer();
55+
store.getState.onCall(0).returns({
56+
playback: {
57+
isPlaying: false
58+
}
59+
});
60+
store.getState.onCall(1).returns({
61+
playback: {
62+
isPlaying: true
63+
}
64+
});
65+
const unsubscribe = mapStoreToPlayer(store, player);
66+
67+
store.dispatch({type: 'FAKE_DISPATCH'});
68+
assert(player.playSpy.calledOnce);
69+
70+
unsubscribe();
71+
});
72+
73+
it('triggers the pause method and resets the player when dispatching hasStopped: true', () => {
74+
const player = setupPlayer();
75+
store.getState.onCall(0).returns({
76+
playback: {
77+
hasStopped: false
78+
}
79+
});
80+
store.getState.onCall(1).returns({
81+
playback: {
82+
hasStopped: true
83+
}
84+
});
85+
const unsubscribe = mapStoreToPlayer(store, player);
86+
87+
store.dispatch({type: 'FAKE_DISPATCH'});
88+
assert(player.pauseSpy.calledOnce);
89+
assert.equal(player.currentTime, 0);
90+
91+
unsubscribe();
92+
});
93+
});

0 commit comments

Comments
 (0)