@@ -2,15 +2,19 @@ import Axios from 'axios'
22import BaseProxy from '../core/BaseProxy'
33import MockAdapter from 'axios-mock-adapter'
44import PostProxy from '../core/PostPorxy'
5+ import type { ValidatorType } from '../core/Validator'
6+ import Validator from '../core/Validator'
57import BaseTransformer from '../core/BaseTransformer'
68import PaginationTransformer from '../core/PaginationTransformer'
79import { merge } from '../util/objects'
810
911let proxy : PostProxy
1012let mockAdapter : MockAdapter
13+ let validator : ValidatorType
1114
1215describe ( 'BaseProxy' , ( ) => {
1316 beforeEach ( ( ) => {
17+ validator = Validator
1418 const axios = Axios . create ( { baseURL : 'https://mock-api.test' } )
1519 BaseProxy . $http = axios
1620 proxy = new PostProxy ( )
@@ -21,7 +25,7 @@ describe('BaseProxy', () => {
2125 it ( 'check if http was installed' , async ( ) => {
2226 BaseProxy . $http = undefined
2327 try {
24- await proxy . all ( )
28+ await proxy . getMany ( )
2529 } catch ( e ) {
2630 expect ( e . message ) . toBe ( 'Vue Api Queries, No http library provided.' )
2731 }
@@ -178,7 +182,7 @@ describe('BaseProxy', () => {
178182 it ( 'it should find an item by id' , async ( ) => {
179183 const item = { first_name : 'Chantouch' , last_name : 'Sek' , id : 1 }
180184 mockAdapter . onGet ( 'posts/1' ) . reply ( 200 , { data : item } )
181- const { data } = await proxy . find ( 1 )
185+ const { data } = await proxy . getOne ( 1 )
182186 expect ( data ) . toEqual ( item )
183187 } )
184188
@@ -198,7 +202,7 @@ describe('BaseProxy', () => {
198202 baz : new Date ( Date . UTC ( 2012 , 3 , 13 , 2 , 12 ) ) ,
199203 }
200204 form . field2 = file
201- form . files = [ { file } ]
205+ form . files = [ file ]
202206
203207 mockAdapter . onPost ( '/posts' ) . reply ( ( request ) => {
204208 expect ( request . data ) . toBeInstanceOf ( FormData )
@@ -207,20 +211,20 @@ describe('BaseProxy', () => {
207211 expect ( request . data . get ( 'field1[bar][1]' ) ) . toBe ( 'testBar2' )
208212 expect ( request . data . get ( 'field1[baz]' ) ) . toBe ( '2012-04-13T02:12:00.000Z' )
209213 expect ( request . data . get ( 'field2' ) ) . toEqual ( file )
210- expect ( request . data . get ( 'files[0][file] ' ) ) . toEqual ( file )
214+ expect ( request . data . get ( 'files[0]' ) ) . toEqual ( file )
211215
212216 expect ( getFormDataKeys ( request . data ) ) . toEqual ( [
213217 'field1[foo]' ,
214218 'field1[bar][0]' ,
215219 'field1[bar][1]' ,
216220 'field1[baz]' ,
217221 'field2' ,
218- 'files[0][file] ' ,
222+ 'files[0]' ,
219223 ] )
220224 return [ 200 , { } ]
221225 } )
222226
223- await proxy . post ( form )
227+ await proxy . create ( form )
224228 } )
225229
226230 it ( 'transforms the data to a FormData object if there is a File with post' , async ( ) => {
@@ -257,44 +261,47 @@ describe('BaseProxy', () => {
257261
258262 it ( 'transforms the boolean values in FormData object to "1" or "0"' , async ( ) => {
259263 const file = new File ( [ 'hello world!' ] , 'myfile' )
260- const form : any = { field1 : { } , field2 : null }
264+ const form : any = { field1 : { } , field2 : null , files : null }
261265 form . field1 = {
262266 foo : true ,
263267 bar : false ,
264268 }
265269 form . field2 = file
270+ form . files = [ file ]
271+ form . __proto__ = file
266272
267273 mockAdapter . onPost ( '/posts' ) . reply ( ( request ) => {
268274 expect ( request . data ) . toBeInstanceOf ( FormData )
269275 expect ( request . data . get ( 'field1[foo]' ) ) . toBe ( '1' )
270276 expect ( request . data . get ( 'field1[bar]' ) ) . toBe ( '0' )
271277 expect ( request . data . get ( 'field2' ) ) . toEqual ( file )
278+ expect ( request . data . get ( 'files[0]' ) ) . toEqual ( file )
279+ expect ( request . data . get ( '__proto__' ) ) . toEqual ( null )
272280
273281 expect ( getFormDataKeys ( request . data ) ) . toEqual ( [
274282 'field1[foo]' ,
275283 'field1[bar]' ,
276284 'field2' ,
285+ 'files[0]' ,
277286 ] )
278287 return [ 200 , { } ]
279288 } )
280289
281290 await proxy . post ( form )
282291 } )
283292
284- /*
285293 it ( 'it should throw errors message when data is not valid' , async ( ) => {
286294 const item = { first_name : null , last_name : 'Sek' , id : 1 }
287- mockAdapter.onPost('/posts').reply (422, {
295+ mockAdapter . onPost ( '/posts' ) . replyOnce ( 422 , {
288296 errors : { first_name : [ 'The first name field is required' ] } ,
289297 } )
290298 try {
291299 await proxy . post ( item )
292300 } catch ( e ) {
293- console.log('post:', e )
301+ expect ( e . message ) . toBe ( 'Request failed with status code 422' )
294302 }
295303 expect ( validator . has ( 'first_name' ) ) . toBeTruthy ( )
296304 } )
297- */
298305
299306 it ( 'it should store the item' , async ( ) => {
300307 const item = { first_name : 'Chantouch' , last_name : 'Sek' , id : 1 }
@@ -306,7 +313,7 @@ describe('BaseProxy', () => {
306313 it ( 'it should be able to put item' , async ( ) => {
307314 const item = { first_name : 'Chantouch' , last_name : 'Sek' , id : 1 }
308315 mockAdapter . onPut ( 'posts/1' ) . reply ( 200 , { data : item } )
309- const { data } = await proxy . put ( item . id , item )
316+ const { data } = await proxy . replace ( item . id , item )
310317 expect ( data ) . toEqual ( item )
311318 } )
312319
@@ -317,13 +324,27 @@ describe('BaseProxy', () => {
317324 expect ( data ) . toEqual ( item )
318325 } )
319326
320- it ( 'it should be able to patch item' , async ( ) => {
327+ it ( 'it should be able to update an item' , async ( ) => {
328+ const item = { first_name : 'Chantouch' , last_name : 'Sek' , id : 1 }
329+ mockAdapter . onPatch ( 'posts/1' ) . reply ( 200 , { data : item } )
330+ const { data } = await proxy . update ( item . id , item )
331+ expect ( data ) . toEqual ( item )
332+ } )
333+
334+ it ( 'it should be able to delete an item' , async ( ) => {
321335 const item = { first_name : 'Chantouch' , last_name : 'Sek' , id : 1 }
322336 mockAdapter . onDelete ( 'posts/1' ) . reply ( 200 , { data : item } )
323337 const { data } = await proxy . delete ( item . id )
324338 expect ( data ) . toEqual ( item )
325339 } )
326340
341+ it ( 'it should be able to remove an item' , async ( ) => {
342+ const item = { first_name : 'Chantouch' , last_name : 'Sek' , id : 1 }
343+ mockAdapter . onDelete ( 'posts/1' ) . reply ( 200 , { data : item } )
344+ const { data } = await proxy . remove ( item . id )
345+ expect ( data ) . toEqual ( item )
346+ } )
347+
327348 it ( 'can accept a custom http instance in options' , ( ) => {
328349 BaseProxy . $http = Axios . create ( { baseURL : 'https://another-example.com' } )
329350 expect ( proxy . $http . defaults . baseURL ) . toBe ( 'https://another-example.com' )
0 commit comments