Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can `document.execCommand` be unit tested?

I'm trying to write unit tests for word-processor-like operations such as applying a list to a text node, but I found that document.execCommand is not available to jsdom, so I'm stumped as to how I could unit test the following operation:

document.getElementById('run').addEventListener('click', function() {
  document.execCommand("insertorderedlist");
});
<div contenteditable="true">Foo</div>
<button id="run">Select "Foo" then Click</button>
like image 929
rebelliard Avatar asked Oct 16 '22 19:10

rebelliard


1 Answers

I ended up having to mock the document.execCommand explicitly:

// test-mocks.js
global.document.execCommand = function execCommandMock() { };

And then feeding my mocks into mocha directly:

"specs": "mocha --require @babel/register --require jsdom-global/register --require ignore-styles --require test-mocks.js ./path/to/tests.spec.js"
like image 89
Alexandra Avatar answered Oct 21 '22 08:10

Alexandra