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