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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. TPO5-1 Minerals and plants

    Only recently have investigators considered using these plants to clean up soil and waste sites that ...

  4. react与jQuery对比,有空的时候再翻译一下

    参考资料:http://reactfordesigners.com/labs/reactjs-introduction-for-people-who-know-just-enough-jquery-t ...

  5. 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, ...

  6. [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 ...

  7. [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 ...

  8. react programming

    So you're curious in learning this new thing called Reactive Programming, particularly its variant c ...

  9. react图工具集成

    背景 调查了react下的图工具库, 并继承到项目中, 经过调研列出如下两个图工具库,可以同时使用. data-ui react-c3js 在一个工具中没有所需的图时候, 可以使用另一个替代. dat ...

随机推荐

  1. 洛谷 P1303 A*B Problem

    P1303 A*B Problem 题目描述 求两数的积. 输入输出格式 输入格式: 两行,两个数. 输出格式: 积 输入输出样例 输入样例#1: 复制 1 2 输出样例#1: 复制 2 说明 每个数 ...

  2. Mining Station on the Sea (hdu 2448 SPFA+KM)

    Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  3. 《Java实战开发经典》第五章5.3

    package xiti5; public class Third { public static void main(String[] args) { T t=new T("want yo ...

  4. java的23中设计模式

    一.设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接 ...

  5. Java中接口继承泛型接口

    在使用Mybatis做web开发时,每一个模块的数据持久层的接口都会定义:增删改查四个方法.我想为什么不新建一个Base接口来做所有数据持久层的父接口呢? 于是,我试验了一下,建立了一个泛型接口,里面 ...

  6. Altium Designer中死铜的问题

  7. jmeter--九种定时器介绍(包括思考时间、集合点)

    知识来源:http://www.cnblogs.com/imyalost/p/6004678.html jmeter提供了很多元件,帮助我们更好的完成各种场景的性能测试,其中,定时器(timer)是很 ...

  8. GO语言学习(一)Windows 平台下 Go 语言的安装和环境变量设置

    1. Go 语言 SDK 安装包下载和安装 GO语言安装包下载地址:https://www.golangtc.com/download 下载 go1.9.2.windows-amd64 2. Go 语 ...

  9. 【CS Round #48 (Div. 2 only)】8 Divisible

    [链接]h在这里写链接 [题意] 给你一个长度为n的数字(n<=1000) 然后让你任意组合这个数字. 使得这个数字能被8整除. (不能出现前导0) [题解] 只要后三位能被8整除就可以了. 则 ...

  10. ping 本地端口

    C:\Users\Administrator>netstat -ano | findstr 8001