1. 程式人生 > >Testing in React: Getting Off The Ground

Testing in React: Getting Off The Ground

A first look: the Low Level Approach

Let’s start with a low level approach by using the React specific Test Utils. Test Utils enables a concept called shallow rendering, which simply means rendering a component one level deep, neglecting any child components.

import TestUtils from 'react-addons-test-utils';
const renderer = TestUtils.createRenderer();
function shallow(Component, props) {
renderer.render(<Component {...props} />);
return renderer.getRenderOutput();
}

By implementing our own shallow function, we can simply call shallowRenderer internally and return a shallow rendered component.

Now we can simply test a Bar component for example.

const Bar = React.createClass({
render() {
return (
<div className="bar">{this.props.title}</div>
)
}
});

Here’s our very first test, we want to make sure that the title passed in via props is being rendered.

describe('low level testing a React Application', () => {
it
('should shallow render', () => {
const bar = shallow(Bar, {title: 'fooBar'});
expect(bar.type).toBe('div');
expect(bar.props.className).toBe('bar');
expect(bar.props.children).toBe('fooBar');
});
});

Shallow rendering doesn’t rely on any DOM, it returns a React Element one level deep meaning calling shallow(Bar, {title: ‘fooBar’}) will return an object.

{
$$typeof: ...,
type: 'div',
key: null,
ref: null,
props: {className: 'bar', children: 'bar'},
...
}

This also explains why we can assert bar.props.ClassName to be ‘fooBar’ for example.

Why shallow rendering? This approach has a couple of advantages but also a couple of drawbacks, obviously. One advantage is not having to deal with a real DOM, meaning no side effects. Further more, we can isolate the component quite comfortably, without having to depend on other components to function properly. The disadvantages include not being able to simulate clicks with Simulate (which we will get to later on) and no access to refs (references). There are workarounds to these limitations, but that also means a lot of extra work to implement in cases where we need to access references.

相關推薦

Testing in React: Getting Off The Ground

A first look: the Low Level ApproachLet’s start with a low level approach by using the React specific Test Utils. Test Utils enables a concept called shall

Components testing in React: what and how to test with Jest and Enzyme.

Testing React components may be challenging for beginners as well as experienced developers who have already worked with tests. It may be interesting to co

[React] Render Elements Outside the Current React Tree using Portals in React 16

wrap wrapper http att sso ood tps ant ref By default the React Component Tree directly maps to the DOM Tree. In some cases when you have

I created the exact same app in React and Vue. Here are the differences.

So you can see that we have passed the same data into both, but they’re simply labelled differently. So passing initial data into our components is very,

Position element at the bottom of the screen using Flexbox in React Native

Position element at the bottom of the screen using Flexbox in React NativeSource: British AirwaysReact Native uses Yoga to achieve Flexbox style layout, wh

Getting Started With Testing in Python

This tutorial is for anyone who has written a fantastic application in Python but hasn’t yet written any tests. Testing in Python is a huge topic and ca

Exception: Too many parameters were provided in this RPC request. The maximum

procedure protocol provided request stream Exception: Too many parameters were provided in this RPC request. The maximum is 2100. [Microsoft

[React] Render Text Only Components in React 16

home comment rep logs lin reat end ren version In this session we create a comment component to explore how to create components that onl

[Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream

() ppr and create tin tee rom other fun Rather than using Components to push streams into other Components, mapPropsStream allows you to

ssh: Could not resolve hostname git.*****-inc.com : Temporary failure in name resolution fatal: The remote end hung up unexpectedly

配置 soft mic target clas 無法執行 ssh pull 開發   問題出現的情景:使用git pull拉取開發的代碼到測試服務器,報錯:   ssh: Could not resolve hostname git.****-inc.com : Tempo

[React] Work with HTML Canvas in React

pil nbsp sso spl des automatic monit related overflow React‘s abstraction over the DOM means that it‘s not always obvious how to do DOM-r

[React] Reference a node using createRef() in React 16.3

lin node eat can lesson return using field div In this lesson, we look at where we came from with refs in React. Starting with the deprec

[React Native] Reduce Long Import Statements in React Native with Absolute Imports

other absolute fig Go react project only port gen In large React Native projects, it’s common to have long relative import paths li

[React] Refactor componentWillReceiveProps() to getDerivedStateFromProps() in React 16.3

exist ins date chan hang serve RR ogg stat The componentWillReceiveProps() method is being deprecated in future version of React (17). Ma

[git] -- warning: LF will be replaced by CRLF in app/app.iml. The file will have its original line endings in your working directory.

file 告訴 結束 windows 記錄 換行 錯誤 core HA 遇到這個錯誤,因為是git的換行符檢查功能。之前代碼是在MAC下,後來放在windows系統下,git add .的時候報這個錯誤。 core.safecrlf git 提供了一個換行符檢查功能(cor

【狀態壓縮 meet in middle】poj3139Balancing the Scale

per program 十分 滿足 single 狀態壓縮 scenario sort rotation 數組溢出真是可怕的事情 Description You are given a strange scale (see the figure below)

E. Turn Off The TV Educational Codeforces Round 29

algo cat tac () span mes stack turn struct http://codeforces.com/contest/863/problem/E 註意細節 1 #include <cstdio> 2 #include <c

[React] Extend styles with styled-components in React

code order pan radi splay upper clas tran imp In this lesson, you will learn how to extend styles from one styled-component to another in

[Unit Testing] Fundamentals of Testing in Javascript

sar help catch ret same develop more ESS cts In this lesson, we’ll get the most fundamental understanding of what an automated test

You need tcl 8.5 or newer in order to run the Redis test.

You need tcl 8.5 or newer in order to run the Redis test. 安裝Redis,執行make test的時候出錯: You need tcl 8.5 or newer in order to run the Redis test make: *** [te