@@ -111,47 +111,100 @@ describe('#fastApiTableAdapter', () => {
111111 } ) ;
112112
113113 describe ( '#next' , ( ) => {
114- beforeEach ( async ( ) => {
115- fetchResponse . contentRangeTotal = 1 ;
116- fetchResponse . items = [ 'foo' ] ;
114+ describe ( 'if there is a next page' , ( ) => {
115+ beforeEach ( async ( ) => {
116+ fetchResponse . contentRangeTotal = 11 ;
117+ fetchResponse . items = [ 'foo' ] ;
117118
118- result = await adapter . next ( ) ;
119- } ) ;
119+ await adapter . load ( ) ;
120+ result = await adapter . next ( ) ;
121+ } ) ;
120122
121- it ( 'increases the current page and performs a request' , ( ) => {
122- expect ( fetch ) . toHaveBeenCalledWith ( '/foo?limit=10&offset=10' ) ;
123+ it ( 'increases the current page and performs a request' , ( ) => {
124+ expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
125+ expect ( fetch ) . toHaveBeenCalledWith ( '/foo?limit=10&offset=10' ) ;
126+ } ) ;
127+
128+ it ( 'returns the result of calling load' , ( ) => {
129+ expect ( result ) . toEqual ( {
130+ page : 2 ,
131+ total : 11 ,
132+ items : [ 'foo' ] ,
133+ } ) ;
134+ } ) ;
123135 } ) ;
124136
125- it ( 'returns the result of calling load' , ( ) => {
126- expect ( result ) . toEqual ( {
127- page : 2 ,
128- total : 1 ,
129- items : [ 'foo' ] ,
137+ describe ( 'if there is NOT a next page' , ( ) => {
138+ beforeEach ( async ( ) => {
139+ fetchResponse . contentRangeTotal = 10 ;
140+ fetchResponse . items = [ 'foo' ] ;
141+
142+ await adapter . load ( ) ;
143+ result = await adapter . next ( ) ;
144+ } ) ;
145+
146+ it ( 'does not perform a request' , ( ) => {
147+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
148+ } ) ;
149+
150+ it ( 'returns the current state' , ( ) => {
151+ expect ( result ) . toEqual ( {
152+ page : 1 ,
153+ total : 10 ,
154+ items : [ 'foo' ] ,
155+ } ) ;
130156 } ) ;
131157 } ) ;
132158 } ) ;
133159
134160 describe ( '#previous' , ( ) => {
135- beforeEach ( async ( ) => {
136- fetchResponse . contentRangeTotal = 1 ;
137- fetchResponse . items = [ 'bar' ] ;
161+ describe ( 'if there is a previous page' , ( ) => {
162+ beforeEach ( async ( ) => {
163+ fetchResponse . contentRangeTotal = 33 ;
164+ fetchResponse . items = [ 'bar' ] ;
138165
139- // Increase page to 3
140- await adapter . next ( ) ;
141- await adapter . next ( ) ;
166+ // Increase page to 3
167+ await adapter . load ( ) ;
168+ await adapter . next ( ) ;
169+ await adapter . next ( ) ;
142170
143- result = await adapter . previous ( ) ;
144- } ) ;
171+ result = await adapter . previous ( ) ;
172+ } ) ;
145173
146- it ( 'decreases the current page and performs a request' , ( ) => {
147- expect ( fetch ) . toHaveBeenCalledWith ( '/foo?limit=10&offset=20' ) ;
174+ it ( 'decreases the current page and performs a request' , ( ) => {
175+ expect ( fetch ) . toHaveBeenCalledWith ( '/foo?limit=10&offset=20' ) ;
176+ } ) ;
177+
178+ it ( 'returns the result of calling load' , ( ) => {
179+ expect ( result ) . toEqual ( {
180+ page : 2 ,
181+ total : 33 ,
182+ items : [ 'bar' ] ,
183+ } ) ;
184+ } ) ;
148185 } ) ;
149186
150- it ( 'returns the result of calling load' , ( ) => {
151- expect ( result ) . toEqual ( {
152- page : 2 ,
153- total : 1 ,
154- items : [ 'bar' ] ,
187+ describe ( 'if there is NOT a previous page' , ( ) => {
188+ beforeEach ( async ( ) => {
189+ fetchResponse . contentRangeTotal = 33 ;
190+ fetchResponse . items = [ 'bar' ] ;
191+
192+ // Do not increase page
193+ await adapter . load ( ) ;
194+
195+ result = await adapter . previous ( ) ;
196+ } ) ;
197+
198+ it ( 'does not performs a request' , ( ) => {
199+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
200+ } ) ;
201+
202+ it ( 'returns the current state' , ( ) => {
203+ expect ( result ) . toEqual ( {
204+ page : 1 ,
205+ total : 33 ,
206+ items : [ 'bar' ] ,
207+ } ) ;
155208 } ) ;
156209 } ) ;
157210 } ) ;
0 commit comments