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. php学习笔记4

    PHP数据类型: String(字符串), Integer(整型), Float(浮点型), Boolean(布尔型), Array(数组), Object(对象), NULL(空值). 说明:var ...

  2. spring基础内容

      关注和收藏在这里   深入理解Spring框架的作用 纵览Spring , 读者会发现Spring 可以做非常多的事情. 但归根结底, 支撑Spring的仅仅是少许的基本理念, 所有的理念都可以追 ...

  3. session应用二

    从session中获取mapper对象,利用mapper对象进行增删改查 Date now = new Date(); SqlSession session = this.yangchebaoDbMa ...

  4. 王小川分享AI

    王小川的分享:Link

  5. node中间层

    node中间层 一.总结 1.node中间层作用:前端也是mvc,NodeJS之后,前端可以更加专注于视图层,而让更多的数据逻辑放在Node层处理 2.node中间层作用:当发现所有请求量太多应付不过 ...

  6. 利用jquery.fullPage.js 和 scrolloverflow.min.js 实现滚屏效果

    参考链接:https://blog.csdn.net/c11073138/article/details/79631036 /* 按着思路去search. */

  7. amazeui学习笔记--css(布局相关3)--辅助类Utility

    amazeui学习笔记--css(布局相关3)--辅助类Utility 一.总结 1.元素清除浮动: 添加 am-cf 这个 class 即可 2.水平滚动: .am-scrollable-horiz ...

  8. HTTP网络协议(四)

    确保Web安全的HTTPS HTTP存在三个比较明显的缺点: 通信使用明文(不加密),内容可能会被窃听. 不验证通信方的身份,因此有可能遭遇伪装. 无法证明报文的完整性,所以可能已遭篡改.  尽管HT ...

  9. Javascript和jquery事件--鼠标事件的小结

    1.鼠标事件的主要事件应该是mouseup, mousedown, mousewheel, mousemove, mouseover, moveout. <1>其中mouseup和mous ...

  10. Linux 命令笔记(1)

    [root@Oracle11_2 ~]# ll total -rw-------. root root May : anaconda-ks.cfg drwxr-xr-x. root root May ...