hydra.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import Hydra from '~/services/serializer/denormalizer/hydra'
  2. import { AnyJson } from '~/types/interfaces'
  3. import { DENORMALIZER_TYPE } from '~/types/enums'
  4. describe('support()', () => {
  5. it('should support model hydra type', () => {
  6. expect(Hydra.support(DENORMALIZER_TYPE.HYDRA)).toBeTruthy()
  7. })
  8. it('should not support yaml type', () => {
  9. expect(Hydra.support(DENORMALIZER_TYPE.YAML)).toBeFalsy()
  10. })
  11. })
  12. describe('denormalize()', () => {
  13. it('should parse a API Item response and return a JSON Object', () => {
  14. const serverResponse:AnyJson = {
  15. '@context': '/api/contexts/Access',
  16. '@id': '/api/accesses/7351',
  17. '@type': 'Access',
  18. organization: '/api/organizations/37306',
  19. id: 7351,
  20. person: {
  21. '@type': 'Person',
  22. id: 11344,
  23. name: 'BRUEL',
  24. givenName: 'Patrick'
  25. },
  26. 'hydra:totalItems': 20,
  27. 'hydra:previous': '/api/organizations?page=1',
  28. 'hydra:next': '/api/organizations?page=2',
  29. 'hydra:itemPosition': 1
  30. }
  31. expect(Hydra.denormalize(serverResponse)).toStrictEqual<AnyJson>({
  32. "data": {
  33. "@context": "/api/contexts/Access",
  34. "@id": "/api/accesses/7351",
  35. "@type": "Access",
  36. "hydra:itemPosition": 1,
  37. "hydra:next": "/api/organizations?page=2",
  38. "hydra:previous": "/api/organizations?page=1",
  39. "hydra:totalItems": 20,
  40. "id": 7351,
  41. "organization": "/api/organizations/37306",
  42. "person": {
  43. "@type": "Person",
  44. "givenName": "Patrick",
  45. "id": 11344,
  46. "name": "BRUEL"
  47. }
  48. },
  49. "metadata": {
  50. "type": 0
  51. }
  52. })
  53. })
  54. it('should parse a API Collection response and return a JSON Object', () => {
  55. const serverResponse:AnyJson = {
  56. '@context': '/api/contexts/Access',
  57. '@id': '/api/accesses',
  58. '@type': 'hydra:Collection',
  59. 'hydra:member': [{
  60. '@id': '/api/accesses/7351',
  61. organization: '/api/organizations/37306',
  62. id: 7351,
  63. person: {
  64. '@type': 'Person',
  65. id: 11344,
  66. name: 'BRUEL',
  67. givenName: 'Patrick'
  68. }
  69. }, {
  70. '@id': '/api/accesses/7352',
  71. organization: '/api/organizations/37306',
  72. id: 7352,
  73. person: {
  74. '@type': 'Person',
  75. id: 11345,
  76. name: 'BRASSENS',
  77. givenName: 'George'
  78. }
  79. }
  80. ],
  81. 'hydra:search': {
  82. 'hydra:mapping': [
  83. {
  84. property: 'id',
  85. required: false,
  86. variable: 'filter[order][id]'
  87. },
  88. {
  89. property: 'id',
  90. required: false,
  91. variable: 'filter[where][id]'
  92. }
  93. ]
  94. }
  95. }
  96. const response = Hydra.denormalize(serverResponse)
  97. expect(JSON.stringify(response)).toEqual(JSON.stringify({"data":[
  98. {
  99. '@id': '/api/accesses/7351',
  100. organization: '/api/organizations/37306',
  101. id: 7351,
  102. person:
  103. {
  104. '@type': 'Person',
  105. id: 11344,
  106. name: 'BRUEL',
  107. givenName: 'Patrick'
  108. }
  109. },
  110. {
  111. '@id': '/api/accesses/7352',
  112. organization: '/api/organizations/37306',
  113. id: 7352,
  114. person:
  115. {
  116. '@type': 'Person',
  117. id: 11345,
  118. name: 'BRASSENS',
  119. givenName: 'George'
  120. }
  121. }
  122. ], 'metadata':{'type':1}}
  123. ))
  124. })
  125. })