11import React from 'react' ;
2+ import sinon from 'sinon' ;
23import { shallow } from 'enzyme' ;
34
45import _ from 'react-bootstrap-table2/src/utils' ;
@@ -20,7 +21,11 @@ for (let i = 0; i < 20; i += 1) {
2021describe ( 'Wrapper' , ( ) => {
2122 let wrapper ;
2223 let instance ;
24+ const onRemoteFilterChangeCB = sinon . stub ( ) ;
2325
26+ afterEach ( ( ) => {
27+ onRemoteFilterChangeCB . reset ( ) ;
28+ } ) ;
2429
2530 const createTableProps = ( ) => {
2631 const tableProps = {
@@ -40,7 +45,8 @@ describe('Wrapper', () => {
4045 data,
4146 filter : filter ( ) ,
4247 _,
43- store : new Store ( 'id' )
48+ store : new Store ( 'id' ) ,
49+ onRemoteFilterChange : onRemoteFilterChangeCB
4450 } ;
4551 tableProps . store . data = data ;
4652 return tableProps ;
@@ -84,13 +90,28 @@ describe('Wrapper', () => {
8490 describe ( 'componentWillReceiveProps' , ( ) => {
8591 let nextProps ;
8692
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+ } ) ;
90102 } ) ;
91103
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+ } ) ;
94115 } ) ;
95116 } ) ;
96117
@@ -126,7 +147,7 @@ describe('Wrapper', () => {
126147
127148 it ( 'should setting store object correctly' , ( ) => {
128149 instance . onFilter ( props . columns [ 1 ] , filterVal , FILTER_TYPE . TEXT ) ;
129- expect ( props . store . filtering ) . toBeTruthy ( ) ;
150+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
130151 } ) ;
131152
132153 it ( 'should setting state correctly' , ( ) => {
@@ -136,30 +157,54 @@ describe('Wrapper', () => {
136157 } ) ;
137158 } ) ;
138159
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+
139184 describe ( 'combination' , ( ) => {
140185 it ( 'should setting store object correctly' , ( ) => {
141186 instance . onFilter ( props . columns [ 1 ] , '3' , FILTER_TYPE . TEXT ) ;
142- expect ( props . store . filtering ) . toBeTruthy ( ) ;
187+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
143188 expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
144189 expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 1 ) ;
145190
146191 instance . onFilter ( props . columns [ 1 ] , '2' , FILTER_TYPE . TEXT ) ;
147- expect ( props . store . filtering ) . toBeTruthy ( ) ;
192+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
148193 expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
149194 expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 1 ) ;
150195
151196 instance . onFilter ( props . columns [ 2 ] , '2' , FILTER_TYPE . TEXT ) ;
152- expect ( props . store . filtering ) . toBeTruthy ( ) ;
197+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
153198 expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
154199 expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 2 ) ;
155200
156201 instance . onFilter ( props . columns [ 2 ] , '' , FILTER_TYPE . TEXT ) ;
157- expect ( props . store . filtering ) . toBeTruthy ( ) ;
202+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
158203 expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
159204 expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 1 ) ;
160205
161206 instance . onFilter ( props . columns [ 1 ] , '' , FILTER_TYPE . TEXT ) ;
162- expect ( props . store . filtering ) . toBeFalsy ( ) ;
207+ expect ( props . store . filters ) . toEqual ( instance . state . currFilters ) ;
163208 expect ( instance . state . isDataChanged ) . toBeTruthy ( ) ;
164209 expect ( Object . keys ( instance . state . currFilters ) ) . toHaveLength ( 0 ) ;
165210 } ) ;
0 commit comments