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}}
    ))
  })
})
