[React Testing] Conditional className with Shallow Rendering
Often our components have output that shows differently depending on the props it is given; in this lesson, we go over how to compare the className prop element tree output based on conditional input.
// LikeCounter.js import React from 'react';
import classnames from 'classnames'; const LikeCounter = ({count, isActive}) => {
return <a
className={
classnames({
'LikeCounter--active': isActive
})
}
href="#">Like: {count}</a>
} export default LikeCounter; // LikeCounter.spec.js
import React from 'react';
import expect from 'expect';
import expectJSX from 'expect-jsx';
import TestUtils from 'react-addons-test-utils';
import LikeCounter from './likeCounter'; describe('LikeCOunter', ()=>{ it('should be a link', ()=>{
const renderer = TestUtils.createRenderer();
renderer.render(<LikeCounter count={5} />);
const actual = renderer.getRenderOutput().type;
const expected = 'a';
expect(actual).toEqual(expected);
});
}); describe('active class', ()=>{
it('should have active class based on isActive props: true', ()=>{ const renderer = TestUtils.createRenderer();
renderer.render(<LikeCounter count={5} isActive={true}/>);
const actual = renderer.getRenderOutput().props.className.includes('LikeCounter--active');
const expected = true;
expect(actual).toEqual(expected);
}); it('should have active class based on isActive props: false', ()=>{ const renderer = TestUtils.createRenderer();
renderer.render(<LikeCounter count={5} isActive={false}/>);
const actual = renderer.getRenderOutput().props.className.includes('LikeCounter--active');
const expected = false;
expect(actual).toEqual(expected);
});
});
[React Testing] Conditional className with Shallow Rendering的更多相关文章
- [React Testing] Element types with Shallow Rendering
When you render a component with the Shallow Renderer, you have access to the underlying object. We ...
- [React Testing] className with Shallow Rendering
The React Shallow Renderer test utility lets us inspect the output of a component one level deep. In ...
- [React Testing] Intro to Shallow Rendering
In this lesson, we walk through how to use one of React's Test Utilities (from thereact-addons-test- ...
- [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 ...
- 如何使用TDD和React Testing Library构建健壮的React应用程序
如何使用TDD和React Testing Library构建健壮的React应用程序 当我开始学习React时,我努力的一件事就是以一种既有用又直观的方式来测试我的web应用程序. 每次我想测试它时 ...
- React Testing All in One
React Testing All in One React 测试 https://reactjs.org/docs/testing.html jest 26.4 https://jestjs.io/ ...
- [React Testing] Children with Shallow Rendering
When testing React components, we often want to make sure the rendered output of the component match ...
- [React Testing] Reusing test boilerplate
Setting up a shallow renderer for each test can be redundant, especially when trying to write simila ...
- [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 ...
随机推荐
- php5.3.3安装mongo扩展
/usr/bin/phpize./configure --with-php-config=/usr/bin/php-configmake && make install/usr/sbi ...
- JS判断图片是否加载完成三种方式
1.img的complete属性 轮询不断监测img的complete属性,如果为true则表明图片已经加载完毕,停止轮询.该属性所有浏览器都支持. <p id="p1"&g ...
- CentOS安装JDK7
1.下载jdk-7-linux-x64.rpm(http://www.Oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-downlo ...
- vs2013+sql server2012
学习 vs2013 sql server2012使用 : linp lambda entity framework wcf tfs;
- iOS中解析 XML / JSON
JSON数据格式 1. 概述: JSON (JavaScript Object Notation) 是⼀一种轻量级的数据交换格式 基于⽂文本格式,易于⼈人阅读和编写,同时也易于机器解析和⽣生成. 2. ...
- JQuery 中的Ajax
JQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...
- sql查询语句心得
1)where 有多个用in ,一个用= 2)自身链接 select A.Sno from S A,S B where A.Sname='a' AND A.City=B.City)) 3)外链接(同时 ...
- 纯css实现slide效果
尝试一下,仅作记录. <!DOCTYPE html><html><head><style> *{transition:all 1s ease 0s ...
- CentOS+nginx+uwsgi+Python 多站点环境搭建
转载:http://www.cnblogs.com/xiongpq/p/3381069.html 环境: CentOS X64 6.5 nginx 1.5.6 Python 2.7.5 正文: 一:安 ...
- jquery 20行代码实现简单轮播效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...