[英]Event testing with Mocha and Vanilla Javascript
I am new to unit tests and want to create a test that will track the inner html after a click event which will indicate the switching between two users. 我是单元测试的新手,并希望创建一个在单击事件之后跟踪内部html的测试,该事件将指示两个用户之间的切换。 I have done several searches and have not been able to figure how to properly attach the unit test to the event. 我已经进行了几次搜索,还无法弄清楚如何将单元测试正确地附加到事件上。
Most of the resources that I have found that are similar seem to be related to React and not Vanilla Javascript. 我发现的大部分相似资源似乎都与React相关,而不与Vanilla Javascript有关。 This is my front-end code: 这是我的前端代码:
const xPlayer = 'X';
const oPlayer = 'O';
const boardSquares = document.querySelectorAll('.square');
let whosTurn = 0;
let board;
gameSetup();
function gameSetup() {
board = Array.from(Array(9).keys());
for (var i = 0; i < boardSquares.length; i++) {
boardSquares[i].innerText = '';
boardSquares[i].addEventListener('click', turnClick, false);
}
}
function turnClick(currentDiv) {
if(document.getElementById(currentDiv.target.id).innerHTML === '') {
writeInSquare(currentDiv);
}
}
function writeInSquare(currentDiv) {
if (whosTurn % 2 === 0) {
document.getElementById(currentDiv.target.id).innerHTML = xPlayer;
whosTurn++;
}
else {
document.getElementById(currentDiv.target.id).innerHTML = oPlayer;
whosTurn++;
}
}
export default{
boardSquares, whosTurn, gameSetup, turnClick, writeInSquare, checkIfGameComplete
};
This is my test file: 这是我的测试文件:
import sum from './index.js';
describe('sum', function() {
it('should change from xPlayer to oPlayer based on if whoseTurn is even or odd', function ({
}))
})
Html: HTML:
<div class="wrapper">
<div class="square" id="0">
</div>
<div class="square" id="1">
</div>
<div class="square" id="2">
</div>
<div class="square" id="3">
</div>
<div class="square" id="4">
</div>
<div class="square" id="5">
</div>
<div id="mocha"></div>
So basically there would be a switch between xplayer and yplayer that I would want to test to see if is happening from the click event 因此,基本上,我想测试一下xplayer和yplayer之间的切换,看看是否发生在click事件中
There is a way you could simulate an event on a DOM element manually. 有一种方法可以手动模拟DOM元素上的事件。 That's mentioned here . 在这里提到。 It involves creating a new Event
( MouseEvent
in your case) and then calling dispatchEvent
with it, on the DOM element you wish to simulate the click. 它涉及到创建一个新Event
(在您的情况下为MouseEvent
),然后在您要模拟点击的DOM元素上调用dispatchEvent
。 It's pretty tedious and has issues cross-browsers. 这非常繁琐,并且跨浏览器存在问题。
There is a popular library called simulant
, that makes this really easy. 有一个流行的库,叫做simulant
,这使得这真的很容易。 A click event would be as simple as 点击事件就像
simulant.fire( target, 'click' );
where target
is the DOM element you want to simulate the click. 其中target
是您要模拟点击的DOM元素。
A simple demo where I simulate a click on a button. 我模拟一个按钮单击的简单演示。
const target = document.getElementById("target"); const simulate = document.getElementById("simulate"); target.addEventListener("click", () => alert("Target clicked!")) simulate.addEventListener("click", () => simulant.fire(target, "click"))
<script src="https://unpkg.com/simulant/dist/simulant.umd.js"></script> <button id="target">Click me</button> <button id="simulate">Simulate</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.