单元测试断言脚本加载事件触发时布尔值已更改

时间:2017-12-01 00:54:35

标签: javascript unit-testing onload

我有一个加载ckeditor.js的脚本,一旦加载它就会禁用自动内联..

var script = window.document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'ckeditor.js');
window.document.body.appendChild(script);
script.addEventListener('load', function() {
  CKEDITOR.disableAutoInline = true;
});

一旦脚本的assert事件处理程序被触发,我可以CKEDITOR.disableAutoInlinetrue设置为load吗?

it('should ensure disableAutoInline is set to true when the ckeditor.js file is loaded in the browser', function() {
  window.CKEDITOR = {
    disableAutoInline: false
  };
  var ckeditorjs = window.document.querySelectorAll('[src=\'/ckeditor.js\']');
  expect(ckeditorjs.length).to.equal(1);
});

1 个答案:

答案 0 :(得分:0)

需要在load ..

上实际触发script事件
var ckeditorjs = window.document.querySelectorAll('[src=\'/ckeditor.js\']');
window.CKEDITOR = {
  disableAutoInline: null
};
var event = new Event('load');
ckeditorjs[0].dispatchEvent(event);
expect(window.CKEDITOR.disableAutoInline).to.equal(true);