Skip to content

Commit fab1087

Browse files
committed
searchbar test
1 parent 6fc1ce2 commit fab1087

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Breaking Changes:
1515

1616
- Remove Dialog.Alert (use Dialog directly)
1717
- Remove Dialog.Confirm (use Dialog directly)
18+
- Icon (safe-warn, safe-success) are taking off from weui
1819

1920
New Components:
2021

@@ -64,7 +65,6 @@ Depreciate on next release
6465
Known Issues
6566

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

6969
#### 0.4.0 (2016-04-28)
7070

src/components/searchbar/searchbar.js

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ class SearchBar extends React.Component {
9494
}
9595
}
9696

97-
9897
submitHandle(e) {
9998
if (this.props.onSubmit) {
10099
e.preventDefault();

test/searchbar.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import React from 'react';
2+
import { mount, shallow } from 'enzyme';
3+
import assert from 'assert';
4+
import sinon from 'sinon';
5+
import WeUI from '../src/index';
6+
var jsdom = require('jsdom');
7+
8+
const { SearchBar } = WeUI;
9+
10+
describe('<SearchBar></SearchBar>', ()=>{
11+
before(() => {
12+
global.document = jsdom.jsdom('<!DOCTYPE html><html><head></head><body></body></html>');
13+
global.window = document.defaultView;
14+
});
15+
16+
it('should have related weui style', ()=>{
17+
const wrapper = shallow(<SearchBar />)
18+
19+
assert(wrapper.hasClass('weui-search-bar'))
20+
assert(wrapper.find('form').hasClass('weui-search-bar__form'))
21+
assert(wrapper.find('form').find('div').hasClass('weui-search-bar__box'))
22+
assert(wrapper.find('input').hasClass('weui-search-bar__input'))
23+
assert(wrapper.find('form').find('div').find('a').hasClass('weui-icon-clear'))
24+
assert(wrapper.find('label').hasClass('weui-search-bar__label'))
25+
assert(wrapper.find('a').last().hasClass('weui-search-bar__cancel-btn'))
26+
})
27+
28+
it('should trigger focus on focus', ()=>{
29+
const wrapper = mount(<SearchBar />)
30+
31+
assert(wrapper.find('.weui-search-bar_focusing').length == 0)
32+
wrapper.find('input').simulate('focus')
33+
assert(wrapper.find('.weui-search-bar_focusing').length > 0)
34+
})
35+
36+
it('should trigger onChange on input', ()=>{
37+
let value = ''
38+
const cb = text => value = text;
39+
const wrapper = mount( <SearchBar onChange={cb} />)
40+
41+
wrapper.find('input').simulate('focus')
42+
wrapper.find('input').simulate('change', { target: { value: 'Changed' } })
43+
44+
assert( value == 'Changed')
45+
})
46+
47+
it('should change focus state onBlur depends on text', ()=> {
48+
let value = ''
49+
const cb = text => value = text;
50+
const wrapper = mount( <SearchBar onChange={cb} />)
51+
52+
wrapper.find('input').simulate('focus')
53+
wrapper.find('input').simulate('change', { target: { value: 'Changed' } })
54+
wrapper.find('input').simulate('blur')
55+
56+
assert(wrapper.find('.weui-search-bar_focusing').length > 0)
57+
58+
wrapper.find('input').simulate('focus')
59+
wrapper.find('input').simulate('change', { target: { value: '' } })
60+
wrapper.find('input').simulate('blur')
61+
62+
assert(wrapper.find('.weui-search-bar_focusing').length == 0)
63+
})
64+
65+
it('should clear text and onClear, on clear', ()=> {
66+
let value = ''
67+
const cbClear = sinon.spy()
68+
const cb = text => value = text;
69+
const wrapper = mount( <SearchBar onChange={cb} onClear={cbClear} />)
70+
71+
wrapper.find('input').simulate('focus')
72+
wrapper.find('input').simulate('change', { target: { value: 'Changed' } })
73+
74+
assert( value == 'Changed')
75+
76+
wrapper.find('.weui-icon-clear').simulate('click')
77+
assert( value == '' )
78+
assert( cbClear.called )
79+
})
80+
81+
it('should clear text and focus and onCancel, on cancel', ()=> {
82+
let value = ''
83+
const cbCancel = sinon.spy()
84+
const cb = text => value = text;
85+
const wrapper = mount( <SearchBar onChange={cb} onCancel={cbCancel} />)
86+
87+
wrapper.find('input').simulate('focus')
88+
wrapper.find('input').simulate('change', { target: { value: 'Changed' } })
89+
90+
assert( value == 'Changed')
91+
92+
wrapper.find('.weui-search-bar__cancel-btn').simulate('click')
93+
assert( value == '' )
94+
assert( wrapper.find('.weui-search-bar_focusing').length == 0 )
95+
assert(cbCancel.called)
96+
})
97+
98+
it('should trigger onSubmit', ()=> {
99+
const cb = sinon.spy();
100+
const wrapper = mount( <SearchBar onSubmit={cb} />)
101+
102+
wrapper.find('input').simulate('focus')
103+
wrapper.find('input').simulate('change', { target: { value: 'Changed' } })
104+
wrapper.find('form').simulate('submit')
105+
106+
assert( cb.called )
107+
})
108+
109+
})

0 commit comments

Comments
 (0)