1
1
import React from 'react' ;
2
+ import sinon from 'sinon' ;
2
3
import { shallow } from 'enzyme' ;
3
4
4
5
import _ from 'react-bootstrap-table2/src/utils' ;
@@ -20,7 +21,11 @@ for (let i = 0; i < 20; i += 1) {
20
21
describe ( 'Wrapper' , ( ) => {
21
22
let wrapper ;
22
23
let instance ;
24
+ const onRemoteFilterChangeCB = sinon . stub ( ) ;
23
25
26
+ afterEach ( ( ) => {
27
+ onRemoteFilterChangeCB . reset ( ) ;
28
+ } ) ;
24
29
25
30
const createTableProps = ( ) => {
26
31
const tableProps = {
@@ -40,7 +45,8 @@ describe('Wrapper', () => {
40
45
data,
41
46
filter : filter ( ) ,
42
47
_,
43
- store : new Store ( 'id' )
48
+ store : new Store ( 'id' ) ,
49
+ onRemoteFilterChange : onRemoteFilterChangeCB
44
50
} ;
45
51
tableProps . store . data = data ;
46
52
return tableProps ;
@@ -84,13 +90,28 @@ describe('Wrapper', () => {
84
90
describe ( 'componentWillReceiveProps' , ( ) => {
85
91
let nextProps ;
86
92
87
- beforeEach ( ( ) => {
88
- nextProps = createTableProps ( ) ;
89
- instance . componentWillReceiveProps ( nextProps ) ;
93
+ describe ( 'when props.store.filters is same as current state.currFilters' , ( ) => {
94
+ beforeEach ( ( ) => {
95
+ nextProps = createTableProps ( ) ;
96
+ instance . componentWillReceiveProps ( nextProps ) ;
97
+ } ) ;
98
+
99
+ it ( 'should setting isDataChanged as false (Temporary solution)' , ( ) => {
100
+ expect ( instance . state . isDataChanged ) . toBeFalsy ( ) ;
101
+ } ) ;
90
102
} ) ;
91
103
92
- it ( 'should setting isDataChanged as false always(Temporary solution)' , ( ) => {
93
- expect ( instance . state . isDataChanged ) . toBeFalsy ( ) ;
104
+ describe ( 'when props.store.filters is different from current state.currFilters' , ( ) => {
105
+ beforeEach ( ( ) => {
106
+ nextProps = createTableProps ( ) ;
107
+ nextProps . store . filters = { price : { filterVal : 20 , filterType : FILTER_TYPE . TEXT } } ;
108
+ instance . componentWillReceiveProps ( nextProps ) ;
109
+ } ) ;
110
+
111
+ it ( 'should setting states correctly' , ( ) => {
112
+ expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
113
+ expect ( instance . state . currFilters ) . toBe ( nextProps . store . filters ) ;
114
+ } ) ;
94
115
} ) ;
95
116
} ) ;
96
117
@@ -126,7 +147,7 @@ describe('Wrapper', () => {
126
147
127
148
it ( 'should setting store object correctly' , ( ) => {
128
149
instance . onFilter ( props . columns [ 1 ] , filterVal , FILTER_TYPE . TEXT ) ;
129
- expect ( props . store . filtering ) . toBeTruthy ( ) ;
150
+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
130
151
} ) ;
131
152
132
153
it ( 'should setting state correctly' , ( ) => {
@@ -136,30 +157,54 @@ describe('Wrapper', () => {
136
157
} ) ;
137
158
} ) ;
138
159
160
+ describe ( 'when remote filter is enabled' , ( ) => {
161
+ const filterVal = '3' ;
162
+
163
+ beforeEach ( ( ) => {
164
+ props = createTableProps ( ) ;
165
+ props . remote = { filter : true } ;
166
+ createFilterWrapper ( props ) ;
167
+ instance . onFilter ( props . columns [ 1 ] , filterVal , FILTER_TYPE . TEXT ) ;
168
+ } ) ;
169
+
170
+ it ( 'should not setting store object correctly' , ( ) => {
171
+ expect ( props . store . filters ) . not . toEqual ( instance . state . currFilters ) ;
172
+ } ) ;
173
+
174
+ it ( 'should not setting state' , ( ) => {
175
+ expect ( instance . state . isDataChanged ) . toBeFalsy ( ) ;
176
+ expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 0 ) ;
177
+ } ) ;
178
+
179
+ it ( 'should calling props.onRemoteFilterChange correctly' , ( ) => {
180
+ expect ( onRemoteFilterChangeCB . calledOnce ) . toBeTruthy ( ) ;
181
+ } ) ;
182
+ } ) ;
183
+
139
184
describe ( 'combination' , ( ) => {
140
185
it ( 'should setting store object correctly' , ( ) => {
141
186
instance . onFilter ( props . columns [ 1 ] , '3' , FILTER_TYPE . TEXT ) ;
142
- expect ( props . store . filtering ) . toBeTruthy ( ) ;
187
+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
143
188
expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
144
189
expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 1 ) ;
145
190
146
191
instance . onFilter ( props . columns [ 1 ] , '2' , FILTER_TYPE . TEXT ) ;
147
- expect ( props . store . filtering ) . toBeTruthy ( ) ;
192
+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
148
193
expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
149
194
expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 1 ) ;
150
195
151
196
instance . onFilter ( props . columns [ 2 ] , '2' , FILTER_TYPE . TEXT ) ;
152
- expect ( props . store . filtering ) . toBeTruthy ( ) ;
197
+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
153
198
expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
154
199
expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 2 ) ;
155
200
156
201
instance . onFilter ( props . columns [ 2 ] , '' , FILTER_TYPE . TEXT ) ;
157
- expect ( props . store . filtering ) . toBeTruthy ( ) ;
202
+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
158
203
expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
159
204
expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 1 ) ;
160
205
161
206
instance . onFilter ( props . columns [ 1 ] , '' , FILTER_TYPE . TEXT ) ;
162
- expect ( props . store . filtering ) . toBeFalsy ( ) ;
207
+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
163
208
expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
164
209
expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 0 ) ;
165
210
} ) ;
0 commit comments