Skip to content

Instantly share code, notes, and snippets.

@sagar-more
Created April 15, 2019 17:05
Show Gist options
  • Save sagar-more/60cf76b385d728bd21069740b4bc5ec6 to your computer and use it in GitHub Desktop.
Save sagar-more/60cf76b385d728bd21069740b4bc5ec6 to your computer and use it in GitHub Desktop.
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(`<body></body>`, { runScripts: "dangerously" });
function createDom(total) {
for (let i = 0; i < total; i++) {
var btn = dom.window.document.createElement('button');
btn.appendChild(dom.window.document.createTextNode('Button ' + i));
btn.addEventListener('click', function(){ console.log(i); });
dom.window.document.body.appendChild(btn);
}
}
function testDom(which) {
var evt = dom.window.document.createEvent("HTMLEvents");
evt.initEvent("click", false, false);
dom.window.document.body.children[which].dispatchEvent(evt);
}
var args = process.argv.slice(2);
createDom(args[0])
testDom(args[1])
// node script.js 5 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment