Having this HTML ->
<!DOCTYPE html>
<html lang="en">
<head>...</head>
<body>
<section class="players hidden"></section>
<button class="btn">Play</button>
</body>
</html>
Other function calls this one, the event is assigned and works well ->
const players = document.querySelector('.players');
const playBtn = document.querySelector('.btn');
const eventListener = () => {
playBtn.addEventListener('click', () => {
if (players.classList.contains('hidden')) {
players.classList.remove('hidden');
}
});
};
export default eventListener;
Now, this test always fails because it only uses the HTML I provided there ->
test('click event starts the game', () => {
document.body.innerHTML = `
<section class="players hidden"></section>
<button class="btn">Play</button>
`;
const players = document.querySelector('.players');
const playBtn = document.querySelector('.btn');
playBtn.click();
const myClass = players.classList.contains('hidden');
expect(myClass).toBe(false);
});
myClass
will always be true
because it's using that HTML I provided there.
My question is, how can I test the functionality of eventListener.js
? I want to simulate that button click, remove class hidden and test if it was indeed removed as intended?
Somehow I have to call the eventListener.js
but even if I do it will still test that interal HTML in the test, correct? If I go to index.html
and remove all classes will not affect the test which makes no sense, The idea of this test is to know if in the future I change or remove classes the test would fail letting me know where is the error.
Is this still considered unit testing? Or would be E2E?