hydraDenormalizer.test.ts 7.2 KB

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