Skip to content

Commit

Permalink
Merge pull request #153 from n7best/master
Browse files Browse the repository at this point in the history
component tests
  • Loading branch information
n7best committed Nov 21, 2016
2 parents 1e75690 + 7404deb commit e84fc21
Show file tree
Hide file tree
Showing 15 changed files with 613 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Depreciate on next release

Known Issues

- React-Router Warnings from using older version of react-router
- Icon 'safe-success' and 'safe-warn' is not showing [fix pr](https://github.com/weui/weui/pull/528)

#### 0.4.0 (2016-04-28)
Expand Down
36 changes: 7 additions & 29 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,13 @@ import FastClick from 'fastclick';
import 'weui';
import "babel-polyfill";

import Home from './pages/home/index';
import Button from './pages/button/index';
import List from './pages/list/index';
import Input from './pages/input/index';
import Toast from './pages/toast/index';
import Dialog from './pages/dialog/index';
import Progress from './pages/progress/index';
import Msg from './pages/msg/index';
import Article from './pages/article/index';
import ActionSheet from './pages/actionsheet/index';
import Icons from './pages/icons/index';
import Panel from './pages/panel/index';
import NavBar from './pages/tab/navbar';
import NavBar2 from './pages/tab/navbar_auto';
import TabBar from './pages/tab/tabbar';
import TabBar2 from './pages/tab/tabbar_auto';
import SearchBar from './pages/searchbar/index';
import Gallery from './pages/gallery';
import Uploader from './pages/uploader';
import Flex from './pages/flex/index';
import Footer from './pages/footer';
import Grid from './pages/grid';
import LoadMore from './pages/loadmore';
import Preview from './pages/preview';
import MsgSuccess from './pages/msg/success';
import MsgFail from './pages/msg/fail';
import TopTips from './pages/toptips';
import Popup from './pages/popup';
import Picker from './pages/picker';
import Pages from './index';
const { Home, Button, List, Input, Toast, Dialog, Progress, Msg, Article,
ActionSheet, Icons, Panel, NavBar, NavBar2, TabBar, TabBar2, SearchBar, Gallery,
Uploader, Flex, Footer, Grid, LoadMore, Preview, MsgSuccess, MsgFail, TopTips,
Popup, Picker
} = Pages;


class App extends React.Component {
render() {
Expand Down
21 changes: 17 additions & 4 deletions src/components/actionsheet/actionsheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ class ActionSheet extends Component {
menus: [],
actions: [],
show: false,
autoDectect: true,
onRequestClose: () => {}
autoDectect: true
};

constructor(props) {
super(props);

this.state = {
isAndroid: isAndroid
}

this.handleMaskClick = this.handleMaskClick.bind(this)
}

renderMenuItem() {
return this.props.menus.map((menu, idx) => {
const {label, className, ...others} = menu;
Expand Down Expand Up @@ -76,6 +85,10 @@ class ActionSheet extends Component {
});
}

handleMaskClick(e){
if(this.props.onRequestClose) this.props.onRequestClose(e)
}

render() {
const {show, autoDectect, type, onRequestClose, menus, actions, ...others} = this.props;
const cls = classNames({
Expand All @@ -86,14 +99,14 @@ class ActionSheet extends Component {
let styleType = type ? type : 'ios';

if(!type && autoDectect){
if(isAndroid) styleType = 'android';
if(this.state.isAndroid) styleType = 'android';
}

return (
<div
className={styleType == 'android' ? 'weui-skin_android' : ''}
>
<Mask style={{display: show ? 'block' : 'none'}} onClick={onRequestClose} />
<Mask style={{display: show ? 'block' : 'none'}} onClick={this.handleMaskClick} />
<div className={cls} {...others} >
<div className="weui-actionsheet__menu">
{this.renderMenuItem()}
Expand Down
2 changes: 1 addition & 1 deletion src/components/msg/msg.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Msg extends Component {

let elFooter = footer ? footer : ()=>false;

if(!elFooter && (extraHref || extraText) ){
if(!elFooter() && (extraHref || extraText) ){
deprecationWarning('Msg extraHref/extraText', 'Msg footer')

elFooter = () => (
Expand Down
3 changes: 3 additions & 0 deletions src/utils/deprecationWarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import warning from 'warning';
const warned = {};

export default function deprecationWarning(oldname, newname, link) {
//avoid test warnings
if(typeof global.it === 'function') return;

const warnKey = `${oldname}\n${newname}`;
if (warned[warnKey]) {
return;
Expand Down
119 changes: 119 additions & 0 deletions test/actionsheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*global before*/
import React from 'react';
import { mount } from 'enzyme';
import sinon from 'sinon';
import assert from 'assert';
import WeUI from '../src/index';
var jsdom = require('jsdom');

const {ActionSheet, Mask} = WeUI;
const menus = [{
label: '拍照',
className: 'customClassName1'
}, {
label: '从相册中选取',
className: 'customClassName2'
}];
const actions = [{
label: '取消',
className: 'customClassName'
}, {
label: '确定'
}];

const androidUA = 'Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Build/KLP) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30';
const iosUA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25';

describe('<ActionSheet></ActionSheet>', ()=> {
[androidUA, iosUA].map(userAgent=>{
['ios', 'android', undefined].map(type => {
[undefined, null, true, false].map((show) => {
[null, sinon.spy()].map( onRequestClose => {
describe(`<ActionSheet menus='${menus}' actions='${actions}' show='${show}' onRequestClose='${onRequestClose}' type='${type}' />`, ()=> {

before(() => {
global.document = jsdom.jsdom('<!DOCTYPE html><html><head></head><body></body></html>', { userAgent: userAgent });
global.window = document.defaultView;
global.navigator = { userAgent: userAgent };
//console.log('test', window.navigator.userAgent)
});

const wrapper = mount(
<ActionSheet menus={menus} actions={actions} show={show} onRequestClose={onRequestClose} type={type}/>
);

const wrapperInstance = wrapper.instance();

it('should render ActionSheet', () => {
assert(wrapperInstance instanceof ActionSheet);
});

it(`should have a hidden <Mask></Mask> when 'show' prop is false or undefined`, ()=> {
const mask = wrapper.find(Mask)
if (show) {
assert(mask.prop('style').display === 'block');
}
else {
assert(mask.prop('style').display === 'none');
}
});

it(`should have 'onRequestClose' event on Mask when 'show' prop is true`, ()=> {
if (show && onRequestClose) {
wrapper.find(Mask).simulate('click');
assert(onRequestClose.calledOnce === true);
}
});

it(`should have 'weui_actionsheet_toggle' class name when 'show' prop is true`, ()=> {
const actionSheet = wrapper.find('.weui-actionsheet');
if (show) {
assert(actionSheet.hasClass(`weui-actionsheet_toggle`));
}
else {
assert(!actionSheet.hasClass(`weui-actionsheet_toggle`));
}
});

it(`should render ${menus.length} menu items `, ()=> {
const menuItems = wrapper.find('.weui-actionsheet__menu').find('.weui-actionsheet__cell');
assert(menuItems.length === menus.length);

menuItems.map((menuItem, index)=> {
const menu = menus[index];
assert(menuItem.text() === menu.label);
menu.className && assert(menuItem.hasClass(menu.className));
});
});

it(`should render ${actions.length} actions`, ()=> {
const actionItems = wrapper.find('.weui-actionsheet__action').find('.weui-actionsheet__cell');
assert(actionItems.length === actions.length);

actionItems.map((actionItem, index)=> {
const action = actions[index];
assert(actionItem.text() === action.label);
action.className && assert(actionItem.hasClass(action.className));
});
});

if(type == 'android'){
it('should have "weui-skin_android" class on main wrapper', ()=> {
assert(wrapper.find('div').first().hasClass('weui-skin_android'));
});
}

if(!type && userAgent == androidUA) {
wrapper.setState({ isAndroid: true })
it(`when no type define, should detect android and have "weui-skin_android" class on main wrapper`, ()=> {
assert(wrapper.find('div').first().hasClass('weui-skin_android'));
});
}

});
})
});
})
})

});
58 changes: 58 additions & 0 deletions test/article.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { shallow } from 'enzyme';
import assert from 'assert';
import WeUI from '../src/index';

const { Article } = WeUI;

describe('<ActionSheet></ActionSheet>', ()=> {
[undefined, null, '', 'custom_class'].map((clazz)=> {
describe('<Article className="${clazz}"></Article>', ()=> {
const content = (
<div>
<h1>大标题</h1>
<section>
<h2 className="title">章标题</h2>
<section>
<h3>1.1 节标题</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute</p>
</section>
<section>
<h3>1.2 节标题</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>
</section>
</div>
);
const wrapper = shallow(
<Article className={clazz}>
{content}
</Article>
);

it('should render <Article></Article> component', () => {
assert(wrapper.instance() instanceof Article);
});

it(`should have class name 'weui-article'`, ()=> {
assert(wrapper.hasClass(`weui-article`));
});

it(`should have custom class name ${clazz}`, ()=> {
if (clazz) {
assert(wrapper.hasClass(clazz));
}
});

it(`should have children`, ()=> {
assert(shallow(content).html() === wrapper.children().html());
});
});
})
});
28 changes: 27 additions & 1 deletion test/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@ import { shallow } from 'enzyme';
import assert from 'assert';
import WeUI from '../src/index';

const {Button} = WeUI;
const { Button, PreviewButton } = WeUI;

describe('<PreviewButton></PreviewButton>', () => {
[true, false].map(primary => {
describe(`<PreviewButton primary="${primary}">ok</PreviewButton>`, ()=>{
let wrapper = shallow(
<PreviewButton primary={primary}>ok</PreviewButton>
);

it('should have class with "weui-form-preview__btn"', ()=> {
assert(wrapper.hasClass('weui-form-preview__btn'));
});

it('should have class with "weui-form-preview__btn_default" when not primary', ()=> {
if (!primary) {
assert(wrapper.hasClass('weui-form-preview__btn_default'));
}
});

it('should have class with "weui-form-preview__btn_primary" when primary', ()=> {
if (primary) {
assert(wrapper.hasClass('weui-form-preview__btn_primary'));
}
});
})
})
})

describe('<Button></Button>', () => {

Expand Down
48 changes: 48 additions & 0 deletions test/button_area.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { shallow } from 'enzyme';
import assert from 'assert';
import WeUI from '../src/index';

const { ButtonArea, Button } = WeUI;

describe('<ButtonArea></ButtonArea>', () => {
[undefined, null, '', 'vertical', 'horizontal'].map((direction) => {
[undefined, null, '', 'custom_class'].map((className) => {
describe(`<ButtonArea className="${className} direction="${direction}></ButtonArea>`, () => {
const child = <Button>确定</Button>;
const wrapper = shallow(
<ButtonArea className={className} direction={direction}>
{child}
</ButtonArea>
);

it(`should render a <ButtonArea></ButtonArea> component`, () => {
assert(wrapper.instance() instanceof ButtonArea);
});

it(`should have 'weui-btn-area' class name`, () => {
assert(wrapper.hasClass(`weui-btn-area`));
});

it(`should have custom class name ${className}`, () => {
if (className) {
assert(wrapper.hasClass(`${className}`));
}
});

it(`should have 'weui-btn-area_inline' when direction equal 'horizontal'`, () => {
if (direction === 'horizontal') {
assert(wrapper.hasClass(`weui-btn-area_inline`));
}
else {
assert(!wrapper.hasClass(`weui-btn-area_inline`));
}
});

it(`should have children`, () => {
assert(wrapper.find(Button).html() === shallow(child).html());
});
});
});
});
});
Loading

0 comments on commit e84fc21

Please sign in to comment.