[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 ...
随机推荐
- OpenFace Docker 使用简介
在Docker中使用openface最大的问题是数据与主机的交互,下面我介绍几种方法来实现主机与Docker容器的数据交互. 1.第一种也是最方便的一种方法是在进入容器时使用-v参数将主机的目录挂载到 ...
- 在Eclipse中创建Maven多模块项目
在Eclipse中创建Maven多模块项目1,创建多模块项目选择File>New>Project,打开New Project窗口,选择Maven>Maven Project,选择下一 ...
- Advanced Fruits HDU杭电1503【LCS的保存】
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- ubuntu16.04安装配置mysql数据库,分割视频为帧图像
参考http://wiki.ubuntu.org.cn/MySQL%E5%AE%89%E8%A3%85%E6%8C%87%E5%8D%97 版本为5.7 一.安装 安装命令sudo apt-get i ...
- php中的self关键字和this关键字的区别和联系
php中的self关键字和this关键字的区别和联系 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行PHP的高级编程 ...
- mysql(8.0.*版本 windows10 )忘记密码解决方案
安装完mysql-8.0.13-winx64后,一些列的安装命令过后再执行mysql -uroot -p之后 报错了 what fuck 什么鬼,就是这个错 ERROR (): Access deni ...
- TCP/IP协议族简介
OSI网络分层介绍 网络结构的标准模型是OSI模型,由国际互联网标准化组织定义的网络分层模型.虽然目前没有完全按照这种模型实现的网络协议栈,但是学习这个模型对于我们理解网络协议还是很有帮助的. 1.O ...
- BZOJ 1786 DP
思路: 肯定从小往大填合适了 f[i][j]表示第i个数是j的最少逆序对数 f[i][j]=min(f[i-1][k]+cost,f[i][j]) 优化一下成O(nk)就好啦~ (不优化也可以过的-) ...
- BZOJ 2212线段树的合并
借鉴(抄)了一下题解-- 线段树合并的裸题吧- //By SiriusRen #include <cstdio> #include <cstring> #include < ...
- ubuntu下安装VMware
1 用apt-get命令更新系统 loginname@localhost:~$ sudo apt-get update 2 从官方网站下载Workstation11(Bundle Script) lo ...