Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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);
});
Expand Down
20 changes: 18 additions & 2 deletions projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions projects/angular2-jsonapi/test/models/chapter.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -28,4 +29,10 @@ export class Chapter extends JsonApiModel {

@BelongsTo()
firstSection: Section;

@HasMany()
related: Chapter[];

@BelongsTo()
relatesTo: Chapter;
}