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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 转载 React.createClass 对决 extends React.Component

    先给出结论,这其实是殊途同归的两种方式.过去我们一般都会使用 React.createClass 方法来创建组件,但基于 ES6 的小小语法糖,我们还可以通过 extends React.Compon ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 【react学习】关于react框架使用的一些细节要点的思考

    ( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的)   这篇文章主要是写关于学习react中的一些自己的思考:   1.set ...

  8. React Native 系列(二) -- React入门知识

    前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...

  9. React-Native(三):React Native是基于React设计的

    React Native是基于React js设计的. 参考:<React 入门实例教程> React 起源于 Facebook 的内部项目,因为该公司对市场上所有 JavaScript ...

随机推荐

  1. Gzip压缩优化网站

    网站常使用GZIP压缩算法对网页内容进行压缩,然后传给浏览器,以减小数据传输量,提高响应速度.浏览器接收到GZIP压缩数据后会自动解压并正确显示.GZIP加速常用于解决网速慢的瓶颈. 压缩Filter ...

  2. 2015 Multi-University Training Contest 2 1006 Friends 壮压

    题目链接 题意:t 组測试数据,每组測试数据有 n个人,m条关系 每条关系能够是 "线上关系" 或者 "线下关系". 要求每一个人的线上关系(条数) == 线下 ...

  3. 【C++】String类中的运算符重载

    模块化设计: 头文件: <span style="font-size:18px;"> #ifndef operator_operator_h #define opera ...

  4. Cocos2d-x 3.0 红孩儿私家必修 - 第一章 初识Cocos2d-x 3.0project

    第一章    初识Cocos2d-x 3.0project Cocos2d-x 3.0出来了,听说与之前版本号相比修改较大 做为一个游戏开发人员.我们应该欢迎Cocos2d-x持续的更新和强大,Coc ...

  5. BZOJ 2124: 等差子序列 线段树维护hash

    2124: 等差子序列 Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一 ...

  6. java.lang.ClassNotFoundException: org.objectweb.asm.ClassWriter

    转自:https://www.cnblogs.com/yfceshi/p/6814802.html Caused by: javax.xml.ws.WebServiceException: java. ...

  7. 1. Two Sum[E]两数之和

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  8. POJ 2388 基数排序

    这题可以直接nth_element过去 比如这样子 //By SiriusRen #include <cstdio> #include <algorithm> using na ...

  9. Android 中的View与ViewGroup

    Android重点知识--View和ViewGroup与自定义控件 作者:丁明祥 邮箱:2780087178@qq.com 一.基础 ViewGroup 参考资料: Android 手把手教您自定义V ...

  10. 云上建站快速入门:博客、论坛、CMS、电子商务网站统统搞定

    现在制作一个网站已经越来越容易了,只要知道清晰的流程之后都是可以很快的建好一个企业或者个人网站的!免费的建站程序很多,下面听哥给你亮出来,建站一般来说分主要有这四步:申请域名.申请虚拟主机.制作网页, ...