[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 ...
随机推荐
- HTML高级标签之表格标签
前面学习了一下HTML的经常使用标签, 今天開始高级标签之路! 一.表格标签 1.作用: 创建一张表格 2.各属性作用: <table cellspacing="0" cel ...
- BFS模版程序
本文转自q=bfs&u=cnyali&t=blog">http://so.csdn.net/so/search/s.do?q=bfs&u=cnyali& ...
- windows系统自带工具
辅助功能向导:单击"開始→执行",在弹出的对话框中输入:accwiz 计算器:单击"開始→执行",在弹出的对话框中输入:calc 字符影射表:单击"開 ...
- 第二遍回顾--①前端flex布局
1.flex: 弯曲,收缩 2.概念 2条主轴,main axis,cross axis; 每个单元为flex item,主轴空间main size,交叉轴空间cross size; 3.容器 .co ...
- 【2017中国大学生程序设计竞赛 - 网络选拔赛 && hdu 6154】CaoHaha's staff
[链接]点击打开链接 [题意] 给你一个面积,让你求围成这个面积最少需要几条边,其中边的连线只能是在坐标轴上边长为1的的线或者是两个边长为1 的线的对角线. [题解] 找规律题 考虑s[i]表示i条边 ...
- 洛谷 P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 题目描述 回文数是指从左向右念和从右向左念都一样的数.如12321就是一个典型的回文数. 给定一个进制B(2< ...
- 洛谷 P1691 有重复元素的排列问题
P1691 有重复元素的排列问题 题目描述 设R={r1,r2,……,rn}是要进行排列的n个元素.其中元素r1,r2,……,rn可能相同.使设计一个算法,列出R的所有不同排列. 给定n以及待排列的n ...
- C++的模板template
模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数.返回值取得任意类型. 模板是一种对类型进行参数化的工具: 因此,使用模板的目 ...
- 格式化时间的一个好方法(补充moment)
/** * * 格式化时间 * @param {*} time * @param {*} fmt * @returns * time(new Date(), 'yyyy/MM/dd') ==> ...
- JS错误记录 - 取消事件冒泡、按钮、回车、ctrl回车提交留言
window.onload = function () { var oDiv = document.getElementById('div1'); var oBtn = document.getEle ...