[React] Test friendly approach
Add functional function such as change state, this should have tests covered.
For example, in a component, there is a function call 'addBox':
onAddBox = () => {
const newBox = {
id : this.state.boxes.length,
color : 'red'
};
const boxes = addBox(this.state.boxes, newBox);
this.setState({ boxes });
};
Here we use a function call 'addBox', this is written in a new file:
export const addBox = (boxes, newBox) => boxes.concat(newBox);
SO when need to use it, we need to import it to the component, not just write this function into component methods. Because if we make this function sprated from component methods, we are able to test it by just simply import this function to the test file.
import {addBox} from '../components/App/AppHelper';
const boxes = [
{
id : ,
color : 'red'
},
{
id : ,
color : 'red'
}
];
const newBox = {
id: ,
color: 'green'
};
test('Should be able to add new box', () => {
const expected = [
{
id : ,
color : 'red'
},
{
id : ,
color : 'red'
},
{
id: ,
color: 'green'
}
];
const result = addBox(boxes, newBox);
expect(result).toEqual(expected);
});
test('addBox should be immutable', () => {
const result = addBox(boxes, newBox);
expect(result).not.toBe(boxes);
});
Here has two test, one is to test 'addBox' should actually add a new box to the existing array. Second test is to make sure we don't mutatue origianl data.
[React] Test friendly approach的更多相关文章
- JavaScript Application Architecture On The Road To 2015
JavaScript Application Architecture On The Road To 2015 I once told someone I was an architect. It’s ...
- why updating the Real DOM is slow, what is Virtaul DOM, and how updating Virtual DOM increase the performance?
个人翻译: Updating a DOM is not slow, it is just like updating any JavaScript object; then what exactly ...
- TPO5-1 Minerals and plants
Only recently have investigators considered using these plants to clean up soil and waste sites that ...
- react与jQuery对比,有空的时候再翻译一下
参考资料:http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-t ...
- ANGULAR 2 FOR REACT DEVELOPERS
Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...
- [Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
Applications are driven by state. Many things, like the user interface, should always be consistent ...
- react programming
So you're curious in learning this new thing called Reactive Programming, particularly its variant c ...
- react图工具集成
背景 调查了react下的图工具库, 并继承到项目中, 经过调研列出如下两个图工具库,可以同时使用. data-ui react-c3js 在一个工具中没有所需的图时候, 可以使用另一个替代. dat ...
随机推荐
- Node里面的对象创建问题
1.利用new Object()创建时 var a =new Object() a.b = 1 console.log(a) // 打印出来是[object Object] console.log(J ...
- CISP/CISA 每日一题 九(2017-11-30 09:25)
电子银行风险管理责任: 1.风险管理是董事会和高级管理层的责任 2.实施技术是信息技术高级管理层的责任 3.测量和监控风险是经营管理层的责任 管理层在实施一个新的电子银行应用程序之前要 ___ ...
- 03002_MySQL数据库的安装和配置
1.MySQL的安装 (1)下载mysql-5.5.49-win32.msi, 链接:MySQL安装包下载 密码:geqh : (2)打开下载的MySQL安装文件mysql-5.5.27-win32. ...
- 腾讯官网生成qq在线客服代码
http://jingyan.baidu.com/article/e2284b2b42ce8ce2e6118ddd.html
- sql 高性能存储过程分页
USE [Lyjjr] GO /****** Object: StoredProcedure [dbo].[P_ViewPage] Script Date: 05/29/2015 17:18:56 * ...
- LayoutAnimation-容器动画
1.LayoutAnimation的作用主要就是加载到一个layout上,让这个layout里面的所有控件都有相同的动画效果.现在用到的是在listview中添加动画,使得它每一个item都是滑落显示 ...
- 认识一下JavaScript
1.什么是JavaScript? JavaScript简称“JS”,应用于HTML和WEB,广泛应用于服务器.PC.笔记本等设备. JavaScript 是 Web 的编程语言. 所有现代的 HTML ...
- 开发板 Linux驱动视频 驱动是什么
内存管理单元很重要. linux把设备看成文件,(open,read,write,ioctrl,close)主要写这几个函数. 哈弗结构,取指令和取数据同时进行. arm处理器体系架构以及发展方向 单 ...
- logback--How do I configure an AsyncAppender with code? 转载
原文地址:https://github.com/tony19/logback-android/issues/54 Please provide an example of how to configu ...
- trunc与round
TRUNC(number[,num_digits]) number 需要截尾取整的数字. num_digits 用于指定取整精度的数字.Num_digits 的默认值为 0. 作用:截断数字和时间 ...