hydraDenormalizer.spec.ts 4.1 KB

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