Let’s start indeed. After adding external resources and the test runner, the basic test shows that there’s a conflict between your CSS styles and the reporter. https://jsfiddle.net/pmw57/ffvkbLjw/304/
The title
class is used by both of them, so updating your title class so that it is only applied inside of the .wrap class, and the testing is ready to begin. https://jsfiddle.net/pmw57/ffvkbLjw/305/
What test can we use that will accurately tell us that the problem with the links image is fixed? Looking at the console view of the code, I see that class names seem to be correctly added to it, so checking class names will not in this case be of an use.
The problem is that the links image is being displayed when it shouldn’t be displayed, so we can check its computed style, to figure out if it is still showing or not.
it("hides the title image when it's clicked", function () {
var title = document.querySelector(".title");
title.click();
expect(window.getComputedStyle(title).display).toBe("none");
});
That gives us a test that accurately shows us what’s failing. https://jsfiddle.net/pmw57/ffvkbLjw/326/
But, when the test is run it changes how the button looks. That’s not good, so we can use an afterEach() function to return things back to how they should be, after each test.
afterEach(function () {
var title = document.querySelector(".title");
title.setAttribute("class", "title");
});
That’s partly effective, but we really need to return the wrap
element back to how it started too.
afterEach(function () {
var wrap = document.querySelector(".wrap");
var title = document.querySelector(".title");
wrap.setAttribute("class", "wrap inactive");
title.setAttribute("class", "title");
});
And the button returns back to looking how it should be, after the test.
We do have some duplication now of the title variable, which is going to get worse as testing occurs.
The best-practice technique of dealing with that is to first declare the variable, then inside of a beforeEach() function assign the variable.
var wrap;
var title;
beforeEach(function () {
wrap = document.querySelector(".wrap");
title = wrap.querySelector(".title");
});
We can now remove those var
statements from throughout the rest of the code too.
afterEach(function () {
// var wrap = document.querySelector(".wrap");
// var title = document.querySelector(".title");
...
});
it("hides the title image when it's clicked", function () {
// var title = document.querySelector(".title");
...
});
There’s one final piece of cleaning up to do. The test triggered the initialOverlay click handler, which then removed itself from the title image. We can deal with that by having a reset() function in the code, and running that after each test. By running that reset() code, we can then move the setAttribute code out of the test, and in to that reset() function instead where it more appropriately belongs.
To make it easier for us to access the button code, I’ve added the button code to a generic app.
var app = {};
app.buttone = (function iife() {
We can then return from the function, anything that we want to make accessible.
function reset(container) {
var title = container.querySelector(".title");
container.setAttribute("class", "wrap inactive");
title.setAttribute("class", "title");
container.removeEventListener("click", playButtonClickHandler);
container.addEventListener("click", initialOverlayClickHandler);
}
return {
reset
};
}());
Normally all of these changes don’t need to be all made at the same time, but because we are dealing with untested code, it hasn’t been written so that it’s easy to test. Starting with tests when you write code results in code that is much easier to test and maintain too.
For now though, the (currently failing) test is in place, and we are in a good situation to work on making the test pass.
https://jsfiddle.net/pmw57/ffvkbLjw/347/