-
Notifications
You must be signed in to change notification settings - Fork 837
/
core_spec.js
228 lines (183 loc) · 7.25 KB
/
core_spec.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import Core from '../../src/components/core'
import Browser from '../../src/components/browser'
import Events from '../../src/base/events'
import { Fullscreen } from '../../src/base/utils'
import MediaControl from '../../src/plugins/media_control'
describe('Core', function() {
describe('When configure', function() {
beforeEach(function () {
this.core = new Core({})
this.core.load = sinon.spy()
})
it('should update option', function() {
const newOptions = {
autoPlay: true
}
this.core.configure(newOptions)
expect(this.core.options.autoPlay).to.equal(newOptions.autoPlay)
})
it('should update option and load source', function() {
const newOptions = {
source: 'some/path/to/media.mp4',
mute: true
}
this.core.configure(newOptions)
assert.ok(this.core.load.called)
expect(this.core.options.mute).to.equal(newOptions.mute)
})
it('shoud trigger options change event', function () {
let callback = sinon.spy()
this.core.on(Events.CORE_OPTIONS_CHANGE, callback)
const newOptions = {
autoPlay: false
}
this.core.configure(newOptions)
assert.ok(callback.called)
expect(this.core.options.autoPlay).to.equal(newOptions.autoPlay)
})
})
describe('#toggleFullscreen', () => {
beforeEach(() => {
this.core = new Core({})
this.core.plugins.push(new MediaControl(this.core))
})
describe('when is not in fullscreen', () => {
let fullScreenSpy
beforeEach(() => {
fullScreenSpy = sinon.spy(Fullscreen, 'requestFullscreen')
})
afterEach(() => {
fullScreenSpy.restore()
})
describe('and is not an iOS Browser', () => {
beforeEach(() => {
sinon.stub(Browser, 'isiOS').value(false)
})
it('calls Fullscreen.requestFullscreen with core element', () => {
this.core.toggleFullscreen()
expect(fullScreenSpy).to.have.been.calledWith(this.core.el)
})
it('adds a class "fullscreen" to core element', () => {
const spy = sinon.spy(this.core.$el, 'addClass')
sinon.stub(this.core, 'isFullscreen').returns(false)
expect(spy).not.to.have.been.called
this.core.toggleFullscreen()
expect(spy).to.have.been.calledWith('fullscreen')
})
})
describe('and is an iOS Browser', () => {
it('calls Fullscreen.requestFullscreen with currentContainer element', () => {
sinon.stub(Browser, 'isiOS').value(true)
const fakeCurrentContainer = document.createElement('div')
fakeCurrentContainer.setAttribute('id', 'fakeCurrentContainer')
this.core.activeContainer = { el: fakeCurrentContainer }
this.core.toggleFullscreen()
expect(fullScreenSpy).to.have.been.calledWith(this.core.activeContainer.el)
})
})
})
describe('when is in fullscreen', () => {
it('calls Fullscreen.cancelFullscreen', () => {
const spy = sinon.spy(Fullscreen, 'cancelFullscreen')
sinon.stub(Browser, 'isiOS').value(false)
sinon.stub(this.core, 'isFullscreen').returns(true)
this.core.toggleFullscreen()
expect(spy).to.have.been.called
})
describe('Browser.isiOS', () => {
it('removes "fullscreen nocursor" classes from core element', () => {
sinon.stub(Browser, 'isiOS').value(false)
sinon.stub(this.core, 'isFullscreen').returns(true)
const spy = sinon.spy(this.core.$el, 'removeClass')
expect(spy).not.to.have.been.called
this.core.toggleFullscreen()
expect(spy).to.have.been.calledWith('fullscreen nocursor')
})
})
})
describe('Multiple instances', () => {
it('shouldn\'t toggle one instance fullscreen state when another one stops', () => {
const newInstance = new Core({})
const fakeContainer1 = document.createElement('div')
fakeContainer1.setAttribute('id', 'fakeContainer1')
newInstance.el = fakeContainer1
expect(this.core.isFullscreen()).to.equal(false)
expect(newInstance.isFullscreen()).to.equal(false)
sinon.stub(Fullscreen, 'getFullscreenElement').returns(fakeContainer1)
expect(this.core.isFullscreen()).to.equal(false)
expect(newInstance.isFullscreen()).to.equal(true)
})
})
})
describe('#enableResizeObserver', () => {
beforeEach(() => {
this.clock = sinon.useFakeTimers()
this.core = new Core({})
sinon.spy(this.core, 'triggerResize')
})
afterEach(() => {
this.clock.restore()
})
it('calls #triggerResize every 500 milliseconds', () => {
this.core.enableResizeObserver()
expect(this.core.triggerResize).not.to.have.been.called
this.clock.tick(500)
expect(this.core.triggerResize).to.have.been.called
this.clock.tick(500)
expect(this.core.triggerResize).to.have.been.calledTwice
})
it('calls #triggerResize with core element width and height', () => {
this.core.enableResizeObserver()
this.clock.tick(500)
expect(this.core.triggerResize).to.have.been.calledWith({ height: 0, width: 0 })
})
})
describe('#triggerResize', () => {
it('sets the properties oldHeight and oldWidth with the new one', () => {
const newSize = { width: '50%', height: '50%' }
this.core = new Core({})
expect(this.core.oldHeight).equal(undefined)
expect(this.core.oldWidth).equal(undefined)
sinon.spy(this.core, 'trigger')
this.core.triggerResize(newSize)
expect(this.core.oldHeight).equal('50%')
expect(this.core.oldWidth).equal('50%')
})
it('sets the property computedSize with the new one', () => {
const newSize = { width: '50%', height: '50%' }
this.core = new Core({})
expect(this.core.computedSize).equal(undefined)
sinon.spy(this.core, 'trigger')
this.core.triggerResize(newSize)
expect(this.core.playerInfo.computedSize).equal(newSize)
})
it('triggers on an event Events.CORE_RESIZE', () => {
const newSize = { width: '50%', height: '50%' }
this.core = new Core({})
sinon.spy(this.core, 'trigger')
this.core.triggerResize(newSize)
expect(this.core.trigger).to.have.been.calledWith(Events.CORE_RESIZE, newSize)
})
})
describe('#handleWindowResize', () => {
beforeEach(() => {
this.core = new Core({})
this.currentScreenOrientation = window.innerWidth > window.innerHeight ? 'landscape' : 'portrait'
this.core._screenOrientation = this.currentScreenOrientation
sinon.spy(this.core, 'triggerResize')
})
describe('when change the screen orientation', () => {
it('calls #triggerResize with core element width and height', () => {
this.core._screenOrientation = this.currentScreenOrientation == 'landscape' ? 'portrait' : 'landscape'
this.core.handleWindowResize('event')
expect(this.core.triggerResize).to.have.been.calledWith({ height: 0, width: 0 })
})
})
describe('when screen orientation doesn\'t change', () => {
it('doesn\'t calls #triggerResize', () => {
this.core.handleWindowResize('event')
expect(this.core.triggerResize).not.to.have.been.called
})
})
})
})