Skip to content

Commit 3d46214

Browse files
committed
adding in timeupdate listener
1 parent d0a0ecb commit 3d46214

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

app/store/mapStoreToPlayer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
import { hasStopped } from '../actions/hasStopped';
4+
import { setProgress } from '../actions/setProgress';
45

56
function setupPlayer(player) {
67
return {
@@ -22,6 +23,7 @@ function setupPlayer(player) {
2223

2324
function setupDispatch(store, player) {
2425
player.addEventListener('ended', () => store.dispatch(hasStopped(true)));
26+
player.addEventListener('timeupdate', () => store.dispatch(setProgress(player.currentTime)));
2527
}
2628

2729
function isPlayingChanged(previousState, currentState) {

test/store/mapStoreToPlayer.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
import assert from 'assert';
44
import sinon from 'sinon';
5+
import _ from 'lodash';
56

67
import configureStore from '../../app/store/configurePlayerStore';
78
import mapStoreToPlayer from '../../app/store/mapStoreToPlayer';
89

910
const store = configureStore();
1011
const sandbox = sinon.sandbox.create();
1112

12-
function setupPlayer() {
13+
function setupPlayer(params) {
1314
const playSpy = sinon.spy();
1415
const pauseSpy = sinon.spy();
15-
16-
return {
16+
const player = {
1717
play: playSpy,
1818
pause: pauseSpy,
1919
playSpy: playSpy,
@@ -22,6 +22,8 @@ function setupPlayer() {
2222
this[name] = cb;
2323
}
2424
};
25+
26+
return _.assign(player, params);
2527
}
2628

2729
describe('mapStoreToPlayer', () => {
@@ -117,14 +119,36 @@ describe('mapStoreToPlayer', () => {
117119
});
118120

119121
describe('player events', () => {
120-
beforeEach(() => sinon.stub(store, 'dispatch'));
122+
beforeEach(() => {
123+
sandbox.stub(store, 'dispatch');
124+
});
121125

122126
it('dispatches HAS_STOPPED: true when the player ends', () => {
123127
const player = setupPlayer();
124128
const unsubscribe = mapStoreToPlayer(store, player);
129+
const expectedCallArgs = {
130+
type: 'HAS_STOPPED',
131+
hasStopped: true
132+
};
125133

126134
player.ended();
127-
assert(store.dispatch.calledOnce);
135+
assert(store.dispatch.calledWith(expectedCallArgs));
136+
137+
unsubscribe();
138+
});
139+
140+
it('dispatched SET_PROGRESS: {NUMBER} when the timeupdate event is triggered', () => {
141+
const player = setupPlayer({
142+
currentTime: 75
143+
});
144+
const unsubscribe = mapStoreToPlayer(store, player);
145+
const expectedCallArgs = {
146+
type: 'SET_PROGRESS',
147+
progress: 75
148+
};
149+
150+
player.timeupdate();
151+
assert(store.dispatch.calledWith(expectedCallArgs));
128152

129153
unsubscribe();
130154
});

0 commit comments

Comments
 (0)