[React] Integration test a React component that consumes a Render Prop
In this lesson, I use Enzyme and Jest's Snapshot functionality to write an integration test for a component called CounterConsumer that consumes the Render Prop component Counter. This integration test is great because it doesn't necessarily care that CounterConsumer uses Counter behind the scenes, just that it works when integrated.
import React from "react";
import Counter from "./Counter"; export default function CounterConsumer({ initial }) {
return (
<Counter initial={initial}>
{({ increment, decrement, counter }) => (
<div className="content" style={{ textAlign: "center" }}>
<h1>{counter}</h1>
<button className="button is-success" onClick={increment}>
Increment
</button>
<button className="button is-danger" onClick={decrement}>
Decrement
</button>
</div>
)}
</Counter>
);
}
test:
import React from "react";
import ReactDOM from "react-dom";
import toJSON from "enzyme-to-json";
import { mount } from "enzyme";
import "./enzymeSetup";
import CounterConsumer from "./CounterConsumer"; it("accepts an initial value", () => {
const wrapper = mount(<CounterConsumer initial={} />);
expect(toJSON(wrapper)).toMatchSnapshot();
}); it("increments counter", () => {
const wrapper = mount(<CounterConsumer />);
wrapper
.find("button")
.at()
.simulate("click"); expect(toJSON(wrapper)).toMatchSnapshot();
}); it("decrements counter", () => {
const wrapper = mount(<CounterConsumer />);
wrapper
.find("button")
.at()
.simulate("click"); expect(toJSON(wrapper)).toMatchSnapshot();
});
[React] Integration test a React component that consumes a Render Prop的更多相关文章
- [React + Mobx] Mobx and React intro: syncing the UI with the app state using observable and observer
Applications are driven by state. Many things, like the user interface, should always be consistent ...
- [React] Unit test a React Render Prop component
In this lesson, I use Enzyme and Jest to unit test a Counter Render Prop component. Writing integrat ...
- 转载 React.createClass 对决 extends React.Component
先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...
- [React] Use React.memo with a Function Component to get PureComponent Behavior
A new Higher Order Component (HOC) was recently released in React v16.6.0 called React.memo. This be ...
- [React Router] Create a ProtectedRoute Component in React Router (setState callback to force update)
In this lesson we'll create a protected route just for logged in users. We'll combine a Route with a ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...
- 【react学习】关于react框架使用的一些细节要点的思考
( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的) 这篇文章主要是写关于学习react中的一些自己的思考: 1.set ...
- React Native 系列(二) -- React入门知识
前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...
- React-Native(三):React Native是基于React设计的
React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...
随机推荐
- [SharePoint][SharePoint Designer 入门经典]Chapter10 Web部件链接
本章概要: 1.Web部件作用 2.如何添加和配置 3.如何个性化 4.如何导出,并在其他站点重利用 5.通过组合web part创建复杂的用户界面
- 树莓派与window 10组成的物联网核心:让人失望
去年春天,微软公布了自己的window系统与物联网系统的方案,该方案使用树莓派和window 10组成物联网的核心.树莓派是一个与window全然不同的执行在ARM构架下的系统. 是的,也许微软决心离 ...
- 解惑rJava R与Java的快速通道
阅读导读: 1.什么是RJava? 2.怎样安装RJava? 3.怎样用RJava实现R调用Java? 1. rJava介绍 rJava是一个R语言和Java语言的通信接口.通过底层JNI实现调用,同 ...
- 局域网网络性能測试方法HDtune 64K有缓存測速法,让你得知你的网络性能
该方法能够有效測试出您的网络传输性能究竟有多高,该方法通用于有盘,无盘(系统虚拟盘) ,游戏虚拟盘,以及其它带有缓存功能的虚拟盘软件,可是由于每款软件的工作方式和原理都不仅同样,所以每款软件的測试结果 ...
- c10---多文件开发
a.h // // lisi.h // 注意: .h是专门用来被拷贝的, 不会参与编译 #ifndef day05_lisi_h #define day05_lisi_h int sum(int v1 ...
- Linux操作系统下Oracle主要监控工具介绍
Oracle监控包括有效且完全地监控Oracle数据库的性能.可用性和使用率等统计量,还包括即时的错误通知和纠正措施,并提供全面的报表和图表.本文中主要介绍几种Linux操作系统下Oracle主要监控 ...
- 【BZOJ 2821】作诗
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2821 [算法] 如果不强制在线,显然莫队是可以解决此题的,那么,强制在线怎么办呢? ...
- [Tomcat]Tomcat6和Tomcat7的区别
Tomcat 7最大的改进是对Servlet 3.0和Java EE 6的支持.◆Tomcat 7完全支持Servlet 3.0规范◆Tomcat 7新增了对Java注释的支持◆Tomcat 7通过w ...
- checkbox改写
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Docker运行程序报错 WARNING: IPv4 forwarding is disabled. Networking will not work
WARNING: IPv4 forwarding is disabled. Networking will not work. 第一步:vi /usr/lib/sysctl.d/00-system ...