[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 ...
随机推荐
- JavaScript 进度条重复加载
<!DOCTYPE HTML> <html> <head> <meta charset ="utf-8"> <title> ...
- CISP/CISA 每日一题 17
CISSP 每日一题(答) What are often added to passwords to maketheir resultant hash secure and resistant to ...
- 【习题 7-1 UVA-208】Firetruck
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 预处理一下终点能到达哪些点. 暴力就好. 输出结果的时候,数字之间一个空格.. [代码] /* 1.Shoud it use lon ...
- ActiveMQ学习总结(6)——ActiveMQ集成Spring和Log4j实现异步日志
我的团队和我正在创建一个由一组RESTful JSON服务组成的服务平台,该平台中的每个服务在平台中的作用就是分别提供一些独特的功能和/或数据.由于平台中产生的日志四散各处,所以我们想,要是能将这些日 ...
- 6. MongoDB
https://www.mongodb.com/ https://pan.baidu.com/s/1mhPejwO#list/path=%2F 安装MongoDB# 安装MongoDB http:// ...
- MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- 项目太大tomcat启动不起来
双击server,Open launch configuration Arguments VM arguments增加参数: -Xms512m -Xmx1024m -Xss4m -XX:PermSiz ...
- Codeforces 232A - Cycles (构造 + 思维)
题目链接: 232A - Cycles(点击打开) 题意: 要构成一个存在 \(k\) 个三元环的图,需要多少个点,输出顶点数 \(n\),并输出图. 题解: 题目中的任何图都可以用 \(90\)~ ...
- HTTP网络协议(三)
HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: 首部字段 说明 Cache-Control 控制缓存的行为 Connection 逐跳首部.连接 ...
- fatfs输出目录
利用fatfs文件系统, 如何输出SD卡根目录下的各个文件夹名字呢? 程序如下: u8 Dirname_i; u8 Dirname_j; DIR dir; //读取txt里的目录用,还是要把fatfs ...