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. (转)server.urlencode有什么用?怎么用?

    这个函数主要是为了,不同浏览器的事,如果你传递的参数是中文,有的浏览器就显示出错,如果转换成编码形式,就可以正确传递!当然在IE下2个结果都是一样的,你看不出来区别,用编码来传递参数是一个好习惯,养成 ...

  2. iOS开发之录音

    录音 除了上面说的,在AVFoundation框架中还要一个AVAudioRecorder类专门处理录音操作,它同样支持多种音频格式.与AVAudioPlayer类似,你完全可以将它看成是一个录音机控 ...

  3. Php ORM 对象关系映射

    ORM的全称是Object Relational Mapping,即对象关系映射.它的实质就是将关系数据(库)中的业务数据用对象的形式表示出来,并通过面向对象(Object-Oriented)的方式将 ...

  4. Qt5如何设置静态编译,解决生成的可执行文件打开出错问题

    将https://yunpan.cn/cqGGURjmG2fEY  访问密码 8de5  中的压缩包Qt5-MSVC-Static-master.zip 解压到你的qt安装目录,一般就是C:\Qt下, ...

  5. Ubuntu 10.04启动进入命令行模式

    进终端先  运行 sudo gedit /etc/default/grub  找到 GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash”  改为 GRUB_CMDLINE ...

  6. js前台与后台数据交互

    客户端脚本一般都在前台,这里讲的是(1)在后台调用前台定义的脚本(2)在后台如何注册客户端脚本 用途 何时使用服务器代码向页中添加客户端脚本: u 当客户端脚本的内容依赖于直到运行时才可用的信息时 u ...

  7. python之scrapy入门教程

    看这篇文章的人,我假设你们都已经学会了python(派森),然后下面的知识都是python的扩展(框架). 在这篇入门教程中,我们假定你已经安装了Scrapy.如果你还没有安装,那么请参考安装指南. ...

  8. OpenStack点滴02-WSGI

    OpenStack对外提供REST API,那么REST API是如何建立起来的呢? OpenStack用了一种叫WSGI(Web Service Gateway Interface)的东西,WSGI ...

  9. Jasper_filter data_pass field data from main to sub to filter some data

    main report: 1 add variable <variable name="Variable_rule" class="java.lang.String ...

  10. 一条sql,有分页,表合并查询,多表连接,用于oracle数据库

    SELECT * FROM ( SELECT TT.*,ROWNUM RN FROM ( SELECT A.CASE_ID AS TREATID, A.TYPE AS TYPE, B.CONTENT ...