hydra.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. '@context': '/api/contexts/Access',
  33. '@id': '/api/accesses/7351',
  34. '@type': 'Access',
  35. 'hydra:itemPosition': 1,
  36. 'hydra:next': '/api/organizations?page=2',
  37. 'hydra:previous': '/api/organizations?page=1',
  38. 'hydra:totalItems': 20,
  39. id: 7351,
  40. itemPosition: 1,
  41. next: 'organizations?page=2',
  42. organization: '/api/organizations/37306',
  43. person: {
  44. '@type': 'Person',
  45. givenName: 'Patrick',
  46. id: 11344,
  47. name: 'BRUEL'
  48. },
  49. previous: 'organizations?page=1',
  50. totalItems: 20
  51. })
  52. })
  53. it('should parse a API Collection response and return a JSON Object', () => {
  54. const serverResponse:AnyJson = {
  55. '@context': '/api/contexts/Access',
  56. '@id': '/api/accesses',
  57. '@type': 'hydra:Collection',
  58. 'hydra:member': [{
  59. '@id': '/api/accesses/7351',
  60. organization: '/api/organizations/37306',
  61. id: 7351,
  62. person: {
  63. '@type': 'Person',
  64. id: 11344,
  65. name: 'BRUEL',
  66. givenName: 'Patrick'
  67. }
  68. }, {
  69. '@id': '/api/accesses/7352',
  70. organization: '/api/organizations/37306',
  71. id: 7352,
  72. person: {
  73. '@type': 'Person',
  74. id: 11345,
  75. name: 'BRASSENS',
  76. givenName: 'George'
  77. }
  78. }
  79. ],
  80. 'hydra:search': {
  81. 'hydra:mapping': [
  82. {
  83. property: 'id',
  84. required: false,
  85. variable: 'filter[order][id]'
  86. },
  87. {
  88. property: 'id',
  89. required: false,
  90. variable: 'filter[where][id]'
  91. }
  92. ]
  93. }
  94. }
  95. const response = Hydra.denormalize(serverResponse)
  96. expect(JSON.stringify(response)).toEqual(JSON.stringify([
  97. {
  98. '@id': '/api/accesses/7351',
  99. organization: '/api/organizations/37306',
  100. id: 7351,
  101. person:
  102. {
  103. '@type': 'Person',
  104. id: 11344,
  105. name: 'BRUEL',
  106. givenName: 'Patrick'
  107. }
  108. },
  109. {
  110. '@id': '/api/accesses/7352',
  111. organization: '/api/organizations/37306',
  112. id: 7352,
  113. person:
  114. {
  115. '@type': 'Person',
  116. id: 11345,
  117. name: 'BRASSENS',
  118. givenName: 'George'
  119. }
  120. }
  121. ]
  122. ))
  123. })
  124. })