|
|
@@ -122,6 +122,14 @@ describe('denormalize', () => {
|
|
|
})
|
|
|
|
|
|
describe('getData', () => {
|
|
|
+ // @ts-ignore
|
|
|
+ const initialDenormalizeItem = HydraNormalizer.denormalizeItem
|
|
|
+
|
|
|
+ afterEach(() => {
|
|
|
+ // @ts-ignore
|
|
|
+ HydraNormalizer.denormalizeItem = initialDenormalizeItem
|
|
|
+ })
|
|
|
+
|
|
|
test('With collection', () => {
|
|
|
const data = {
|
|
|
'@context': '/api/contexts/Foo',
|
|
|
@@ -130,8 +138,13 @@ describe('getData', () => {
|
|
|
member: ['foo'],
|
|
|
}
|
|
|
|
|
|
+ const model = DummyApiModel
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ HydraNormalizer.denormalizeItem = vi.fn((data, model) => data)
|
|
|
+
|
|
|
// @ts-ignore
|
|
|
- expect(HydraNormalizer.getData(data)).toEqual(['foo'])
|
|
|
+ expect(HydraNormalizer.getData(data, model)).toEqual(['foo'])
|
|
|
})
|
|
|
|
|
|
test('With item', () => {
|
|
|
@@ -142,8 +155,13 @@ describe('getData', () => {
|
|
|
param1: 'a',
|
|
|
}
|
|
|
|
|
|
+ const model = DummyApiModel
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ HydraNormalizer.denormalizeItem = vi.fn((data, model) => data)
|
|
|
+
|
|
|
// @ts-ignore
|
|
|
- expect(HydraNormalizer.getData(data)).toEqual(data)
|
|
|
+ expect(HydraNormalizer.getData(data, model)).toEqual(data)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
@@ -152,16 +170,16 @@ describe('getMetadata', () => {
|
|
|
const data = {
|
|
|
'@context': '/api/contexts/Foo',
|
|
|
'@id': '/api/foo',
|
|
|
- '@type': 'hydra:Collection',
|
|
|
- 'hydra:member': ['foo'],
|
|
|
- 'hydra:totalItems': 10,
|
|
|
- 'hydra:view': {
|
|
|
+ '@type': 'Collection',
|
|
|
+ 'member': ['foo'],
|
|
|
+ 'totalItems': 10,
|
|
|
+ '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',
|
|
|
+ '@type': 'PartialCollectionView',
|
|
|
+ 'first': '/api/foo?page=1',
|
|
|
+ 'last': '/api/foo?page=5',
|
|
|
+ 'next': '/api/foo?page=4',
|
|
|
+ 'previous': '/api/foo?page=2',
|
|
|
},
|
|
|
}
|
|
|
|
|
|
@@ -179,13 +197,13 @@ describe('getMetadata', () => {
|
|
|
const data = {
|
|
|
'@context': '/api/contexts/Foo',
|
|
|
'@id': '/api/foo',
|
|
|
- '@type': 'hydra:Collection',
|
|
|
- 'hydra:member': ['foo'],
|
|
|
- 'hydra:totalItems': 10,
|
|
|
- 'hydra:view': {
|
|
|
+ '@type': 'Collection',
|
|
|
+ 'member': ['foo'],
|
|
|
+ 'totalItems': 10,
|
|
|
+ 'view': {
|
|
|
'@id': '/api/foo?page=3',
|
|
|
- '@type': 'hydra:PartialCollectionView',
|
|
|
- 'hydra:first': '/api/foo?page=1',
|
|
|
+ '@type': 'PartialCollectionView',
|
|
|
+ 'first': '/api/foo?page=1',
|
|
|
},
|
|
|
}
|
|
|
|
|
|
@@ -234,20 +252,18 @@ describe('denormalizeItem', () => {
|
|
|
}
|
|
|
|
|
|
const model = DummyApiModel
|
|
|
- const expected = new DummyApiModel(item)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- HydraNormalizer.denormalizeEntity = vi.fn((model, item) => {
|
|
|
- return expected
|
|
|
+ const expected = new DummyApiModel({
|
|
|
+ '@id': '/api/dummyModel/1',
|
|
|
+ id: 1,
|
|
|
+ name: 'foo',
|
|
|
+ oneToOneRelation: null,
|
|
|
+ oneToManyRelation: [],
|
|
|
})
|
|
|
|
|
|
// @ts-ignore
|
|
|
const result = HydraNormalizer.denormalizeItem(item, model)
|
|
|
|
|
|
expect(result).toEqual(expected)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- expect(HydraNormalizer.denormalizeEntity).toHaveBeenCalledWith(model, item)
|
|
|
})
|
|
|
|
|
|
test('with no @id prop', () => {
|
|
|
@@ -256,17 +272,10 @@ describe('denormalizeItem', () => {
|
|
|
name: 'foo',
|
|
|
}
|
|
|
|
|
|
- console.error = vi.fn((msg) => {})
|
|
|
-
|
|
|
// @ts-ignore
|
|
|
const result = HydraNormalizer.denormalizeItem(item)
|
|
|
|
|
|
expect(result).toEqual(item)
|
|
|
-
|
|
|
- expect(console.error).toHaveBeenCalledWith(
|
|
|
- 'De-normalization error : the item is not hydra formatted',
|
|
|
- item,
|
|
|
- )
|
|
|
})
|
|
|
|
|
|
test('with enum', () => {
|
|
|
@@ -277,18 +286,10 @@ describe('denormalizeItem', () => {
|
|
|
C: 3,
|
|
|
}
|
|
|
|
|
|
- // @ts-ignore
|
|
|
- HydraNormalizer.denormalizeEnum = vi.fn((item) => {
|
|
|
- return item
|
|
|
- })
|
|
|
-
|
|
|
// @ts-ignore
|
|
|
const result = HydraNormalizer.denormalizeItem(item)
|
|
|
|
|
|
expect(result).toEqual(item)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- expect(HydraNormalizer.denormalizeEnum).toHaveBeenCalledWith(item)
|
|
|
})
|
|
|
|
|
|
test('with unparsable iri', () => {
|
|
|
@@ -298,75 +299,32 @@ describe('denormalizeItem', () => {
|
|
|
name: 'foo',
|
|
|
}
|
|
|
|
|
|
- // @ts-ignore
|
|
|
- HydraNormalizer.parseEntityIRI = vi.fn((iri) => {
|
|
|
- throw new Error('parsing error')
|
|
|
- })
|
|
|
-
|
|
|
- console.error = vi.fn((msg) => {})
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- const result = HydraNormalizer.denormalizeItem(item)
|
|
|
-
|
|
|
- expect(result).toEqual(item)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- expect(console.error).toHaveBeenCalledWith(
|
|
|
- 'De-normalization error : could not parse the IRI',
|
|
|
- item,
|
|
|
- )
|
|
|
+ expect(
|
|
|
+ // @ts-ignore
|
|
|
+ () => HydraNormalizer.denormalizeItem(item),
|
|
|
+ ).toThrowError()
|
|
|
})
|
|
|
|
|
|
test('With valid iri and existing model', () => {
|
|
|
-
|
|
|
- //@ts-expect-error problème de typage sans conséquence
|
|
|
- HydraNormalizer.models = { dummyModel: DummyApiModel }
|
|
|
-
|
|
|
const item = {
|
|
|
'@id': '/api/dummyModel/1',
|
|
|
id: 1,
|
|
|
name: 'foo',
|
|
|
}
|
|
|
|
|
|
- // @ts-ignore
|
|
|
- HydraNormalizer.parseEntityIRI = vi.fn((iri) => {
|
|
|
- return { entity: 'dummyModel' }
|
|
|
- })
|
|
|
-
|
|
|
- const expected = new DummyApiModel(item)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- HydraNormalizer.denormalizeEntity = vi.fn((model, item) => {
|
|
|
- return expected
|
|
|
- })
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- const result = HydraNormalizer.denormalizeItem(item)
|
|
|
-
|
|
|
- expect(result).toEqual(expected)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- expect(HydraNormalizer.denormalizeEntity).toHaveBeenCalledWith(
|
|
|
- DummyApiModel,
|
|
|
- item,
|
|
|
- )
|
|
|
+ expect(
|
|
|
+ // @ts-ignore
|
|
|
+ () => HydraNormalizer.denormalizeItem(item),
|
|
|
+ ).toThrowError()
|
|
|
})
|
|
|
|
|
|
test('With valid iri and un-existing model', () => {
|
|
|
- //@ts-expect-error problème de typage sans conséquence
|
|
|
- HydraNormalizer.models = { dummyModel: DummyApiModel }
|
|
|
-
|
|
|
const item = {
|
|
|
'@id': '/api/unknownModel/1',
|
|
|
id: 1,
|
|
|
name: 'foo',
|
|
|
}
|
|
|
|
|
|
- // @ts-ignore
|
|
|
- HydraNormalizer.parseEntityIRI = vi.fn((iri) => {
|
|
|
- return 'unknownModel'
|
|
|
- })
|
|
|
-
|
|
|
expect(
|
|
|
// @ts-ignore
|
|
|
() => HydraNormalizer.denormalizeItem(item),
|