[React Unit Testing] React unit testing demo
import React from 'react'
const Release = React.createClass({
render() {
const { title, artist, outOfPrint } = this.props.release;
const className = outOfPrint? 'release outOfPrint' : 'release';
return (
<tr className={className} >
<td className="artist">{ artist }</td>
<td className="title">{ title }</td>
<td className="comment">{ outOfPrint ? <span style={{color: 'red', fontStyle: 'italic'}}>Out Of Print!</span> : null }</td>
</tr>
);
}
});
const ReleaseTable = React.createClass({
render() {
const { searchText, releases, noOutOfPrint } = this.props;
const rows = releases.filter((release) => {
return (release.title.indexOf(searchText) !== -1
|| release.artist.indexOf(searchText) !== -1)
&& !(release.outOfPrint && noOutOfPrint);
});
return (
<table className="releaseTable">
<thead>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Comment</th>
</tr>
</thead>
<tbody>{rows.map(release => {
return <Release release={ release }
key={ release.title } />
})}</tbody>
</table>
);
}
});
const SearchBar = React.createClass({
updateSearch() {
this.props.onSearch(
this.refs.search.value,
this.refs.noOutOfPrint.checked
);
},
render() {
return (
<form className="searchBar">
<input
type="text"
placeholder="Search..."
value={this.props.searchText}
ref="search"
onChange={this.updateSearch}
id="searchFilter"
/>
<p>
<input
type="checkbox"
checked={this.props.noOutOfPrint}
ref="noOutOfPrint"
onChange={this.updateSearch}
id="noOutOfPrint"
/>
{' '}
Only show available releases
</p>
</form>
);
}
});
const Root = React.createClass({
getInitialState() {
return {
searchText: '',
noOutOfPrint: false
};
},
updateSearch: function (searchText, noOutOfPrint) {
this.setState({ searchText, noOutOfPrint });
},
render() {
return (
<div className="main">
<SearchBar
searchText={this.state.searchText}
inStockOnly={this.state.noOutOfPrint}
onSearch={this.updateSearch}
/>
<ReleaseTable
releases={this.props.releases}
searchText={this.state.searchText}
noOutOfPrint={this.state.noOutOfPrint}
/>
</div>
);
}
});
export {
Release,
ReleaseTable,
SearchBar
};
export default Root;
import React from 'react'
import { shallow, mount, render } from 'enzyme'
import Root, { SearchBar, ReleaseTable, Release } from '../src/Root' const createShallowRelease = (outOfPrint = true) => {
let props = {release: { artist: 'foobar', title: 'bar', outOfPrint }};
return shallow(<Release {...props} />);
}; const createShallowRelaseTable = (noOutOfPrint = false, searchText = '') => {
let items = [{ artist: 'foobar', title: 'bar', outOfPrint: true }];
let props = { searchText, releases: items, noOutOfPrint };
return shallow(<ReleaseTable { ...props } />);
} describe('<SearchBar>', () => { let onSearch;
beforeEach(() => {
onSearch = jasmine.createSpy('onSearch');
}); it('calls onSearch when search text changes', () => {
let props = { searchText: '', noOutOfPrint: false, onSearch };
let search = mount(<SearchBar { ...props } />);
let input = search.find('#searchFilter');
input.get(0).value = 'foobar';
input.simulate('change');
expect(onSearch).toHaveBeenCalledWith('foobar', false);
}); it('calls onSearch when no out print is checked', () => {
let props = { searchText: '', noOutOfPrint: false, onSearch };
let search = mount(<SearchBar { ...props } />);
let input = search.find('#noOutOfPrint');
input.get(0).checked = true;
input.simulate('change', {target: { checked: true }});
expect(onSearch).toHaveBeenCalledWith('', true);
}); }); describe('<ReleaseTable>', () => {
it('contains the class name releaseTable', () => {
let release = createShallowRelaseTable();
expect(release.is('.releaseTable')).toBeTruthy();
}); it('renders release item', () => {
let release = createShallowRelaseTable();
expect(release.find(Release).length).toBe(1);
}); it('filters out any out of print items', () => {
let release = createShallowRelaseTable(true, '');
expect(release.find(Release).length).toBe(0);
}); it('filters out any out of items when filtering by search text', () => {
let release = createShallowRelaseTable(false, 'bla');
expect(release.find(Release).length).toBe(0);
}); }); describe('<Release>', () => {
it('contains the class name release', () => {
let release = createShallowRelease();
expect(release.is('.release')).toBeTruthy();
}); it ('contains the class name outOfPrint if out of print', () => {
let release = createShallowRelease(true);
expect(release.is('.outOfPrint')).toBeTruthy();
}); it ('does not contain the class name outOfPrint if available', () => {
let release = createShallowRelease(false);
expect(release.is('.outOfPrint')).toBeFalsy();
}); it ('renders the artist name', () => {
let release = createShallowRelease();
expect(release.find('.artist').text()).toEqual('foobar');
}); it ('renders the release title', () => {
let release = createShallowRelease();
expect(release.find('.title').text()).toEqual('bar');
}); it ('renders the correct comment', () => {
let release = createShallowRelease(true);
expect(release.find('.comment').text()).toEqual('Out Of Print!');
}); });
{
"name": "example-karma-jasmine-webapck-test-setup",
"description": "React Test Setup with Karma/Jasmine/Webpack",
"scripts": {
"test": "karma start --single-run --browsers PhantomJS"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-core": "^6.5.2",
"babel-eslint": "^5.0.0",
"babel-loader": "^6.2.3",
"babel-preset-airbnb": "^1.1.1",
"babel-preset-es2015": "^6.5.0",
"babel-preset-react": "^6.5.0",
"enzyme": "^2.0.0",
"jasmine-core": "^2.4.1",
"json-loader": "^0.5.4",
"karma": "^0.13.21",
"karma-babel-preprocessor": "^6.0.1",
"karma-jasmine": "^0.3.7",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.7.0",
"lodash": "^4.5.1",
"phantomjs-prebuilt": "^2.1.4",
"react": "^0.14.7",
"react-addons-test-utils": "^0.14.7",
"react-dom": "^0.14.7",
"react-test-utils": "0.0.1",
"webpack": "^1.12.14"
}
}
[React Unit Testing] React unit testing demo的更多相关文章
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
- Unit Testing, Integration Testing and Functional Testing
转载自:https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional- ...
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
- 【温故知新】—— React/Redux/React-router4基础知识&独立团Demo
前言:React专注View层,一切皆组件:全部使用ES6语法,最新版本为React16. Redux是专注于状态管理的库,和react解耦:单一状态,单向数据流.[独立团github地址] 一.Re ...
- 【React入门】React父子组件传值demo
公司一直是前后端分离的,最近集团开始推进中后台可视化开发组件(基于React封装),跟师兄聊起来也听说最近对后台开发人员的前端能力也是越来越重视了.所以作为一名后端,了解下前端的框架对自己也是大有好处 ...
- 【react】关于react框架使用的一些细节要点的思考
( _(:3 」∠)_给园友们提个建议,无论是API文档还是书籍,一定要多看几遍!特别是隔一段时间后,会有意想不到的收获的) 这篇文章主要是写关于学习react中的一些自己的思考: 1.set ...
- 【React Native】React Native项目设计与知识点分享
闲暇之余,写了一个React Native的demo,可以作为大家的入门学习参考. GitHub:https://github.com/xujianfu/ElmApp.git GitHub:https ...
- Difference Between Performance Testing, Load Testing and Stress Testing
http://www.softwaretestinghelp.com/what-is-performance-testing-load-testing-stress-testing/ Differen ...
- react系列从零开始-react介绍
react算是目前最火的js MVC框架了,写一个react系列的博客,顺便回忆一下react的基础知识,新入门前端的小白,可以持续关注,我会从零开始教大家用react开发一个完整的项目,也会涉及到w ...
随机推荐
- Spring中事务的XML方式[声明方式]
事务管理: 管理事务,管理数据,数据完整性和一致性 事务[业务逻辑] : 由一系列的动作[查询书价格,更新库存,更新余额],组成一个单元[买书业务], 当我们动作当中有一个错了,全错~ ACID 原子 ...
- Day4晚笔记
数据结构 并查集:捆绑两个点的信息,判断对错 倍增:LCA, 字符串 hash,模拟, 最小表示法 给定一个环状字符串,切开,使得字符串的字典序最小 图和树 割点,割边,强联通分量 点双联通分量 (把 ...
- Spring MVC modelandview
一开始${Name} 不能显示 原来是: import org.springframework.web.portlet.ModelAndView; --错误的引入 import org.springf ...
- Eclipse使用方法和技巧二十七:定义自己的高速联想词
某天在调试代码的时候.尽管是android的project还是习惯的输入syso.然后在ALT+/一下. 旁边的同事就问了一下,这个log打印输出的tag是什么. 接着又问了为什么syso可以智能联想 ...
- cocos2d-x认识之旅
cocos2d-x 学习历程 1. 了解cocos2d-x.官网 : www.cocos2d-x.org 2. 搭建cocos2d-x. 使用版本号cocos2d-x 3.0 搭建好开发环境教程:ht ...
- CentOS6.X安装10G需要额外安装的软件包
yum -y install libXp yum -y install libXp.i686 yum -y install libXtst.i686
- Vue 使用use、prototype自定义自己的全局组件
使用Vue.use()写一个自己的全局组件. 目录如下: 然后在Loading.vue里面定义自己的组件模板 <template> <div v-if="loadFlag& ...
- 最大似然 vs. 最小二乘
有一篇是比较最大似然估计和最小二乘法的: http://www.cnblogs.com/hxsyl/p/5590358.html 最大似然估计:现在已经拿到了很多个样本(你的数据集中所有因变量),这些 ...
- Altium Designer中的粉红色网格和绿色框框
- 芯片TPS70925
TPS70925电源芯片 从上图中可以看出EN脚是使能脚,并且是高使能,低失能. tps70925的典型用法: 这个芯片有很多封装,我们用的是第一个: