用React.addons.TestUtils、Jasmine进行单元测试
2015-12-24 21:50
537 查看
一、用到的工具
1.React.addons.TestUtils
2.Jasmine
3.Browserify(处理jsx文件的require依赖关系)
4.Reactify(能和browserify很好的配合,把jsx转换为js)
5.编译命令为 browserify -t reactify test.jsx > app.js (参数t为transform)
二、测试代码:
1.test.jsx
2.CheckboxWithLabel.jsx
3.index.html
三、运行结果
1.
![](http://images2015.cnblogs.com/blog/653056/201512/653056-20151224214931734-1960448942.png)
2.若修改测试代码为expect(text).toContain("你是否爱吃橘子1"),则如下:
1.React.addons.TestUtils
2.Jasmine
3.Browserify(处理jsx文件的require依赖关系)
4.Reactify(能和browserify很好的配合,把jsx转换为js)
5.编译命令为 browserify -t reactify test.jsx > app.js (参数t为transform)
二、测试代码:
1.test.jsx
var React = require("react/addons"); var TestUtils = React.addons.TestUtils; var CheckboxWithLabel = require("./CheckboxWithLabel.jsx"); describe("test CheckboxWithLabel component", function () { it("check label text", function () { var checkbox = TestUtils.renderIntoDocument(<CheckboxWithLabel text="你是否爱吃橘子" on="爱吃" off="不爱吃"></CheckboxWithLabel>); var text = React.findDOMNode(checkbox).textContent; expect(text).toContain("你是否爱吃橘子1"); }) })
2.CheckboxWithLabel.jsx
var React = require('react/addons'); var CheckboxWithLabel = React.createClass({ getInitialState: function () { return { checked: false } }, onChange: function() { this.setState({ checked: !!this.state.checked }); }, render: function() { return ( <label> {this.props.text} <input type = "checkbox" checked={this.state.checked} onChange={this.onChange}/> {this.checked ? this.props.on : this.props.off} </label>); } }); module.exports = CheckboxWithLabel;
3.index.html
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <title>测试</title> </head> <body> <link rel="stylesheet" href="./node_modules/jasmine.css"> <script src="./node_modules/jasmine.js"></script> <script src="./node_modules/jasmine-html.js"></script> <script src="./node_modules/boot.js"></script> <script src="./app.js"></script> </body> </html>
三、运行结果
1.
![](http://images2015.cnblogs.com/blog/653056/201512/653056-20151224214931734-1960448942.png)
2.若修改测试代码为expect(text).toContain("你是否爱吃橘子1"),则如下:
![](http://images2015.cnblogs.com/blog/653056/201512/653056-20151224214758452-1738025922.png)
相关文章推荐
- 基于React实现项目中状态型变量的数据驱动可视化
- react 写的省市三级联动
- ReactiveCocoa
- 手把手教你在Windows下搭建React Native Android开发环境
- 手把手教你在Windows下搭建React Native Android开发环境
- ReactJs入门思路小指南
- 初识React
- 手把手教你在Windows下搭建React Native Android开发环境
- Codeforces Round #336 (Div. 2)C. Chain Reaction DP
- ANGULAR 2 FOR REACT DEVELOPERS
- React with TypeScript 系列(一) --概述
- webpack使用babel 6打包react报错
- React gulp、Browserify、Webpack实例
- iOS.ReactNative-5-make-react-native-to-support-dynamically-update
- react native error record
- 小记react-native 安装
- React - 交互性和动态UI
- Reactive Streams介绍
- 入职两月半的工作业务总结
- React-Native(iOS) 学习一 环境安装