|
|
@@ -0,0 +1,122 @@
|
|
|
+import { describe, test, it, expect } from 'vitest'
|
|
|
+import {AnyJson} from "~/types/data";
|
|
|
+import HydraDenormalizer from "~/services/data/serializer/denormalizer/hydraDenormalizer";
|
|
|
+
|
|
|
+describe('denormalize', () => {
|
|
|
+ test('should parse a API Item response and return a JSON Object', () => {
|
|
|
+ const serverResponse: AnyJson = {
|
|
|
+ '@context': '/api/contexts/Access',
|
|
|
+ '@id': '/api/accesses/7351',
|
|
|
+ '@type': 'Access',
|
|
|
+ organization: '/api/organizations/37306',
|
|
|
+ id: 7351,
|
|
|
+ person: {
|
|
|
+ '@type': 'Person',
|
|
|
+ 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>({
|
|
|
+ "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": {
|
|
|
+ "@type": "Person",
|
|
|
+ "givenName": "Patrick",
|
|
|
+ "id": 11344,
|
|
|
+ "name": "BRUEL"
|
|
|
+ }
|
|
|
+ },
|
|
|
+ "metadata": {
|
|
|
+ "type": 0
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('should parse a API Collection response and return a JSON Object', () => {
|
|
|
+ const serverResponse: AnyJson = {
|
|
|
+ '@context': '/api/contexts/Access',
|
|
|
+ '@id': '/api/accesses',
|
|
|
+ '@type': 'hydra:Collection',
|
|
|
+ 'hydra:member': [{
|
|
|
+ '@id': '/api/accesses/7351',
|
|
|
+ organization: '/api/organizations/37306',
|
|
|
+ id: 7351,
|
|
|
+ person: {
|
|
|
+ '@type': 'Person',
|
|
|
+ id: 11344,
|
|
|
+ name: 'BRUEL',
|
|
|
+ givenName: 'Patrick'
|
|
|
+ }
|
|
|
+ }, {
|
|
|
+ '@id': '/api/accesses/7352',
|
|
|
+ organization: '/api/organizations/37306',
|
|
|
+ id: 7352,
|
|
|
+ person: {
|
|
|
+ '@type': 'Person',
|
|
|
+ id: 11345,
|
|
|
+ name: 'BRASSENS',
|
|
|
+ givenName: 'George'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ 'hydra:search': {
|
|
|
+ 'hydra:mapping': [
|
|
|
+ {
|
|
|
+ property: 'id',
|
|
|
+ required: false,
|
|
|
+ variable: 'filter[order][id]'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ property: 'id',
|
|
|
+ required: false,
|
|
|
+ variable: 'filter[where][id]'
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = HydraDenormalizer.denormalize(serverResponse)
|
|
|
+ expect(JSON.stringify(response)).toEqual(JSON.stringify({"data":[
|
|
|
+ {
|
|
|
+ '@id': '/api/accesses/7351',
|
|
|
+ organization: '/api/organizations/37306',
|
|
|
+ id: 7351,
|
|
|
+ person:
|
|
|
+ {
|
|
|
+ '@type': 'Person',
|
|
|
+ id: 11344,
|
|
|
+ name: 'BRUEL',
|
|
|
+ givenName: 'Patrick'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ '@id': '/api/accesses/7352',
|
|
|
+ organization: '/api/organizations/37306',
|
|
|
+ id: 7352,
|
|
|
+ person:
|
|
|
+ {
|
|
|
+ '@type': 'Person',
|
|
|
+ id: 11345,
|
|
|
+ name: 'BRASSENS',
|
|
|
+ givenName: 'George'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ], 'metadata':{'type':1}}
|
|
|
+ ))
|
|
|
+ })
|
|
|
+})
|
|
|
+
|