File tree 2 files changed +48
-0
lines changed
2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ describe ( 'Trim values before search' , ( ) => {
2
+ beforeEach ( ( ) => {
3
+ cy . visit ( '/tests/index.html' ) ;
4
+ } ) ;
5
+
6
+ it ( 'Matches even if there are leading and trailing spaces' , ( ) => {
7
+ cy . selectpicker ( {
8
+ html : `
9
+ <select class="selectpicker" data-live-search="true">
10
+ ${ [
11
+ 'apple' ,
12
+ 'pear' ,
13
+ 'dragon fruit'
14
+ ] . map ( ( value ) =>
15
+ `<option value="${ value } ">${ value } </option>`
16
+ ) . join ( '' ) } }
17
+ </select>
18
+ `
19
+ } ) . then ( ( $select ) => {
20
+ const button = `[data-id="${ $select [ 0 ] . id } "]` ;
21
+ cy . get ( button ) . click ( ) ;
22
+ $select . on ( 'fetched.bs.select' , cy . stub ( ) . as ( 'fetched' ) ) ;
23
+ [ {
24
+ searchValue : ' app ' , // leading and trailing spaces
25
+ expectedContain : 'apple'
26
+ } , {
27
+ searchValue : ' pea' , // both half and full space mixed
28
+ expectedContain : 'pear'
29
+ } , {
30
+ searchValue : ' ' , // text with half-space also match
31
+ expectedContain : 'dragon fruit'
32
+ } ] . forEach ( ( test ) => {
33
+ cy . get ( 'input' ) . type ( test . searchValue ) ;
34
+ cy . get ( '.dropdown-menu' ) . find ( 'li' ) . first ( ) . should ( ( $el ) => {
35
+ expect ( $el ) . to . have . class ( 'active' )
36
+ expect ( $el ) . to . contain ( test . expectedContain ) ;
37
+ } ) ;
38
+ cy . get ( '.dropdown-menu' ) . find ( 'li' ) . first ( ) . click ( ) ;
39
+ cy . get ( button ) . contains ( test . expectedContain ) . click ( ) ;
40
+ } ) ;
41
+ } ) ;
42
+ } ) ;
43
+ } ) ;
Original file line number Diff line number Diff line change 3007
3007
3008
3008
this . $searchbox . on ( 'input propertychange' , function ( ) {
3009
3009
var searchValue = that . $searchbox [ 0 ] . value ;
3010
+ var isWhitespace = / ^ \s * $ / . test ( searchValue ) ;
3011
+ if ( ! isWhitespace ) {
3012
+ // trim leading and trailing half-width spaces and full-width spaces.
3013
+ searchValue = searchValue . replace ( / ^ \s + | \s + $ / g, '' ) ;
3014
+ }
3010
3015
3011
3016
that . selectpicker . search . elements = [ ] ;
3012
3017
that . selectpicker . search . data = [ ] ;
You can’t perform that action at this time.
0 commit comments