hydraDenormalizer.spec.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { describe, test, it, expect, afterEach, vi } from 'vitest'
  2. import {AnyJson, ApiResponse, HydraMetadata} from "~/types/data";
  3. import HydraDenormalizer from "~/services/data/serializer/denormalizer/hydraDenormalizer";
  4. import UrlUtils from "~/services/utils/urlUtils";
  5. import {METADATA_TYPE} from "~/types/enum/data";
  6. class TestableHydraDenormalizer extends HydraDenormalizer {
  7. public static denormalize(hydraData: AnyJson): ApiResponse { return super.denormalize(hydraData) }
  8. public static getData(hydraData: AnyJson): AnyJson { return super.getData(hydraData) }
  9. public static getMetadata(data: AnyJson): HydraMetadata { return super.getMetadata(data) }
  10. }
  11. describe('denormalize', () => {
  12. test('should parse a API Item response and return a JSON Object', () => {
  13. const data: AnyJson = {
  14. '@context': '/api/contexts/Access',
  15. '@id': '/api/accesses/7351',
  16. '@type': 'Access',
  17. organization: '/api/organizations/37306',
  18. id: 7351,
  19. person: {
  20. '@type': 'Person',
  21. id: 11344,
  22. name: 'BRUEL',
  23. givenName: 'Patrick'
  24. }
  25. }
  26. const result = HydraDenormalizer.denormalize(data)
  27. expect(result.data).toEqual(TestableHydraDenormalizer.getData(data))
  28. expect(result.metadata).toEqual(TestableHydraDenormalizer.getMetadata(data))
  29. const expected = {
  30. "data": {
  31. "@context": "/api/contexts/Access",
  32. "@id": "/api/accesses/7351",
  33. "@type": "Access",
  34. "id": 7351,
  35. "organization": "/api/organizations/37306",
  36. "person": {
  37. "@type": "Person",
  38. "givenName": "Patrick",
  39. "id": 11344,
  40. "name": "BRUEL"
  41. }
  42. },
  43. "metadata": {
  44. "type": METADATA_TYPE.ITEM
  45. }
  46. }
  47. expect(result).toStrictEqual<AnyJson>(expected)
  48. })
  49. it('should parse a API Collection response and return a JSON Object', () => {
  50. const data: AnyJson = {
  51. '@context': '/api/contexts/Access',
  52. '@id': '/api/accesses',
  53. '@type': 'hydra:Collection',
  54. 'hydra:member': [{
  55. '@id': '/api/accesses/7351',
  56. organization: '/api/organizations/37306',
  57. id: 7351,
  58. person: {
  59. '@type': 'Person',
  60. id: 11344,
  61. name: 'BRUEL',
  62. givenName: 'Patrick'
  63. }
  64. }, {
  65. '@id': '/api/accesses/7352',
  66. organization: '/api/organizations/37306',
  67. id: 7352,
  68. person: {
  69. '@type': 'Person',
  70. id: 11345,
  71. name: 'BRASSENS',
  72. givenName: 'George'
  73. }
  74. }
  75. ],
  76. "hydra:view": {
  77. "@id": "/api/accesses?page=3",
  78. "@type": "hydra:PartialCollectionView",
  79. "hydra:first": "/api/accesses?page=1",
  80. "hydra:last": "/api/accesses?page=5",
  81. "hydra:next": "/api/accesses?page=4",
  82. "hydra:previous": "/api/accesses?page=2"
  83. }
  84. }
  85. const result = HydraDenormalizer.denormalize(data)
  86. expect(result.data).toEqual(TestableHydraDenormalizer.getData(data))
  87. expect(result.metadata).toEqual(TestableHydraDenormalizer.getMetadata(data))
  88. const expected = JSON.stringify(
  89. {"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. ],
  115. 'metadata': {
  116. 'firstPage': 1,
  117. 'lastPage': 5,
  118. 'nextPage': 4,
  119. 'previousPage': 2,
  120. 'type': 1
  121. }
  122. })
  123. expect(JSON.stringify(result)).toEqual(expected)
  124. })
  125. })
  126. describe('getData', () => {
  127. test('With collection', () => {
  128. const data = {
  129. "@context": "/api/contexts/Foo",
  130. "@id": "/api/foo",
  131. "@type": "hydra:Collection",
  132. "hydra:member": [ 'foo' ],
  133. }
  134. expect(TestableHydraDenormalizer.getData(data)).toEqual([ 'foo' ])
  135. })
  136. test('With item', () => {
  137. const data = {
  138. "@context": "/api/contexts/Foo",
  139. "@id": "/api/foo",
  140. "@type": "Foo",
  141. "param1": 'a',
  142. }
  143. expect(TestableHydraDenormalizer.getData(data)).toEqual(data)
  144. })
  145. })
  146. describe('getMetadata', () => {
  147. test('With valid collection metadata', () => {
  148. const data = {
  149. "@context": "/api/contexts/Foo",
  150. "@id": "/api/foo",
  151. "@type": "hydra:Collection",
  152. "hydra:member": [ 'foo' ],
  153. "hydra:totalItems": 10,
  154. "hydra:view": {
  155. "@id": "/api/foo?page=3",
  156. "@type": "hydra:PartialCollectionView",
  157. "hydra:first": "/api/foo?page=1",
  158. "hydra:last": "/api/foo?page=5",
  159. "hydra:next": "/api/foo?page=4",
  160. "hydra:previous": "/api/foo?page=2"
  161. }
  162. }
  163. const metadata = TestableHydraDenormalizer.getMetadata(data)
  164. expect(metadata.totalItems).toEqual(10)
  165. expect(metadata.firstPage).toEqual(1)
  166. expect(metadata.lastPage).toEqual(5)
  167. expect(metadata.nextPage).toEqual(4)
  168. expect(metadata.previousPage).toEqual(2)
  169. expect(metadata.type).toEqual(METADATA_TYPE.COLLECTION)
  170. })
  171. test('With partial collection metadata', () => {
  172. const data = {
  173. "@context": "/api/contexts/Foo",
  174. "@id": "/api/foo",
  175. "@type": "hydra:Collection",
  176. "hydra:member": [ 'foo' ],
  177. "hydra:totalItems": 10,
  178. "hydra:view": {
  179. "@id": "/api/foo?page=3",
  180. "@type": "hydra:PartialCollectionView",
  181. "hydra:first": "/api/foo?page=1",
  182. }
  183. }
  184. const metadata = TestableHydraDenormalizer.getMetadata(data)
  185. expect(metadata.totalItems).toEqual(10)
  186. expect(metadata.firstPage).toEqual(1)
  187. expect(metadata.lastPage).toEqual(1)
  188. expect(metadata.nextPage).toEqual(undefined)
  189. expect(metadata.previousPage).toEqual(undefined)
  190. })
  191. test('With item metadata', () => {
  192. const metadata = TestableHydraDenormalizer.getMetadata({})
  193. expect(metadata.type).toEqual(METADATA_TYPE.ITEM)
  194. })
  195. })