Vincent GUFFON 4 лет назад
Родитель
Сommit
ab11728b2e

+ 2 - 1
tests/unit/services/connection/connection.spec.ts

@@ -39,7 +39,8 @@ describe('invoke()', () => {
           'method': HTTP_METHOD.GET,
           'progress': false,
           'responseType': 'blob',
-          'url': 'files/1/download'
+          'url': 'files/1/download',
+          'params': {}
         }
       )
     })

+ 1 - 1
tests/unit/services/connection/urlBuilder.spec.ts

@@ -17,7 +17,7 @@ describe('invoke()', () => {
       expect(UrlBuilder.build({
         type: QUERY_TYPE.DEFAULT,
         url: 'users'
-      })).toEqual('/api/users')
+      })).toEqual('users')
     })
   })
 

+ 7 - 1
tests/unit/services/connection/urlOptionsBuilder.spec.ts

@@ -25,11 +25,17 @@ describe('build()', () => {
       type: QUERY_TYPE.MODEL,
       listArgs: {
         itemsPerPage:10,
-        page:1
+        page:1,
+        filters:[
+          {key: 'name', value: 'Foo'},
+          {key: 'givenName', value: 'Bar'}
+        ]
       }
     }
 
     const options = UrlOptionsBuilder.build(args)
+    expect(options.pop()).toEqual('givenName=Bar')
+    expect(options.pop()).toEqual('name=Foo')
     expect(options.pop()).toEqual('page=1')
     expect(options.pop()).toEqual('itemsPerPage=10')
   })

+ 48 - 2
tests/unit/services/serializer/normalizer/model.spec.ts

@@ -1,13 +1,17 @@
 import Model from '~/services/serializer/normalizer/model'
-import { DataPersisterArgs } from '~/types/interfaces'
+import {AnyJson, DataPersisterArgs} from '~/types/interfaces'
 import { QUERY_TYPE } from '~/types/enums'
 import { repositoryHelper } from '~/services/store/repository'
 import User from '~/tests/unit/fixture/models/User'
 import { createStore } from '~/tests/unit/Helpers'
+import {queryHelper} from "~/services/store/query";
 
 jest.mock('~/services/store/repository')
 const repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
 
+jest.mock('~/services/store/query')
+const queryHelperMock = queryHelper as jest.Mocked<typeof queryHelper>
+
 describe('support()', () => {
   it('should support model query type', () => {
     expect(Model.support(QUERY_TYPE.MODEL)).toBeTruthy()
@@ -39,7 +43,7 @@ describe('normalize()', () => {
     expect(() => Model.normalize(args)).toThrowError('Item not found')
   })
 
-  it('should normalize model to JSON', async () => {
+  it('should find an item thanks to a repository, and normalize it to JSON', async () => {
     const store = createStore()
     const user = store.$repo(User).make()
     repositoryHelperMock.findItemFromModel = jest.fn().mockReturnValue(user)
@@ -51,4 +55,46 @@ describe('normalize()', () => {
     }
     expect(Model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
   })
+
+  it('should find an item thanks to a query, and normalize it to JSON', async () => {
+    const store = createStore()
+    const user = store.$repo(User).make()
+    const query = store.$repo(User).query()
+    queryHelperMock.getItem = jest.fn().mockReturnValue(user)
+
+    const args:DataPersisterArgs = {
+      type: QUERY_TYPE.MODEL,
+      model: User,
+      id: 1,
+      query: query
+    }
+    expect(Model.normalize(args)).toStrictEqual({ id: 1, name: 'John Doe' })
+  })
+})
+
+describe('isPostQuery()', () => {
+  it('should return true if args has got a temp ID', () => {
+    const args:DataPersisterArgs = {
+      type: QUERY_TYPE.MODEL,
+      idTemp: 1
+    }
+    expect(Model.isPostQuery(args)).toBeTruthy()
+  })
+
+  it('should return false if args has not a temp ID', () => {
+    const args:DataPersisterArgs = {
+      type: QUERY_TYPE.MODEL
+    }
+    expect(Model.isPostQuery(args)).toBeFalsy()
+  })
+})
+
+describe('sanitizeBeforePost()', () => {
+  it('should return data without id', () => {
+    const data:AnyJson = {
+      id: 1,
+      foo: 'bar'
+    }
+    expect(Model.sanitizeBeforePost(data)).toEqual({foo:'bar'})
+  })
 })

+ 1 - 1
tests/unit/services/store/repository.spec.ts

@@ -84,7 +84,7 @@ describe('cleanRepository()', () => {
   it('should clean the entire repository', () => {
     const user = repository.make()
     repository.save(user)
-    repositoryHelper.cleanRepository(repository)
+    repositoryHelper.cleanRepository(User)
     expect(repository.all()).toEqual([])
   })
 })

+ 15 - 0
tests/unit/services/utils/modelsUtils.spec.ts

@@ -0,0 +1,15 @@
+import ModelsUtils from "~/services/utils/modelsUtils";
+
+describe('extractIdFromUri()', () => {
+  it('should return null if uri is empty', () => {
+    expect(ModelsUtils.extractIdFromUri('')).toBeNull()
+  })
+
+  it('should return Error if uri is not an number', () => {
+    expect(() => ModelsUtils.extractIdFromUri('/api/person/id')).toThrow()
+  })
+
+  it('should return the id s uri', () => {
+    expect(ModelsUtils.extractIdFromUri('/api/person/2')).toEqual(2)
+  })
+})