Skip to content

Commit

Permalink
add mute prop and improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
kaorun343 committed Mar 18, 2017
1 parent 046dd10 commit 2e7d6fa
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 302 deletions.
12 changes: 4 additions & 8 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Karma configuration
// Generated on Wed Dec 16 2015 21:28:49 GMT+0900 (JST)

module.exports = function(config) {
module.exports = function (config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
Expand All @@ -13,7 +13,7 @@ module.exports = function(config) {

// list of files / patterns to load in the browser
files: [
'test/index.spec.js'
'test/*.spec.js'
],

// list of files to exclude
Expand All @@ -23,7 +23,7 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test/index.spec.js': 'webpack'
'test/*.spec.js': 'webpack'
},

// test results reporter to use
Expand Down Expand Up @@ -75,11 +75,7 @@ module.exports = function(config) {
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel'
},
{
test: /\.json$/,
loader: 'json'
loader: 'babel-loader'
}
]
},
Expand Down
4 changes: 1 addition & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'

import { getIdFromURL, getTimeFromURL } from './utils'
import container from './container'
import YouTubePlayer from './player'

export { getIdFromURL, getTimeFromURL }
export { YouTubePlayer, getIdFromURL, getTimeFromURL }

export function install (Vue) {
container.Vue = Vue
Expand Down
44 changes: 28 additions & 16 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@ import container from './container'
let pid = 0

export default {
props: ['playerHeight', 'playerWidth', 'playerVars', 'videoId', 'mute'],
props: {
playerHeight: {
type: String,
default: '390'
},
playerWidth: {
type: String,
default: '640'
},
playerVars: {
type: Object,
default: () => ({ autoplay: 0, time: 0 })
},
videoId: {
type: String
},
mute: {
type: Boolean,
default: false
}
},
render (h) {
return h('div', [
h('div', { attrs: { id: this.elementId }})
Expand All @@ -25,20 +45,17 @@ export default {
},
methods: {
setSize () {
this.player.setSize(this.playerWidth || '640', this.playerHeight || '390')
this.player.setSize(this.playerWidth, this.playerHeight)
},
setMute () {
if (this.mute && this.player.isMuted()) {
setMute (value) {
if (value) {
this.player.mute()
} else {
this.player.unMute()
}
},
update (videoId) {
const {
playerVars = { autoplay: 0 }
} = this
const name = `${playerVars.autoplay ? 'load' : 'cue'}VideoById`
const name = `${this.playerVars.autoplay ? 'load' : 'cue'}VideoById`
if (this.player.hasOwnProperty(name)) {
this.player[name](videoId)
} else {
Expand All @@ -50,16 +67,11 @@ export default {
},
mounted () {
container.register((YouTube) => {
const {
playerHeight: height = '390',
playerWidth: width = '640',
playerVars = { autoplay: 0, start: 0 },
videoId
} = this
const { playerHeight, playerWidth, playerVars, videoId } = this

this.player = new YouTube.Player(this.elementId, {
height,
width,
height: playerHeight,
width: playerWidth,
playerVars,
videoId,
events: {
Expand Down
66 changes: 66 additions & 0 deletions test/container.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import assert from 'power-assert'
import container from '../src/container'

describe('container', () => {
context('when YouTube is not ready', () => {
describe('#register', () => {
it('should add a callback to scripts', () => {
const length = container.scripts.length
container.register(() => {})
assert.equal(container.scripts.length, length + 1)
})
})

after(() => {
container.scripts = []
})
})

context('when YouTube is ready', () => {
before(() => {
container.YT = {
Player () {}
}
container.Vue = {
nextTick (callback) {
callback()
}
}
})

describe('#run', () => {
it('should call calbacks and pass YT.Player to its scripts', () => {
const spy = sinon.spy()
container.scripts = [spy]
container.run()
assert.ok(spy.calledWith(container.YT))
})

it('should remove elements from scripts', () => {
container.scripts.push(() => {})
container.run()
assert.equal(container.scripts.length, 0)
})

afterEach(() => {
container.scripts = []
})
})

describe('#register', () => {
it('should call callback', () => {
sinon.spy(container.Vue, 'nextTick')
const callbackSpy = sinon.spy()
container.register(callbackSpy)
assert.ok(container.Vue.nextTick.called)
assert.ok(callbackSpy.called)
container.Vue.nextTick.restore()
})
})
})

after(() => {
delete container.YT
delete container.Vue
})
})
Loading

0 comments on commit 2e7d6fa

Please sign in to comment.