| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- import { describe, test, it, expect } from 'vitest'
- import {AnyJson, ApiResponse, HydraMetadata} from "~/types/data";
- import HydraDenormalizer from "~/services/data/normalizer/hydraDenormalizer";
- import {METADATA_TYPE} from "~/types/enum/data";
- class TestableHydraDenormalizer extends HydraDenormalizer {
- public static denormalize(hydraData: AnyJson): ApiResponse { return super.denormalize(hydraData) }
- public static getData(hydraData: AnyJson): AnyJson { return super.getData(hydraData) }
- public static getMetadata(data: AnyJson): HydraMetadata { return super.getMetadata(data) }
- }
- 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: 'BRUEL',
- givenName: 'Patrick'
- }
- }
- const result = HydraDenormalizer.denormalize(data)
- expect(result.data).toEqual(TestableHydraDenormalizer.getData(data))
- expect(result.metadata).toEqual(TestableHydraDenormalizer.getMetadata(data))
- const expected = {
- "data": {
- "@context": "/api/contexts/Access",
- "@id": "/api/accesses/7351",
- "@type": "Access",
- "id": 7351,
- "organization": "/api/organizations/37306",
- "person": {
- "@type": "Person",
- "givenName": "Patrick",
- "id": 11344,
- "name": "BRUEL"
- }
- },
- "metadata": {
- "type": METADATA_TYPE.ITEM
- }
- }
- expect(result).toStrictEqual<AnyJson>(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: {
- '@type': 'Person',
- id: 11344,
- name: 'BRUEL',
- givenName: 'Patrick'
- }
- }, {
- '@id': '/api/accesses/7352',
- organization: '/api/organizations/37306',
- id: 7352,
- person: {
- '@type': '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 = HydraDenormalizer.denormalize(data)
- expect(result.data).toEqual(TestableHydraDenormalizer.getData(data))
- expect(result.metadata).toEqual(TestableHydraDenormalizer.getMetadata(data))
- const expected = JSON.stringify(
- {"data":[
- {
- '@id': '/api/accesses/7351',
- organization: '/api/organizations/37306',
- id: 7351,
- person:
- {
- '@type': 'Person',
- id: 11344,
- name: 'BRUEL',
- givenName: 'Patrick'
- }
- },
- {
- '@id': '/api/accesses/7352',
- organization: '/api/organizations/37306',
- id: 7352,
- person:
- {
- '@type': 'Person',
- id: 11345,
- name: 'BRASSENS',
- givenName: 'George'
- }
- }
- ],
- 'metadata': {
- 'firstPage': 1,
- 'lastPage': 5,
- 'nextPage': 4,
- 'previousPage': 2,
- 'type': 1
- }
- })
- expect(JSON.stringify(result)).toEqual(expected)
- })
- })
- describe('getData', () => {
- test('With collection', () => {
- const data = {
- "@context": "/api/contexts/Foo",
- "@id": "/api/foo",
- "@type": "hydra:Collection",
- "hydra:member": [ 'foo' ],
- }
- expect(TestableHydraDenormalizer.getData(data)).toEqual([ 'foo' ])
- })
- test('With item', () => {
- const data = {
- "@context": "/api/contexts/Foo",
- "@id": "/api/foo",
- "@type": "Foo",
- "param1": 'a',
- }
- expect(TestableHydraDenormalizer.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"
- }
- }
- const metadata = TestableHydraDenormalizer.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",
- }
- }
- const metadata = TestableHydraDenormalizer.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', () => {
- const metadata = TestableHydraDenormalizer.getMetadata({})
- expect(metadata.type).toEqual(METADATA_TYPE.ITEM)
- })
- })
|