hydra.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. let 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. const hydra = new Hydra()
  32. expect(hydra.denormalize(serverResponse)).toStrictEqual<AnyJson>({
  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. "itemPosition": 1,
  42. "next": "organizations?page=2",
  43. "organization": "/api/organizations/37306",
  44. "person": {
  45. "@type": "Person",
  46. "givenName": "Patrick",
  47. "id": 11344,
  48. "name": "BRUEL"
  49. },
  50. "previous": "organizations?page=1",
  51. "totalItems": 20
  52. });
  53. });
  54. it('should parse a API Collection response and return a JSON Object', () => {
  55. let 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 hydra = new Hydra()
  97. const response = hydra.denormalize(serverResponse)
  98. expect(JSON.stringify(response)).toEqual(JSON.stringify([
  99. {
  100. "@id": "/api/accesses/7351",
  101. "organization":"/api/organizations/37306",
  102. "id":7351,
  103. "person":
  104. {
  105. "@type":"Person",
  106. "id":11344,
  107. "name":"BRUEL",
  108. "givenName":"Patrick"
  109. }
  110. },
  111. {
  112. "@id": "/api/accesses/7352",
  113. "organization":"/api/organizations/37306",
  114. "id":7352,
  115. "person":
  116. {
  117. "@type":"Person",
  118. "id":11345,
  119. "name":"BRASSENS",
  120. "givenName":"George"
  121. }
  122. }
  123. ]
  124. ))
  125. });
  126. })