Parcourir la source

unit tests ok for HydraDenormalizer

Olivier Massot il y a 2 ans
Parent
commit
e18610f981

+ 1 - 1
services/data/imageManager.ts

@@ -43,7 +43,7 @@ class ImageManager {
         if (height > 0 || width > 0) {
             // @see https://thumbor.readthedocs.io/en/latest/crop_and_resize_algorithms.html
             // TODO: ajouter le support de ces options dans ap2i
-            // url = Url.join(url, `${height}x${width}`)
+            // url = UrlUtils.join(url, `${height}x${width}`)
         }
 
         // Une image doit toujours avoir le time en options pour éviter les problèmes de cache

+ 130 - 33
tests/units/services/data/serializer/denormalizer/hydraDenormalizer.spec.ts

@@ -1,10 +1,21 @@
-import { describe, test, it, expect } from 'vitest'
-import {AnyJson} from "~/types/data";
+import { describe, test, it, expect, afterEach, vi } from 'vitest'
+import {AnyJson, ApiResponse, HydraMetadata} from "~/types/data";
 import HydraDenormalizer from "~/services/data/serializer/denormalizer/hydraDenormalizer";
+import UrlUtils from "~/services/utils/urlUtils";
+import {METADATA_TYPE} from "~/types/enum/data";
+
+class TestableHydraDenormalizer extends HydraDenormalizer {
+    public static denormalize(hydraData: AnyJson): ApiResponse { return super.denormalize(hydraData) }
+    public static getData(hydraData: AnyJson): AnyJson { return super.getData(hydraData) }
+    public static getMetadata(data: AnyJson): HydraMetadata { return super.getMetadata(data) }
+}
+
+
 
 describe('denormalize', () => {
+
     test('should parse a API Item response and return a JSON Object', () => {
-        const serverResponse: AnyJson = {
+        const data: AnyJson = {
             '@context': '/api/contexts/Access',
             '@id': '/api/accesses/7351',
             '@type': 'Access',
@@ -15,22 +26,19 @@ describe('denormalize', () => {
                 id: 11344,
                 name: 'BRUEL',
                 givenName: 'Patrick'
-            },
-            'hydra:totalItems': 20,
-            'hydra:previous': '/api/organizations?page=1',
-            'hydra:next': '/api/organizations?page=2',
-            'hydra:itemPosition': 1
+            }
         }
 
-        expect(HydraDenormalizer.denormalize(serverResponse)).toStrictEqual<AnyJson>({
+        const result = HydraDenormalizer.denormalize(data)
+
+        expect(result.data).toEqual(TestableHydraDenormalizer.getData(data))
+        expect(result.metadata).toEqual(TestableHydraDenormalizer.getMetadata(data))
+
+        const expected = {
             "data": {
                 "@context": "/api/contexts/Access",
                 "@id": "/api/accesses/7351",
                 "@type": "Access",
-                "hydra:itemPosition": 1,
-                "hydra:next": "/api/organizations?page=2",
-                "hydra:previous": "/api/organizations?page=1",
-                "hydra:totalItems": 20,
                 "id": 7351,
                 "organization": "/api/organizations/37306",
                 "person": {
@@ -41,13 +49,15 @@ describe('denormalize', () => {
                 }
             },
             "metadata": {
-                "type": 0
+                "type": METADATA_TYPE.ITEM
             }
-        })
+        }
+
+        expect(result).toStrictEqual<AnyJson>(expected)
     })
 
     it('should parse a API Collection response and return a JSON Object', () => {
-        const serverResponse: AnyJson = {
+        const data: AnyJson = {
             '@context': '/api/contexts/Access',
             '@id': '/api/accesses',
             '@type': 'hydra:Collection',
@@ -73,24 +83,23 @@ describe('denormalize', () => {
                 }
             }
             ],
-            'hydra:search': {
-                'hydra:mapping': [
-                    {
-                        property: 'id',
-                        required: false,
-                        variable: 'filter[order][id]'
-                    },
-                    {
-                        property: 'id',
-                        required: false,
-                        variable: 'filter[where][id]'
-                    }
-                ]
+            "hydra:view": {
+                "@id": "/api/accesses?page=3",
+                "@type": "hydra:PartialCollectionView",
+                "hydra:first": "/api/accesses?page=1",
+                "hydra:last": "/api/accesses?page=5",
+                "hydra:next": "/api/accesses?page=4",
+                "hydra:previous": "/api/accesses?page=2"
             }
         }
 
-        const response = HydraDenormalizer.denormalize(serverResponse)
-        expect(JSON.stringify(response)).toEqual(JSON.stringify({"data":[
+        const result = HydraDenormalizer.denormalize(data)
+
+        expect(result.data).toEqual(TestableHydraDenormalizer.getData(data))
+        expect(result.metadata).toEqual(TestableHydraDenormalizer.getMetadata(data))
+
+        const expected = JSON.stringify(
+            {"data":[
                 {
                     '@id': '/api/accesses/7351',
                     organization: '/api/organizations/37306',
@@ -115,8 +124,96 @@ describe('denormalize', () => {
                             givenName: 'George'
                         }
                 }
-            ], 'metadata':{'type':1}}
-        ))
+            ],
+            'metadata': {
+                'firstPage': 1,
+                'lastPage': 5,
+                'nextPage': 4,
+                'previousPage': 2,
+                'type': 1
+            }
+        })
+
+        expect(JSON.stringify(result)).toEqual(expected)
     })
 })
 
+describe('getData', () => {
+    test('With collection', () => {
+        const data = {
+            "@context": "/api/contexts/Foo",
+            "@id": "/api/foo",
+            "@type": "hydra:Collection",
+            "hydra:member": [ 'foo' ],
+        }
+
+        expect(TestableHydraDenormalizer.getData(data)).toEqual([ 'foo' ])
+    })
+
+    test('With item', () => {
+        const data = {
+            "@context": "/api/contexts/Foo",
+            "@id": "/api/foo",
+            "@type": "Foo",
+            "param1": 'a',
+        }
+
+        expect(TestableHydraDenormalizer.getData(data)).toEqual(data)
+    })
+})
+
+describe('getMetadata', () => {
+    test('With valid collection metadata', () => {
+        const data = {
+            "@context": "/api/contexts/Foo",
+            "@id": "/api/foo",
+            "@type": "hydra:Collection",
+            "hydra:member": [ 'foo' ],
+            "hydra:totalItems": 10,
+            "hydra:view": {
+                "@id": "/api/foo?page=3",
+                "@type": "hydra:PartialCollectionView",
+                "hydra:first": "/api/foo?page=1",
+                "hydra:last": "/api/foo?page=5",
+                "hydra:next": "/api/foo?page=4",
+                "hydra:previous": "/api/foo?page=2"
+            }
+        }
+
+        const metadata = TestableHydraDenormalizer.getMetadata(data)
+
+        expect(metadata.totalItems).toEqual(10)
+        expect(metadata.firstPage).toEqual(1)
+        expect(metadata.lastPage).toEqual(5)
+        expect(metadata.nextPage).toEqual(4)
+        expect(metadata.previousPage).toEqual(2)
+        expect(metadata.type).toEqual(METADATA_TYPE.COLLECTION)
+    })
+    test('With partial collection metadata', () => {
+        const data = {
+            "@context": "/api/contexts/Foo",
+            "@id": "/api/foo",
+            "@type": "hydra:Collection",
+            "hydra:member": [ 'foo' ],
+            "hydra:totalItems": 10,
+            "hydra:view": {
+                "@id": "/api/foo?page=3",
+                "@type": "hydra:PartialCollectionView",
+                "hydra:first": "/api/foo?page=1",
+            }
+        }
+
+        const metadata = TestableHydraDenormalizer.getMetadata(data)
+
+        expect(metadata.totalItems).toEqual(10)
+        expect(metadata.firstPage).toEqual(1)
+        expect(metadata.lastPage).toEqual(1)
+        expect(metadata.nextPage).toEqual(undefined)
+        expect(metadata.previousPage).toEqual(undefined)
+    })
+
+    test('With item metadata', () => {
+        const metadata = TestableHydraDenormalizer.getMetadata({})
+        expect(metadata.type).toEqual(METADATA_TYPE.ITEM)
+    })
+})