Skip to content

Commit

Permalink
Use correct key for BelongsTo-relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
hpawe01 committed May 28, 2019
1 parent 65d02f0 commit 6517914
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
26 changes: 25 additions & 1 deletion src/services/json-api-datastore.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { TestBed } from '@angular/core/testing';
import { format, parse } from 'date-fns';
import { Author } from '../../test/models/author.model';
import { Chapter } from '../../test/models/chapter.model';
import { AUTHOR_API_VERSION, AUTHOR_MODEL_ENDPOINT_URL, CustomAuthor } from '../../test/models/custom-author.model';
import { AUTHOR_BIRTH, AUTHOR_ID, AUTHOR_NAME, BOOK_TITLE, getAuthorData } from '../../test/fixtures/author.fixture';
import { AUTHOR_BIRTH, AUTHOR_ID, AUTHOR_NAME, BOOK_TITLE, getAuthorData, CHAPTER_TITLE } from '../../test/fixtures/author.fixture';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { API_VERSION, BASE_URL, Datastore } from '../../test/datastore.service';
import { ErrorResponse } from '../models/error-response.model';
Expand Down Expand Up @@ -455,6 +456,29 @@ describe('JsonApiDatastore', () => {

saveRequest.flush(null, { status: 204, statusText: 'No Content' });
});

it('should use correct key for BelongsTo-relationship', () => {
const expectedUrl = `${BASE_URL}/${API_VERSION}/books`;
const CHAPTER_ID = '1';
const book = datastore.createRecord(Book, {
title: BOOK_TITLE
});

book.firstChapter = new Chapter(datastore, {
id: CHAPTER_ID
});

book.save().subscribe();

const saveRequest = httpMock.expectOne(expectedUrl);
const obj = saveRequest.request.body.data;
expect(obj.relationships).toBeDefined();
expect(obj.relationships.firstChapter).toBeUndefined();
expect(obj.relationships['first-chapter']).toBeDefined();
expect(obj.relationships['first-chapter'].data.id).toBe(CHAPTER_ID);

saveRequest.flush({});
});
});

describe('updateRecord', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/services/json-api-datastore.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,17 @@ export class JsonApiDatastore {
protected getRelationships(data: any): any {
let relationships: any;

const belongsToMetadata: any[] = Reflect.getMetadata('BelongsTo', data) || [];

for (const key in data) {
if (data.hasOwnProperty(key)) {
if (data[key] instanceof JsonApiModel) {
relationships = relationships || {};

if (data[key].id) {
relationships[key] = {
const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key);
const relationshipKey = entity.relationship;
relationships[relationshipKey] = {
data: this.buildSingleRelationshipData(data[key])
};
}
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/author.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getSampleBook} from './book.fixture';
import { getSampleBook } from './book.fixture';
import { getSampleChapter } from './chapter.fixture';
import { getSampleSection } from './section.fixture';
import { getSampleParagraph } from './paragraph.fixture';
Expand Down Expand Up @@ -80,7 +80,7 @@ export function getIncludedBooks(totalBooks: number, relationship?: string, tota
if (relationship && relationship.indexOf('books.firstChapter') !== -1) {
const firstChapterId = '1';

book.relationships.firstChapter = {
book.relationships['first-chapter'] = {
data: {
id: firstChapterId,
type: 'chapters'
Expand Down
2 changes: 1 addition & 1 deletion test/models/book.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Book extends JsonApiModel {
@HasMany()
chapters: Chapter[];

@BelongsTo()
@BelongsTo({ key: 'first-chapter' })
firstChapter: Chapter;

@BelongsTo()
Expand Down

0 comments on commit 6517914

Please sign in to comment.