-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathSpotify.js
110 lines (96 loc) · 3.32 KB
/
Spotify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React from 'react'
import test from 'ava'
import sinon from 'sinon'
import { shallow } from 'enzyme'
import testPlayerMethods from '../helpers/testPlayerMethods'
import * as utils from '../../src/utils'
import Spotify from '../../src/players/Spotify'
global.window = {}
const TEST_URL = 'spotify:track:0KhB428j00T8lxKCpHweKw'
testPlayerMethods(Spotify, {
play: 'resume',
pause: 'pause',
stop: 'destroy',
seekTo: 'seek'
})
test('load() - Player not initialized and sdk not loaded', t => {
class MockPlayer {
constructor (container, options) {
t.true(container === 'mock-container')
setTimeout(options.events.onReady, 100)
}
}
const getSDK = sinon.stub(utils, 'getSDK').resolves({ MockPlayer })
const instance = shallow(
<Spotify url={TEST_URL} />
).instance()
instance.container = 'mock-container'
instance.load(TEST_URL)
t.truthy(global.window.onSpotifyIframeApiReady)
t.true(getSDK.calledOnce)
getSDK.restore()
})
test('load() - sdk already loaded', t => {
const getSDK = sinon.stub(utils, 'getSDK')
window.SpotifyIframeApi = true
const instance = shallow(
<Spotify url={TEST_URL} />
).instance()
const initializePlayer = sinon.stub(instance, 'initializePlayer')
instance.container = 'mock-container'
instance.load(TEST_URL)
t.false(getSDK.calledOnce)
t.true(initializePlayer.calledOnce)
getSDK.restore()
initializePlayer.restore()
})
test('load() - player already initialized', t => {
const getSDK = sinon.stub(utils, 'getSDK')
window.SpotifyIframeApi = true
const instance = shallow(
<Spotify url={TEST_URL} />
).instance()
instance.player = true
const initializePlayer = sinon.stub(instance, 'initializePlayer')
const callPlayer = sinon.stub(instance, 'callPlayer')
instance.container = 'mock-container'
instance.load(TEST_URL)
t.false(getSDK.calledOnce)
t.false(initializePlayer.calledOnce)
t.true(callPlayer.calledOnce)
getSDK.restore()
initializePlayer.restore()
callPlayer.restore()
})
test('onStateChange() - play', t => {
const called = {}
const onPlay = () => { called.onPlay = true }
const onBufferEnd = () => { called.onBufferEnd = true }
const instance = shallow(<Spotify url={TEST_URL} onPlay={onPlay} onBufferEnd={onBufferEnd} />).instance()
instance.onStateChange({ data: { isPaused: false, isBuffering: false } })
t.true(called.onPlay && called.onBufferEnd)
})
test('onStateChange() - pause', async t => {
const onPause = () => t.pass()
const instance = shallow(<Spotify url={TEST_URL} onPause={onPause} />).instance()
instance.onStateChange({ data: { isPaused: true } })
})
test('onStateChange() - buffer', async t => {
const onBuffer = () => t.pass()
const instance = shallow(<Spotify url={TEST_URL} onBuffer={onBuffer} />).instance()
instance.onStateChange({ data: { isBuffering: true } })
})
test('onStateChange() - ended', async t => {
const onEnded = () => t.pass()
const instance = shallow(<Spotify url={TEST_URL} onEnded={onEnded} onPlay={() => {}} onBufferEnd={() => {}} />).instance()
instance.onStateChange({ data: { duration: 100, position: 105, isPaused: false, isBuffering: false } })
})
test('render()', t => {
const wrapper = shallow(<Spotify url={TEST_URL} />)
const style = { width: '100%', height: '100%' }
t.true(wrapper.contains(
<div style={style}>
<div />
</div>
))
})