hydraNormalizer.test.ts 6.9 KB

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