[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 ...
随机推荐
- springMVC如何判断入参是默认参数还是请求传过来的参数?
springMVC如何判断入参是默认参数还是请求传过来的参数?
- Android 如何调用自写APK和非自写APK
由于项目需要,调用一个现成的APK,总结之余,顺便把怎么调用自写APK的方法也写上,以做比较 1.如何调用现成的APK: 先上调用代码,然后再一一解释: Intent mIntent = new In ...
- XFire构建服务端Service的两种方式
1.原声构建: 2.集成spring构建 http://blog.csdn.net/carefree31441/article/details/4000436XFire构建服务端Service的两种方 ...
- RTX管理器服务运行状态空白
A)打开RTX管理器安装目录下的bin文件夹,运行convert.bat 右键计算机——管理——服务——找到以RTX开头的服务,按RTX_ConfigCenter.RTX_HTTPServer. RT ...
- pod update或者pod install很慢
最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing dependencies不动 原因在于当执行以上两个命令的时候会升级Co ...
- UEFI模式下安装Win 7系统
转载自:http://huayi898.blog.163.com/blog/static/2581351620144442319155/ 下载win7_64bit原版官方系统 1.用软碟通制作U盘启动 ...
- codeforces 672 D
题目链接:http://codeforces.com/problemset/problem/672/D 题目大意:进行k次操作,每次将最大值集合中最大值-1,最小值+1,问你K次操作之后,最大值和最小 ...
- 写一个“标准”宏MIN,这个宏输入两个参数并返回较小的一个。
#define MIN(A,B) ((A)<=(B)?(A):(B))
- MySQL索引和锁
索引和锁可以让查询锁定更少的行.如果你的查询从不访问那些不需要访问的行,那么就会锁定更少的行,从两个方面来看这对性能都有好处.首先,虽然innodb的行锁效率很高,内存使用也很少,但是锁定行的时候仍然 ...
- HTML5学习参考资料整理
给大家推荐一下学习研究HTML5必备的一些个网站,更加有利于大家对HTML5的学些和研究.如果各位童鞋还有更多的,欢迎投递资源给我们,也可以支持 我们,让我们利用大家的力量收集更多的HTML5学习资料 ...