hydraDenormalizer.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { describe, test, it, expect } from 'vitest'
  2. import {AnyJson, ApiResponse, HydraMetadata} from "~/types/data";
  3. import HydraDenormalizer from "~/services/data/normalizer/hydraDenormalizer";
  4. import {METADATA_TYPE} from "~/types/enum/data";
  5. describe('denormalize', () => {
  6. test('should parse a API Item response and return a JSON Object', () => {
  7. const data: AnyJson = {
  8. '@context': '/api/contexts/Access',
  9. '@id': '/api/accesses/7351',
  10. '@type': 'Access',
  11. organization: '/api/organizations/37306',
  12. id: 7351,
  13. person: {
  14. '@type': 'Person',
  15. id: 11344,
  16. name: 'BRUEL',
  17. givenName: 'Patrick'
  18. }
  19. }
  20. const result = HydraDenormalizer.denormalize(data)
  21. // @ts-ignore
  22. expect(result.data).toEqual(HydraDenormalizer.getData(data))
  23. // @ts-ignore
  24. expect(result.metadata).toEqual(HydraDenormalizer.getMetadata(data))
  25. const expected = {
  26. "data": {
  27. "@context": "/api/contexts/Access",
  28. "@id": "/api/accesses/7351",
  29. "@type": "Access",
  30. "id": 7351,
  31. "organization": "/api/organizations/37306",
  32. "person": {
  33. "@type": "Person",
  34. "givenName": "Patrick",
  35. "id": 11344,
  36. "name": "BRUEL"
  37. }
  38. },
  39. "metadata": {
  40. "type": METADATA_TYPE.ITEM
  41. }
  42. }
  43. expect(result).toStrictEqual<AnyJson>(expected)
  44. })
  45. it('should parse a API Collection response and return a JSON Object', () => {
  46. const data: AnyJson = {
  47. '@context': '/api/contexts/Access',
  48. '@id': '/api/accesses',
  49. '@type': 'hydra:Collection',
  50. 'hydra:member': [{
  51. '@id': '/api/accesses/7351',
  52. organization: '/api/organizations/37306',
  53. id: 7351,
  54. person: {
  55. '@type': 'Person',
  56. id: 11344,
  57. name: 'BRUEL',
  58. givenName: 'Patrick'
  59. }
  60. }, {
  61. '@id': '/api/accesses/7352',
  62. organization: '/api/organizations/37306',
  63. id: 7352,
  64. person: {
  65. '@type': 'Person',
  66. id: 11345,
  67. name: 'BRASSENS',
  68. givenName: 'George'
  69. }
  70. }
  71. ],
  72. "hydra:view": {
  73. "@id": "/api/accesses?page=3",
  74. "@type": "hydra:PartialCollectionView",
  75. "hydra:first": "/api/accesses?page=1",
  76. "hydra:last": "/api/accesses?page=5",
  77. "hydra:next": "/api/accesses?page=4",
  78. "hydra:previous": "/api/accesses?page=2"
  79. }
  80. }
  81. const result = HydraDenormalizer.denormalize(data)
  82. // @ts-ignore
  83. expect(result.data).toEqual(HydraDenormalizer.getData(data))
  84. // @ts-ignore
  85. expect(result.metadata).toEqual(HydraDenormalizer.getMetadata(data))
  86. const expected = JSON.stringify(
  87. {"data":[
  88. {
  89. '@id': '/api/accesses/7351',
  90. organization: '/api/organizations/37306',
  91. id: 7351,
  92. person:
  93. {
  94. '@type': 'Person',
  95. id: 11344,
  96. name: 'BRUEL',
  97. givenName: 'Patrick'
  98. }
  99. },
  100. {
  101. '@id': '/api/accesses/7352',
  102. organization: '/api/organizations/37306',
  103. id: 7352,
  104. person:
  105. {
  106. '@type': 'Person',
  107. id: 11345,
  108. name: 'BRASSENS',
  109. givenName: 'George'
  110. }
  111. }
  112. ],
  113. 'metadata': {
  114. 'firstPage': 1,
  115. 'lastPage': 5,
  116. 'nextPage': 4,
  117. 'previousPage': 2,
  118. 'type': 1
  119. }
  120. })
  121. expect(JSON.stringify(result)).toEqual(expected)
  122. })
  123. })
  124. describe('getData', () => {
  125. test('With collection', () => {
  126. const data = {
  127. "@context": "/api/contexts/Foo",
  128. "@id": "/api/foo",
  129. "@type": "hydra:Collection",
  130. "hydra:member": [ 'foo' ],
  131. }
  132. // @ts-ignore
  133. expect(HydraDenormalizer.getData(data)).toEqual([ 'foo' ])
  134. })
  135. test('With item', () => {
  136. const data = {
  137. "@context": "/api/contexts/Foo",
  138. "@id": "/api/foo",
  139. "@type": "Foo",
  140. "param1": 'a',
  141. }
  142. // @ts-ignore
  143. expect(HydraDenormalizer.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. // @ts-ignore
  164. const metadata = HydraDenormalizer.getMetadata(data)
  165. expect(metadata.totalItems).toEqual(10)
  166. expect(metadata.firstPage).toEqual(1)
  167. expect(metadata.lastPage).toEqual(5)
  168. expect(metadata.nextPage).toEqual(4)
  169. expect(metadata.previousPage).toEqual(2)
  170. expect(metadata.type).toEqual(METADATA_TYPE.COLLECTION)
  171. })
  172. test('With partial collection metadata', () => {
  173. const data = {
  174. "@context": "/api/contexts/Foo",
  175. "@id": "/api/foo",
  176. "@type": "hydra:Collection",
  177. "hydra:member": [ 'foo' ],
  178. "hydra:totalItems": 10,
  179. "hydra:view": {
  180. "@id": "/api/foo?page=3",
  181. "@type": "hydra:PartialCollectionView",
  182. "hydra:first": "/api/foo?page=1",
  183. }
  184. }
  185. // @ts-ignore
  186. const metadata = HydraDenormalizer.getMetadata(data)
  187. expect(metadata.totalItems).toEqual(10)
  188. expect(metadata.firstPage).toEqual(1)
  189. expect(metadata.lastPage).toEqual(1)
  190. expect(metadata.nextPage).toEqual(undefined)
  191. expect(metadata.previousPage).toEqual(undefined)
  192. })
  193. test('With item metadata', () => {
  194. // @ts-ignore
  195. const metadata = HydraDenormalizer.getMetadata({})
  196. expect(metadata.type).toEqual(METADATA_TYPE.ITEM)
  197. })
  198. })