【问题标题】:Is there way to write a unit test for javascript function that calls an html element an does something with it?有没有办法为调用 html 元素并对其执行某些操作的 javascript 函数编写单元测试?
【发布时间】:2021-12-07 00:49:45
【问题描述】:

我有这样的功能:

function modaldisplayer(){
   $('.mymodal').modal('show');
   return 'success';
}

另外,我有一个函数 foo,其中包含 modaldisplayer();在里面。我使用 mocha、jest 和 chai 作为框架。如何对函数 foo 进行单元测试? Mocha 没有将 .modal('show') 视为一个函数。

【问题讨论】:

    标签: javascript html unit-testing jestjs mocha.js


    【解决方案1】:

    你应该看看 $('.mymodal').modal('show'); 在 DOM 树中做了什么改变。很可能它需要一些元素并切换一些 CSS 类。

    然后你的测试代码可以像这样检查这些元素和类

    expect($('some modal element').hasClass('e.g modal-visible')).toBeTrue();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多