Testing DOM manipulating in Jasmine test(在 Jasmine 测试中测试 DOM 操作)
问题描述
我正在创建一个 js 小部件,第一部分是添加脚本宽度 javascript,如下所示(来自谷歌分析的示例):
I'm creating a js widget and first part is to add script width javascript, something like this (example from google analytics):
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
如何用 jasmine 测试它(使用夹具?)?
How to test it with jasmine (using fixtures?)?
推荐答案
为了在我的规范中设置 HTML 固定装置,我写了 茉莉花装置.有了它,你可以做这样的事情:
For setting up HTML fixtures in my specs, I wrote jasmine-fixture. With it, you can do stuff like this:
var $foo, $input;
beforeEach(function(){
$foo = affix('.foo');
# appends to the DOM <div class="foo"></div>
$input = $foo.affix('input[id="name"][value="Jim"]');
# appends <input id="name" value="Jim"/> beneath the .foo div
而且 afterEach,它会在你之后清理.
And afterEach, it'll clean up after you.
对于 DOM 状态的预期,我使用 jasmine-jquery.它提供了大量的匹配器,如下所示:
For expectations about the state of the DOM, I use jasmine-jquery. It offers a ton of matchers like the one below:
it('is named Jim', function(){
expect($input).toHaveValue("Jim");
});
这篇关于在 Jasmine 测试中测试 DOM 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Jasmine 测试中测试 DOM 操作
基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
