| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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', () => {
- 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(Hydra.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 = Hydra.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}}
- ))
- })
- })
|