| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- import { describe, test, it, expect } from 'vitest'
- import {AnyJson} from "~/types/data";
- import HydraNormalizer from "~/services/data/normalizer/hydraNormalizer";
- import {METADATA_TYPE} from "~/types/enum/data";
- import Access from "~/models/Access/Access";
- describe('denormalize', () => {
- test('should parse a API Item response and return a JSON Object', () => {
- const data: AnyJson = {
- '@context': '/api/contexts/Access',
- '@id': '/api/accesses/7351',
- '@type': 'Access',
- organization: '/api/organizations/37306',
- id: 7351,
- person: {
- '@type': 'Person',
- id: 11344,
- name: 'Trennet',
- givenName: 'Charles',
- username: "ctrennet"
- }
- }
- const result = HydraNormalizer.denormalize(data, Access)
- // @ts-ignore
- expect(result.data).toEqual(HydraNormalizer.getData(data))
- // @ts-ignore
- expect(result.metadata).toEqual(HydraNormalizer.getMetadata(data))
- const expected = {
- "data": {
- "id": 7351,
- "organization": 37306,
- "person": {
- "accessId": null,
- "givenName": "Charles",
- "id": 11344,
- "name": "Trennet",
- "username": "ctrennet"
- },
- "activityYear": 0,
- "historical": {}
- },
- "metadata": {
- "type": METADATA_TYPE.ITEM
- }
- }
- expect(result).toEqual(expected)
- })
- it('should parse a API Collection response and return a JSON Object', () => {
- const data: AnyJson = {
- '@context': '/api/contexts/Access',
- '@id': '/api/accesses',
- '@type': 'hydra:Collection',
- 'hydra:member': [{
- '@id': '/api/accesses/7351',
- organization: '/api/organizations/37306',
- id: 7351,
- person: {
- id: 11344,
- name: 'Trennet',
- givenName: 'Charles',
- username: "ctrennet"
- }
- }, {
- '@id': '/api/accesses/7352',
- organization: '/api/organizations/37306',
- id: 7352,
- person: {
- id: 11345,
- name: 'BRASSENS',
- givenName: 'George'
- }
- }
- ],
- "hydra:view": {
- "@id": "/api/accesses?page=3",
- "@type": "hydra:PartialCollectionView",
- "hydra:first": "/api/accesses?page=1",
- "hydra:last": "/api/accesses?page=5",
- "hydra:next": "/api/accesses?page=4",
- "hydra:previous": "/api/accesses?page=2"
- }
- }
- const result = HydraNormalizer.denormalize(data, Access)
- // @ts-ignore
- expect(result.data).toEqual(HydraNormalizer.getData(data))
- // @ts-ignore
- expect(result.metadata).toEqual(HydraNormalizer.getMetadata(data))
- const expected =
- {
- "data": [
- {
- id: 7351,
- organization: 37306,
- person: {
- id: 11344,
- name: 'Trennet',
- givenName: 'Charles',
- accessId: null,
- username: "ctrennet"
- },
- activityYear: 0,
- historical: {}
- },
- {
- id: 7352,
- organization: 37306,
- person: {
- id: 11345,
- name: 'BRASSENS',
- givenName: 'George',
- accessId: null,
- username: null
- },
- activityYear: 0,
- historical: {}
- }
- ],
- 'metadata': {
- firstPage: 1,
- lastPage: 5,
- nextPage: 4,
- previousPage: 2,
- totalItems: undefined,
- type: 1
- }
- }
- expect(result).toEqual(expected)
- })
- })
- describe('getData', () => {
- test('With collection', () => {
- const data = {
- "@context": "/api/contexts/Foo",
- "@id": "/api/foo",
- "@type": "hydra:Collection",
- "hydra:member": [ 'foo' ],
- }
- // @ts-ignore
- expect(HydraNormalizer.getData(data)).toEqual([ 'foo' ])
- })
- test('With item', () => {
- const data = {
- "@context": "/api/contexts/Foo",
- "@id": "/api/foo",
- "@type": "Foo",
- "param1": 'a',
- }
- // @ts-ignore
- expect(HydraNormalizer.getData(data)).toEqual(data)
- })
- })
- describe('getMetadata', () => {
- test('With valid collection metadata', () => {
- const data = {
- "@context": "/api/contexts/Foo",
- "@id": "/api/foo",
- "@type": "hydra:Collection",
- "hydra:member": [ 'foo' ],
- "hydra:totalItems": 10,
- "hydra:view": {
- "@id": "/api/foo?page=3",
- "@type": "hydra:PartialCollectionView",
- "hydra:first": "/api/foo?page=1",
- "hydra:last": "/api/foo?page=5",
- "hydra:next": "/api/foo?page=4",
- "hydra:previous": "/api/foo?page=2"
- }
- }
- // @ts-ignore
- const metadata = HydraNormalizer.getMetadata(data)
- expect(metadata.totalItems).toEqual(10)
- expect(metadata.firstPage).toEqual(1)
- expect(metadata.lastPage).toEqual(5)
- expect(metadata.nextPage).toEqual(4)
- expect(metadata.previousPage).toEqual(2)
- expect(metadata.type).toEqual(METADATA_TYPE.COLLECTION)
- })
- test('With partial collection metadata', () => {
- const data = {
- "@context": "/api/contexts/Foo",
- "@id": "/api/foo",
- "@type": "hydra:Collection",
- "hydra:member": [ 'foo' ],
- "hydra:totalItems": 10,
- "hydra:view": {
- "@id": "/api/foo?page=3",
- "@type": "hydra:PartialCollectionView",
- "hydra:first": "/api/foo?page=1",
- }
- }
- // @ts-ignore
- const metadata = HydraNormalizer.getMetadata(data)
- expect(metadata.totalItems).toEqual(10)
- expect(metadata.firstPage).toEqual(1)
- expect(metadata.lastPage).toEqual(1)
- expect(metadata.nextPage).toEqual(undefined)
- expect(metadata.previousPage).toEqual(undefined)
- })
- test('With item metadata', () => {
- // @ts-ignore
- const metadata = HydraNormalizer.getMetadata({})
- expect(metadata.type).toEqual(METADATA_TYPE.ITEM)
- })
- })
|