import Hydra from "~/services/serializer/denormalizer/hydra"; import {AnyJson} from "~/types/interfaces"; import {DENORMALIZER_TYPE} from "~/types/enums"; describe('support()', () => { it('should support model hydra type', () =>{ expect(Hydra.support(DENORMALIZER_TYPE.HYDRA)).toBeTruthy() }) it('should not support yaml type', () =>{ expect(Hydra.support(DENORMALIZER_TYPE.YAML)).toBeFalsy() }) }) describe('denormalize()', () => { it('should parse a API Item response and return a JSON Object', () => { let 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 } const hydra = new Hydra() expect(hydra.denormalize(serverResponse)).toStrictEqual({ "@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, "itemPosition": 1, "next": "organizations?page=2", "organization": "/api/organizations/37306", "person": { "@type": "Person", "givenName": "Patrick", "id": 11344, "name": "BRUEL" }, "previous": "organizations?page=1", "totalItems": 20 }); }); it('should parse a API Collection response and return a JSON Object', () => { let 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 hydra = new Hydra() const response = hydra.denormalize(serverResponse) expect(JSON.stringify(response)).toEqual(JSON.stringify([ { "@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" } } ] )) }); })