Skip to content

Commit cb5b9ef

Browse files
authored
Merge pull request #13 from richmolj/master
Enhance relationships tests
2 parents ddb9276 + b4ac656 commit cb5b9ef

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

test/fixtures.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ let Author = Person.extend({
3131

3232
nilly: attr(),
3333

34-
multiWords: hasMany('multi_words'),
35-
books: hasMany(),
36-
tags: hasMany(),
37-
genre: belongsTo('genres'),
38-
bio: hasOne('bios')
34+
multiWords: hasMany('multi_words'),
35+
specialBooks: hasMany('books'),
36+
books: hasMany(),
37+
tags: hasMany(),
38+
genre: belongsTo('genres'),
39+
bio: hasOne('bios')
3940
});
4041

4142
class Book extends ApplicationRecord {

test/unit/model-test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ describe('Model', function() {
203203
type: 'books'
204204
}]
205205
},
206+
special_books: {
207+
data: [{
208+
id: '3',
209+
type: 'books'
210+
}]
211+
},
206212
bio: {
207213
data: {
208214
id: '1',
@@ -223,8 +229,8 @@ describe('Model', function() {
223229
},
224230
relationships: {
225231
author: {
226-
data: {
227-
id: '2',
232+
data: {
233+
id: '2',
228234
type: 'authors'
229235
},
230236
}
@@ -245,6 +251,13 @@ describe('Model', function() {
245251
}
246252
}
247253
},
254+
{
255+
type: 'books',
256+
id: '3',
257+
attributes: {
258+
title: "Peanut Butter & Cupcake"
259+
}
260+
},
248261
{
249262
type: 'genres',
250263
id: '1',
@@ -332,6 +345,14 @@ describe('Model', function() {
332345
expect(book.title).to.eq("Where's My Butt?");
333346
});
334347

348+
it('assigns hasMany relationships with same jsonapiType correctly', function() {
349+
let instance = Model.fromJsonapi(doc.data, doc);
350+
expect(instance.specialBooks.length).to.eq(1);
351+
let book = instance.specialBooks[0];
352+
expect(book).to.be.instanceof(Book);
353+
expect(book.title).to.eq("Peanut Butter & Cupcake");
354+
});
355+
335356
it('assigns belongsTo relationships correctly', function() {
336357
let instance = Model.fromJsonapi(doc.data, doc);
337358
let genre = instance.genre;

0 commit comments

Comments
 (0)