Skip to content

Instantly share code, notes, and snippets.

@piotrze
Last active September 19, 2015 08:45
Emulate video tag api, for testing in phantomjs
React.createClass(
componentDidMount: ->
video = @getVideoElement()
video.addEventListener "ended", @ended, false
video.addEventListener "durationchange", @durationChange, false
video.addEventListener "timeupdate", @timeUpdate, false
video.play()
componentWillUnmount: ->
video = @getVideoElement()
video.pause()
video.removeEventListener("ended", @ended)
video.removeEventListener("durationchange", @durationChange)
video.removeEventListener("timeupdate", @timeUpdate)
getVideoElement: ->
# here could be more sofisticated check
if window.RailsEnv == 'test'
video = new VideoTagEmulator()
else
video = @getDOMNode()
render: ->
`<video src={this.props.url} poster={this.props.poster} ></video>`
class window.VideoTagEmulator
speed: 0.1
constructor: ->
@handlers = {}
@duration = 5
@target = {
duration: 0,
currentTime: 0,
ended: false
}
addEventListener: (eventType, handler) ->
@handlers[eventType] = handler
removeEventListener: ->
@_stopLoop()
@handlers = {}
play: ->
@_startLoop()
pause: ->
@_stopLoop()
_playFrame: ->
@target.currentTime += 1
@handlers.timeupdate(target: @target)
if @target.currentTime == 1
@target.duration = @duration
@handlers.durationchange(target: @target)
else if @target.currentTime > @duration
@_stopLoop()
@target.ended = true
@handlers.ended(target: @target)
_startLoop: ->
@timerId = window.setInterval(
@_playFrame.bind(@),
1000 * @speed
)
_stopLoop: ->
window.clearInterval(@timerId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment