Test code: https://jsfiddle.net/xmjhgenz/
Working video code: https://jsfiddle.net/2w14o79v/
Are both good now.
function isIframe(item) {
return item.nodeName === "IFRAME"
}
function getIframe(player) {
return Object.values(player).find(isIframe);
}
Are we able to get back to continuing the tests?
Where I am up to this:
In the videoPlayer tests there is an init section, and at the end of that init section we need a new test for what we expect to happen when we init with a list of videos for the playlist.
https://jsfiddle.net/jo6td04m/
If the test is supposed to fail first, it’s not failing.
it("init with a list of videos for the playlist", function() {
//given
player = undefined;
//when
videoPlayer.addPlayer(video);
//then
expect(typeof options.events.onReady).toBe("function");
});
});
This was my attempt at doing it in the working code: https://jsfiddle.net/gdrhpb3n/
But it has to be first done in the test code, then we can see if I did it wrong or right in my attempt I did.
Maybe some of what I did here can be used in the test code.
const videoPlayer = (function makeVideoPlayer() {
const config = {};
const events = {};
const eventHandlers = {};
let player = null;
function loadIframeScript() {
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
function onYouTubeIframeAPIReady() {
const cover = document.querySelector(".play");
const wrapper = cover.parentElement;
const frameContainer = wrapper.querySelector(".video");
videoPlayer.addPlayer(frameContainer, config.playlist);
}
function isIframe(item) {
return item.nodeName === "IFRAME"
}
function getIframe(player) {
return Object.values(player).find(isIframe);
}
function shufflePlaylist(player) {
player.setShuffle(true);
player.playVideoAt(0);
player.stopVideo();
}
function onPlayerReady(event) {
player = event.target;
player.setVolume(100);
shufflePlaylist(player);
const iframe = getIframe(player);
iframe.dispatchEvent(events.afterPlayerReady);
}
function addPlayer(video, playlist) {
const options = {
height: 360,
host: "https://www.youtube-nocookie.com",
width: 640
};
options.playerVars = {
autoplay: 0,
cc_load_policy: 0,
controls: 1,
disablekb: 1,
fs: 0,
iv_load_policy: 3,
loop: 1,
playlist,
rel: 0
};
options.events = {
"onReady": onPlayerReady
};
player = new YT.Player(video, options);
const iframe = getIframe(player);
const eventHandler = eventHandlers.afterPlayerReady;
iframe.addEventListener("afterPlayerReady", eventHandler);
}
function play() {
player.playVideo();
}
function addEvents() {
eventHandlers.afterPlayerReady = videoPlayer.afterPlayerReady;
events.afterPlayerReady = new Event("afterPlayerReady");
}
function init(videos) {
config.playlist = videos.join();
addEvents();
loadIframeScript();
window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
}
return {
addPlayer,
init,
play
};
}());
function initCover() {
manageCover.init(function playVideo() {
videoPlayer.play();
});
}
videoPlayer.afterPlayerReady = initCover;
videoPlayer.init([
"0dgNc5S8cLI",
"mnfmQe8Mv1g",
"CHahce95B1g",
"2VwsvrPFr9w"
]);