No, that doesn’t get done.
☒ Fail ☐ Pass ☐ Refactor
From the code at https://jsfiddle.net/0zqx6ft3/2/ we are looking for a way to have a failing test, when manageCover.init() is run, to become a passing test.
That means looking for something that the init() code changes, that we can test for.
The easiest kind of test is when the function returns something. That doesn’t happen here.
The next easiest is when the function changes something, which is commonly called a side-effect. Does any of that happen in this init() code?
function init(callback) {
const cover = document.querySelector(".play");
cover.addEventListener("click", coverClickHandler);
events.afterClickCover = new Event("afterClickCover");
cover.addEventListener("afterClickCover", callback);
}
The first two lines of the function make no changes that we can directly test for from outside of the function.
The last two lines of the function involve the callback which we aren’t dealing with yet, so we can ignore those for now.
There are no direct changes that occur, so we must move on to indirect changes. That’s where we use the click event from the first two lines of code.
What does the coverClickHandler do? Here it is:
function coverClickHandler(evt) {
const cover = evt.currentTarget;
const curtain = openCurtain(cover);
showVideo(curtain);
cover.dispatchEvent(events.afterClickCover);
}
The openCurtain() function possibly does things that we can test, as well as the showVideo() function. That showVideo() function is what we call “going for the gold” and needs to be held off for as long as possible. If we most directly test that then we won’t be as interested in testing other things around it that need testing, so delayed gratification really is needed here.
In that openCurtain() function is there anything that we can test for?
function openCurtain(cover) {
hide(cover);
const curtain = document.querySelector(".curtain");
curtain.classList.add("slide");
return curtain;
}
There are two things that we can test for, the hiding of the cover and adding slide to the curtain. Those will be two separate tests.
With hiding the cover, we need to ensure that it is first showing before the test calls manageCover.init(), so that we can expect that it is not showing afterwards.
So in the test, before manageCover.init(), define a cover variable the same as what’s done on line 32, that being the first line of the init() function.
We then use classList to remove “hide” from the cover.
After that the manageCover.init() gets run, and after that we need to expect that the cover doesn’t doesn’t have the class “hide”, by using the toHaveClass() matcher.
And after doing that we should end up with a suitably failing test, from which we can then work on making that test pass.