[React Testing] Intro to Shallow Rendering
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的更多相关文章
- [React Testing] Children with Shallow Rendering
When testing React components, we often want to make sure the rendered output of the component match ...
- [React Testing] className with Shallow Rendering
The React Shallow Renderer test utility lets us inspect the output of a component one level deep. In ...
- [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 ...
- 如何使用TDD和React Testing Library构建健壮的React应用程序
如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...
- React Testing All in One
React Testing All in One React 测试 https://reactjs.org/docs/testing.html jest 26.4 https://jestjs.io/ ...
- [React Testing] Element types with Shallow Rendering
When you render a component with the Shallow Renderer, you have access to the underlying object. We ...
- [React Testing] Conditional className with Shallow Rendering
Often our components have output that shows differently depending on the props it is given; in this ...
- [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 ...
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
随机推荐
- SQL 无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问。)"
用T-SQL命令附加数据库时,出现如下异常信息: 无法打开物理文件 XXX.mdf".操作系统错误 5:"5(拒绝访问.)". (Microsoft SQL Server ...
- 武汉科技大学ACM:1010: 零起点学算法89——母牛的故事
Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多个测 ...
- print流
PrintWriter和PrintStream都属于输出流,分别针对字符和字节. PrintWriter和PrintStream提供了重载的print,println方法用于多种类型的输出 Print ...
- 正则表达式中/g的含义
关于正则表达式 g,m 参数的总结,为了回答“正则表达式(/[^0-9]/g,'')中的"/g"是什么意思?” 为了解答“正则表达式(/[^0-9]/g,'')中的" ...
- 不要依赖hibernate的二级缓存
一.hibernate的二级缓存 如果开启了二级缓存,hibernate在执行任何一次查询的之后,都会把得到的结果集放到缓存中,缓存结构可以看作是一个hash table,key是数据库记录的id ...
- flask开发restful api系列(8)-再谈项目结构
上一章,我们讲到,怎么用蓝图建造一个好的项目,今天我们继续深入.上一章中,我们所有的接口都写在view.py中,如果几十个,还稍微好管理一点,假如上百个,上千个,怎么找?所有接口堆在一起就显得杂乱无章 ...
- CFont 字体类
CFont 字体类 初始化函数 CreateFontIndirect 初始化一个由LOGFONT结构给出其特征的CFont对象 CreateFont 初始化用指定特性定义的CFont对象 Create ...
- web UI
Semantic不错的UI,代码非常详细
- java疯狂演义----简单java IDE工具
file:commons package org.crazyit.editor.commons; import org.crazyit.editor.EditorFrame; import org.c ...
- 实用C51编程的高级技巧(C51编程)
一.C51热启动代码的编制 void main() { char data *HotPoint=(char *)0x7f; if((*HotPoint==0xaa)&&(*(--Hot ...