In this lesson, we walk through how to use one of React's Test Utilities (from thereact-addons-test-utils package) called "Shallow Rendering". This lets us render our React component one level deep - without a DOM - so that we can write tests for it. It works kind of like ReactDOM.render, where the shallow renderer is a temporary place to "hold" your rendered component so that you can assert things about its output. Tests written using the shallow renderer are great for stateless or "dumb" components that simply have their props passed to them from a parent container or "smart" component. These shallow renderer tests work especially well with stateless function components. They also work well for "unit" tests where you want to make sure your code works in isolation.

_NOTE: The React team has recommended composing the majority of your apps using these stateless "dumb" components, so the majority of lessons in this course will focus on writing simple unit tests for these stateless components using Shallow Rendering. If you also want to write tests for the stateful components that are tied to different components and state and can't be tested in isolation, you may want to look at using a DOM (with something like Karma or jsdom) and React's other test utilities like renderIntoDocument and Simulate. However, I've found that it is helpful to try to compose most of your project with simple, isolated, stateless or "pure" components that can be unit tested with Shallow Rendering, and then wrap these components with a few stateful or "impure" components that you can either not worry about testing (what I do most of the time because it is difficult to test stateful components), or write separate integration and functional tests for them using different tools.

import React from 'react';
import expect from 'expect';
import TestUtils from 'react-addons-test-utils'; const CoolComponent = ({greeting}) => {
return (
<div>
<h1>Greeting</h1>
<div>{greeting}</div>
</div>
);
}; describe('CoolComponent', ()=>{ it('should ...', ()=>{
//Shallow Rendering
const renderer = TestUtils.createRenderer(); renderer.render(<CoolComponent greeting='hello world' />);
const output = renderer.getRenderOutput(); console.log(output);
});
});

It outputs:

{ '$$typeof': Symbol(react.element),
type: 'div',
key: null,
ref: null,
props: { children: [ [Object], [Object] ] },
_owner:
{ _currentElement:
{ '$$typeof': Symbol(react.element),
type: [Function: CoolComponent],
key: null,
ref: null,
props: [Object],
_owner: null,
_store: {} },
_rootNodeID: '.atjgairf9c',
_instance:
StatelessComponent {
props: [Object],
context: {},
refs: {},
updater: [Object],
_reactInternalInstance: [Circular],
state: null },
_pendingElement: null,
_pendingStateQueue: null,
_pendingReplaceState: false,
_pendingForceUpdate: false,
_renderedComponent: { _renderedOutput: [Circular], _currentElement: [Circular] },
_context: {},
_mountOrder: 1,
_topLevelWrapper: null,
_pendingCallbacks: null },
_store: {} }

[React Testing] Intro to Shallow Rendering的更多相关文章

  1. [React Testing] Children with Shallow Rendering

    When testing React components, we often want to make sure the rendered output of the component match ...

  2. [React Testing] className with Shallow Rendering

    The React Shallow Renderer test utility lets us inspect the output of a component one level deep. In ...

  3. [React Testing] JSX error diffs -- expect-jsx library

    When writing React component tests, it can be hard to decipher the error diffs of broken tests, sinc ...

  4. 如何使用TDD和React Testing Library构建健壮的React应用程序

    如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...

  5. React Testing All in One

    React Testing All in One React 测试 https://reactjs.org/docs/testing.html jest 26.4 https://jestjs.io/ ...

  6. [React Testing] Element types with Shallow Rendering

    When you render a component with the Shallow Renderer, you have access to the underlying object. We ...

  7. [React Testing] Conditional className with Shallow Rendering

    Often our components have output that shows differently depending on the props it is given; in this ...

  8. [React Testing] Setting up dependencies && Running tests

    To write tests for our React code, we need to first install some libraries for running tests and wri ...

  9. [React & Testing] Simulate Event testing

    Here we want to test a toggle button component, when the button was click, state should change, styl ...

随机推荐

  1. UGUI之UI的深度问题

    学过NGUI的都知道,NGUI的深度是通过值来控制的.Panel也是UI也是,如果空间太多,布局复杂UI深度的值会变得很混乱.所以在NGUI中设置UI深度时一定要多加思考.然而在UGUI控制显示顺序的 ...

  2. Springmvc整合mybatis

    http://blog.csdn.net/geloin/article/details/7536968 http://blog.csdn.net/woshi74/article/details/378 ...

  3. 编程语言大牛王垠:编程的智慧,带你少走弯路 [本文转载CocoaChina]

    作者:王垠 授权本站转载. 编程是一件创造性的工作,是一门艺术.精通任何一门艺术,都需要很多的练习和领悟,所以这里提出的“智慧”,并不是号称三天瘦二十斤的减肥药,它并不能代替你自己的勤奋.然而我希望它 ...

  4. (四)Angularjs - 小实例(2)

    自定义指令编写时钟 模板 <!-- 模板文件 --><html> <!-- 内置的ng-app指令通知编译器启动AngularJS框架--> <body ng ...

  5. keepalived + nginx

    本文主要介绍keepalived的安装,Nginx自行解决,也可以使用httpd.随便任何服务都可以... keepalived 官网http://www.keepalived.org/index.h ...

  6. Array and its point.

    a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...

  7. Andriod手势密码破解

    ★ 引子 之前在Freebuf上看到一片文章讲Andriod的手势密码加密原理,觉得比较有意思,所以就写了一个小程序试试. ★ 原理            Android的手势密码加密原理很简单: 先 ...

  8. <Pro .NET MVC4> 三大工具之依赖注入神器——Ninject

    这篇内容是对<Pro .NET MVC4>一书中关于Ninject介绍的总结. Ninject是.NET MVC的一款开源的依赖注入工具. 使用场景:当MVC项目中使用了依赖注入技术来给程 ...

  9. Linux read语法及浅析

    read命令 -n(不换行) -p(提示语句) -n(字符个数) -t(等待时间) -s(不回显) 1.基本读取read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后, ...

  10. yum 使用说明

    linux如何安装yum 使用YUM来安装软件,就可以不用去到处找依赖关系的RPM了.很方便. 第一步:安装yum  第二步:下载createrepo包并安装 下载地址: ftp://195.220. ...