stenciljs 学习八 组件测试
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了stenciljs 学习八 组件测试,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1620字,纯文字阅读大概需要3分钟。
内容图文
![stenciljs 学习八 组件测试](/upload/InfoBanner/zyjiaocheng/1197/cbda27832ff24a339e6ba0580286de3c.jpg)
测试对于框架来说比较重要,对于web 组件的测试同样很重要,类似的jest 很方便,stenciljs也是基于jest 开发的
包含两个核心api render(), flush()
测试配置
package.json 配置
"devDependencies": {
...
"@types/jest": "^21.1.1",
"jest": "^21.2.1"
},
npm script 配置
"scripts": {
...
"test": "jest --no-cache",
"test.watch": "jest --watch --no-cache"
},
组件渲染测试
主要函数
- render({components:[],html:string} ) 进行组件列表的渲染,html 是html 的代码片段,包含组件的使用
- flush(element) 用来在属性变更之后刷新元素
渲染组件
- components 组件列表
- html html 片段
参考
beforeEach(async () => {
element = await render({
components: [MyName],
html: ‘<my-name></my-name>‘
});
});
刷新组件
flush 函数
it(‘should work with both the first and the last name‘, async () => {
element.first = ‘Peter‘
element.last = ‘Parker‘;
await flush(element);
expect(element.textContent).toEqual(‘Hello, my name is Peter Parker‘);
});
组件测试
参考
it(‘should least each part of the name breaking on spaces‘, async () => {
element.first = ‘Pasta Primavera‘;
element.last = ‘O Shea Buttersworth‘;
await flush(element);
const list = element.querySelector(‘ul‘);
expect(list.children.length).toEqual(5);
expect(list.children[0].textContent).toEqual(‘Pasta‘);
expect(list.children[1].textContent).toEqual(‘Primavera‘);
expect(list.children[2].textContent).toEqual(‘O‘);
expect(list.children[3].textContent).toEqual(‘Shea‘);
expect(list.children[4].textContent).toEqual(‘Buttersworth‘);
});
组件方法测试
it(‘should return an empty string if there is no first or last name‘, () => {
const myName = new MyName();
expect(myName.formatted()).toEqual(‘‘);
});
参考资料
https://stenciljs.com/docs/unit-testing
原文:https://www.cnblogs.com/rongfengliang/p/9711224.html
内容总结
以上是互联网集市为您收集整理的stenciljs 学习八 组件测试全部内容,希望文章能够帮你解决stenciljs 学习八 组件测试所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。