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. ref - 按引用传递参数

    传递的是引用 在 形参 实参前 加ref

  2. Swift隐式可选型简单介绍

    /* 隐式可选型 */ // 隐式可选型同样可以赋值为nil, 而且在后面对这个变量的使用也可以不用进行解包 var value: String! = nil // print(value) 这行代码 ...

  3. jQuery ui 利用 datepicker插件实现开始日期(minDate)和结束日期(maxDate)

    这篇文章主要介绍了jQuery ui 利用 datepicker插件实现开始日期(minDate)和结束日期(maxDate),需要的朋友可以参考下 使用jQuery ui首先需要引入jQuery类库 ...

  4. Spring Boot Web项目之参数绑定

    一.@RequestParam 这个注解用来绑定单个请求数据,既可以是url中的参数,也可以是表单提交的参数和上传的文件 它有三个属性,value用于设置参数名,defaultValue用于对参数设置 ...

  5. core java 第四章笔记

    import java.util.*; public class Employee { private static int nextid = 1; private String name; priv ...

  6. 遍历std::list过程中删除元素后继续遍历过程

    std::list::erase Erase elements Removes from the list container either a single element (position) o ...

  7. QML之窗口(无边框、透明及拖拽)

    1.无边框 Qt Quick 2.0 中 QQuickView代替了1.0中的QDeclarativeView. 无边框窗口代码如下: QQuickView viwer; //QQuickView继承 ...

  8. Qt Quick App的两种启动模式

    QQmlApplicationEngine搭配Window QQuickView搭配Item 两者不同之处在于: 使用QQuickView显示QML文档,对窗口的控制权(比如设置窗口标题.Icon.窗 ...

  9. 中文man帮助安装

    下面我们来安装下中文man帮助 首先在http://pkgs.fedoraproject.org/repo/pkgs/man-pages-zh-CN/manpages-zh-1.5.2.tar.bz2 ...

  10. 自定义窗口 mfc

    typedef struct _WNDCLASS { UINT style; //制定窗口的类型 WNDPROC lpfnWndProc; int cbClsExtra; //额外的数值 int cb ...