-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
576 additions
and
943 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
libraries/common/collections/media-providers/MediaProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { logger } from '@shopgate/pwa-core/helpers'; | ||
import styles from './style'; | ||
|
||
/** | ||
* The MediaProvider base class. | ||
*/ | ||
class MediaProvider { | ||
/** | ||
* Constructor. | ||
*/ | ||
constructor() { | ||
this.containers = new Map(); | ||
} | ||
|
||
/** | ||
* Optimizes video container to make it responsive. | ||
* @param {NodeList} container A DOM container. | ||
* @private | ||
* @returns {MediaProvider} | ||
*/ | ||
responsify(container) { | ||
// Remove fixed dimensions from the container. | ||
container.setAttribute('height', ''); | ||
container.setAttribute('width', ''); | ||
|
||
// Create the wrapper and apply styling. | ||
const wrapper = document.createElement('div'); | ||
|
||
wrapper.className = styles; | ||
|
||
// Add the wrapper right before the container into the DOM. | ||
container.parentNode.insertBefore(wrapper, container); | ||
// Move the container into the wrapper. | ||
wrapper.appendChild(container); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Add a DOM container with embedded videos. | ||
* @param {NodeList} container A DOM container. | ||
* @returns {MediaProvider} | ||
*/ | ||
add() { | ||
logger.error('MediaProvider.add() needs to be implemented within an inheriting class'); | ||
return this; | ||
} | ||
|
||
/** | ||
* Remove a DOM container. | ||
* @param {NodeList} container A DOM container. | ||
* @returns {MediaProvider} | ||
*/ | ||
remove(container) { | ||
this.containers.delete(container); | ||
return this; | ||
} | ||
|
||
/** | ||
* Stops all playing videos within the DOM containers. | ||
* @returns {MediaProvider} | ||
*/ | ||
stop() { | ||
logger.error('MediaProvider.stop() needs to be implemented within an inheriting class'); | ||
return this; | ||
} | ||
} | ||
|
||
export default MediaProvider; |
76 changes: 76 additions & 0 deletions
76
libraries/common/collections/media-providers/MediaProvider.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { JSDOM } from 'jsdom'; | ||
import { logger } from '@shopgate/pwa-core/helpers'; | ||
import MediaProvider from './MediaProvider'; | ||
|
||
jest.mock('@shopgate/pwa-core/helpers', () => ({ | ||
logger: { | ||
error: jest.fn(), | ||
}, | ||
})); | ||
|
||
/** | ||
* Creates a DOM container with iframes. | ||
* @param {Array} srcs A list of video URLs. | ||
* @return {Object} | ||
*/ | ||
const createContainer = (srcs) => { | ||
const html = srcs.map(src => `<iframe src="${src}"></iframe>`).join(''); | ||
return new JSDOM(html).window.document; | ||
}; | ||
|
||
describe('MediaProvider', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('.constructor()', () => { | ||
it('should construct as expected', () => { | ||
const instance = new MediaProvider(); | ||
expect(instance.containers).toBeInstanceOf(Map); | ||
expect(Array.from(instance.containers)).toHaveLength(0); | ||
}); | ||
}); | ||
|
||
describe('.add()', () => { | ||
it('should log an error when it is not overwritten', () => { | ||
const instance = new MediaProvider(); | ||
instance.add(); | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('.stop()', () => { | ||
it('should log an error when it is not overwritten', () => { | ||
const instance = new MediaProvider(); | ||
instance.stop(); | ||
expect(logger.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('.remove()', () => { | ||
it('should remove containers as expected', () => { | ||
const instance = new MediaProvider(); | ||
const containerOne = createContainer(['http://www.provider-one.com/video']); | ||
const containerTwo = createContainer(['http://www.provider-two.com/video']); | ||
|
||
instance.containers.set(containerOne, containerOne); | ||
instance.containers.set(containerTwo, containerTwo); | ||
|
||
instance.remove(containerTwo); | ||
|
||
expect(instance.containers.size).toBe(1); | ||
expect(instance.containers.get(containerTwo)).toBeUndefined(); | ||
expect(instance.containers.get(containerOne)).toEqual(containerOne); | ||
|
||
instance.remove(containerOne); | ||
|
||
expect(instance.containers.size).toBe(0); | ||
}); | ||
}); | ||
|
||
describe.skip('.responsify()', () => { | ||
it('should optimize a container to be responsive', () => { | ||
// TODO: Implement the test when a solution for the insertBefore issue was found. | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as MediaProvider } from './MediaProvider'; | ||
export { default as Vimeo } from './Vimeo'; | ||
export { default as YouTube } from './YouTube'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { css } from 'glamor'; | ||
|
||
export default css({ | ||
position: 'relative', | ||
height: 0, | ||
overflow: 'hidden', | ||
padding: '0 0 56.25% 0', | ||
' iframe, object, embed': { | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
height: '100%', | ||
border: 0, | ||
}, | ||
}).toString(); |
Oops, something went wrong.