Skip to content

Commit 2e7d6fa

Browse files
committed
add mute prop and improve test
1 parent 046dd10 commit 2e7d6fa

File tree

8 files changed

+343
-302
lines changed

8 files changed

+343
-302
lines changed

karma.conf.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Karma configuration
22
// Generated on Wed Dec 16 2015 21:28:49 GMT+0900 (JST)
33

4-
module.exports = function(config) {
4+
module.exports = function (config) {
55
config.set({
66

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

1414
// list of files / patterns to load in the browser
1515
files: [
16-
'test/index.spec.js'
16+
'test/*.spec.js'
1717
],
1818

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

2929
// test results reporter to use
@@ -75,11 +75,7 @@ module.exports = function(config) {
7575
{
7676
test: /\.js$/,
7777
exclude: /node_modules/,
78-
loader: 'babel'
79-
},
80-
{
81-
test: /\.json$/,
82-
loader: 'json'
78+
loader: 'babel-loader'
8379
}
8480
]
8581
},

src/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict'
2-
31
import { getIdFromURL, getTimeFromURL } from './utils'
42
import container from './container'
53
import YouTubePlayer from './player'
64

7-
export { getIdFromURL, getTimeFromURL }
5+
export { YouTubePlayer, getIdFromURL, getTimeFromURL }
86

97
export function install (Vue) {
108
container.Vue = Vue

src/player.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,27 @@ import container from './container'
33
let pid = 0
44

55
export default {
6-
props: ['playerHeight', 'playerWidth', 'playerVars', 'videoId', 'mute'],
6+
props: {
7+
playerHeight: {
8+
type: String,
9+
default: '390'
10+
},
11+
playerWidth: {
12+
type: String,
13+
default: '640'
14+
},
15+
playerVars: {
16+
type: Object,
17+
default: () => ({ autoplay: 0, time: 0 })
18+
},
19+
videoId: {
20+
type: String
21+
},
22+
mute: {
23+
type: Boolean,
24+
default: false
25+
}
26+
},
727
render (h) {
828
return h('div', [
929
h('div', { attrs: { id: this.elementId }})
@@ -25,20 +45,17 @@ export default {
2545
},
2646
methods: {
2747
setSize () {
28-
this.player.setSize(this.playerWidth || '640', this.playerHeight || '390')
48+
this.player.setSize(this.playerWidth, this.playerHeight)
2949
},
30-
setMute () {
31-
if (this.mute && this.player.isMuted()) {
50+
setMute (value) {
51+
if (value) {
3252
this.player.mute()
3353
} else {
3454
this.player.unMute()
3555
}
3656
},
3757
update (videoId) {
38-
const {
39-
playerVars = { autoplay: 0 }
40-
} = this
41-
const name = `${playerVars.autoplay ? 'load' : 'cue'}VideoById`
58+
const name = `${this.playerVars.autoplay ? 'load' : 'cue'}VideoById`
4259
if (this.player.hasOwnProperty(name)) {
4360
this.player[name](videoId)
4461
} else {
@@ -50,16 +67,11 @@ export default {
5067
},
5168
mounted () {
5269
container.register((YouTube) => {
53-
const {
54-
playerHeight: height = '390',
55-
playerWidth: width = '640',
56-
playerVars = { autoplay: 0, start: 0 },
57-
videoId
58-
} = this
70+
const { playerHeight, playerWidth, playerVars, videoId } = this
5971

6072
this.player = new YouTube.Player(this.elementId, {
61-
height,
62-
width,
73+
height: playerHeight,
74+
width: playerWidth,
6375
playerVars,
6476
videoId,
6577
events: {

test/container.spec.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import assert from 'power-assert'
2+
import container from '../src/container'
3+
4+
describe('container', () => {
5+
context('when YouTube is not ready', () => {
6+
describe('#register', () => {
7+
it('should add a callback to scripts', () => {
8+
const length = container.scripts.length
9+
container.register(() => {})
10+
assert.equal(container.scripts.length, length + 1)
11+
})
12+
})
13+
14+
after(() => {
15+
container.scripts = []
16+
})
17+
})
18+
19+
context('when YouTube is ready', () => {
20+
before(() => {
21+
container.YT = {
22+
Player () {}
23+
}
24+
container.Vue = {
25+
nextTick (callback) {
26+
callback()
27+
}
28+
}
29+
})
30+
31+
describe('#run', () => {
32+
it('should call calbacks and pass YT.Player to its scripts', () => {
33+
const spy = sinon.spy()
34+
container.scripts = [spy]
35+
container.run()
36+
assert.ok(spy.calledWith(container.YT))
37+
})
38+
39+
it('should remove elements from scripts', () => {
40+
container.scripts.push(() => {})
41+
container.run()
42+
assert.equal(container.scripts.length, 0)
43+
})
44+
45+
afterEach(() => {
46+
container.scripts = []
47+
})
48+
})
49+
50+
describe('#register', () => {
51+
it('should call callback', () => {
52+
sinon.spy(container.Vue, 'nextTick')
53+
const callbackSpy = sinon.spy()
54+
container.register(callbackSpy)
55+
assert.ok(container.Vue.nextTick.called)
56+
assert.ok(callbackSpy.called)
57+
container.Vue.nextTick.restore()
58+
})
59+
})
60+
})
61+
62+
after(() => {
63+
delete container.YT
64+
delete container.Vue
65+
})
66+
})

0 commit comments

Comments
 (0)