From 59ab2fcb8f685eff1762320ba5573b3a693451f5 Mon Sep 17 00:00:00 2001 From: Friedrich Pawelka Date: Wed, 20 Jan 2021 23:45:46 +0100 Subject: [PATCH 1/3] Add test for including of primary data in relationships --- .../json-api-datastore.service.spec.ts | 24 +++++++++++++++++++ .../test/fixtures/chapter.fixture.ts | 20 ++++++++++++++-- .../test/models/chapter.model.ts | 7 ++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts b/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts index eb896011..fb4f2f01 100644 --- a/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts +++ b/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts @@ -16,6 +16,7 @@ import { Thing } from '../../test/models/thing'; import { getSampleThing } from '../../test/fixtures/thing.fixture'; import { ModelConfig } from '../interfaces/model-config.interface'; import { JsonApiQueryData } from '../models/json-api-query-data'; +import { getSampleChapter } from 'projects/angular2-jsonapi/test/fixtures/chapter.fixture'; let datastore: Datastore; let datastoreWithConfig: DatastoreWithConfig; @@ -208,6 +209,29 @@ describe('JsonApiDatastore', () => { queryRequest.flush(getSampleThing()); }); + it('should handle related resource objects that are listed in primary data', () => { + const expectedUrl = encodeURI(`${BASE_URL}/${API_VERSION}/chapters`); + + datastore.findAll(Chapter).subscribe((document) => { + expect(document).toBeDefined(); + const chapters = document.getModels(); + expect(chapters[0].relatesTo).toBeDefined(); + expect(chapters[0].relatesTo.id).toBe('b'); + expect(chapters[1].related).toBeDefined(); + expect(chapters[1].related.length).toBe(2); + expect(chapters[0].relatesTo.related[0].id).toBe('a'); + }); + + const queryRequest = httpMock.expectOne(expectedUrl); + queryRequest.flush({ + data: [ + getSampleChapter(1, 'a', 'Chapter A', null, 'b'), + getSampleChapter(1, 'b', 'Chapter B', ['a', 'c'], null), + getSampleChapter(1, 'c', 'Chapter C', null, 'b'), + ], + }); + }); + it('should fire error', () => { const expectedUrl = `${BASE_URL}/${API_VERSION}/authors`; const dummyResponse = { diff --git a/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts b/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts index 27126ec1..90f432e9 100644 --- a/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts +++ b/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts @@ -1,5 +1,10 @@ -export function getSampleChapter(i: number, chapterId: string, chapterTitle: string = 'Dummy title') { - return { +export function getSampleChapter( + i: number, chapterId: string, + chapterTitle: string = 'Dummy title', + related?: string[], + relatesTo?: string +) { + const response: any = { id: chapterId, type: 'chapters', attributes: { @@ -30,4 +35,15 @@ export function getSampleChapter(i: number, chapterId: string, chapterTitle: str self: '/v1/authors/288' } }; + if (related) { + response.relationships.related = { + data: related.map(id => ({id, type: 'chapters'})), + }; + } + if (relatesTo) { + response.relationships.relatesTo = { + data: {id: relatesTo, type: 'chapters'} + }; + } + return response; } diff --git a/projects/angular2-jsonapi/test/models/chapter.model.ts b/projects/angular2-jsonapi/test/models/chapter.model.ts index 21e82e20..d7011ceb 100644 --- a/projects/angular2-jsonapi/test/models/chapter.model.ts +++ b/projects/angular2-jsonapi/test/models/chapter.model.ts @@ -4,6 +4,7 @@ import { JsonApiModelConfig } from '../../src/decorators/json-api-model-config.d import { JsonApiModel } from '../../src/models/json-api.model'; import { Attribute } from '../../src/decorators/attribute.decorator'; import { BelongsTo } from '../../src/decorators/belongs-to.decorator'; +import { HasMany } from '../../src/decorators/has-many.decorator'; import { Section } from './section.model'; @JsonApiModelConfig({ @@ -28,4 +29,10 @@ export class Chapter extends JsonApiModel { @BelongsTo() firstSection: Section; + + @HasMany() + related: Chapter[]; + + @BelongsTo() + relatesTo: Chapter; } From 131c4a3f265da670d5433153e81019e18194ad22 Mon Sep 17 00:00:00 2001 From: Friedrich Pawelka Date: Thu, 21 Jan 2021 00:19:37 +0100 Subject: [PATCH 2/3] Use all resource objects when syncing relationships --- .../src/services/json-api-datastore.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts b/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts index e059719b..77c229c0 100644 --- a/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts +++ b/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts @@ -340,14 +340,14 @@ export class JsonApiDatastore { const body: any = response.body; const models: T[] = []; + const resourceObjects = [...body.data, ...(body.included || [])]; + body.data.forEach((data: any) => { const model: T = this.deserializeModel(modelType, data); this.addToStore(model); - if (body.included) { - model.syncRelationships(data, body.included.concat(data)); - this.addToStore(model); - } + model.syncRelationships(data, resourceObjects); + this.addToStore(model); models.push(model); }); From 2ea6e5259678eedeb040ccd140b92ead16175508 Mon Sep 17 00:00:00 2001 From: Friedrich Pawelka Date: Thu, 21 Jan 2021 00:27:08 +0100 Subject: [PATCH 3/3] Fix lint error --- .../angular2-jsonapi/src/services/json-api-datastore.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts b/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts index 77c229c0..cc396e70 100644 --- a/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts +++ b/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts @@ -288,7 +288,7 @@ export class JsonApiDatastore { }; } } else if (data[key] === null) { - const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key); + const entity = belongsToMetadata.find((anEntity: any) => anEntity.propertyName === key); if (entity) { relationships = relationships || {};