| 1 |
import { Server, Model, hasMany, belongsTo, Serializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Associations | Sideloading and Embedded Collections", function () { |
| 4 |
let server, BaseSerializer; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
wordSmith: Model.extend({ |
| 10 |
posts: hasMany("blog-post"), |
| 11 |
}), |
| 12 |
blogPost: Model.extend({ |
| 13 |
author: belongsTo("word-smith"), |
| 14 |
comments: hasMany("fine-comment"), |
| 15 |
}), |
| 16 |
fineComment: Model.extend({ |
| 17 |
post: belongsTo("blog-post"), |
| 18 |
}), |
| 19 |
}, |
| 20 |
}); |
| 21 |
|
| 22 |
let link = server.schema.wordSmiths.create({ name: "Link" }); |
| 23 |
let blogPost = link.createPost({ title: "Lorem" }); |
| 24 |
link.createPost({ title: "Ipsum" }); |
| 25 |
|
| 26 |
blogPost.createComment({ text: "pwned" }); |
| 27 |
|
| 28 |
let zelda = server.schema.wordSmiths.create({ name: "Zelda" }); |
| 29 |
zelda.createPost({ title: `Zeldas blogPost` }); |
| 30 |
|
| 31 |
BaseSerializer = Serializer.extend({ |
| 32 |
embed: false, |
| 33 |
}); |
| 34 |
}); |
| 35 |
|
| 36 |
afterEach(function () { |
| 37 |
server.shutdown(); |
| 38 |
}); |
| 39 |
|
| 40 |
test(`it can sideload a collection with a has-many relationship containing embedded models`, () => { |
| 41 |
server.config({ |
| 42 |
serializers: { |
| 43 |
application: BaseSerializer, |
| 44 |
wordSmith: BaseSerializer.extend({ |
| 45 |
embed: false, |
| 46 |
include: ["posts"], |
| 47 |
}), |
| 48 |
blogPost: BaseSerializer.extend({ |
| 49 |
embed: true, |
| 50 |
include: ["comments"], |
| 51 |
}), |
| 52 |
}, |
| 53 |
}); |
| 54 |
|
| 55 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 56 |
let result = server.serializerOrRegistry.serialize(wordSmiths); |
| 57 |
|
| 58 |
expect(result).toEqual({ |
| 59 |
wordSmiths: [ |
| 60 |
{ id: "1", name: "Link", postIds: ["1", "2"] }, |
| 61 |
{ id: "2", name: "Zelda", postIds: ["3"] }, |
| 62 |
], |
| 63 |
blogPosts: [ |
| 64 |
{ id: "1", title: "Lorem", comments: [{ id: "1", text: "pwned" }] }, |
| 65 |
{ id: "2", title: "Ipsum", comments: [] }, |
| 66 |
{ id: "3", title: "Zeldas blogPost", comments: [] }, |
| 67 |
], |
| 68 |
}); |
| 69 |
}); |
| 70 |
|
| 71 |
test(`it can sideload a collection with a belongs-to relationship containing embedded models`, () => { |
| 72 |
server.config({ |
| 73 |
serializers: { |
| 74 |
application: BaseSerializer, |
| 75 |
fineComment: BaseSerializer.extend({ |
| 76 |
embed: false, |
| 77 |
include: ["post"], |
| 78 |
}), |
| 79 |
blogPost: BaseSerializer.extend({ |
| 80 |
embed: true, |
| 81 |
include: ["author"], |
| 82 |
}), |
| 83 |
}, |
| 84 |
}); |
| 85 |
|
| 86 |
let fineComments = server.schema.fineComments.all(); |
| 87 |
let result = server.serializerOrRegistry.serialize(fineComments); |
| 88 |
|
| 89 |
expect(result).toEqual({ |
| 90 |
fineComments: [{ id: "1", text: "pwned", postId: "1" }], |
| 91 |
blogPosts: [ |
| 92 |
{ |
| 93 |
id: "1", |
| 94 |
title: "Lorem", |
| 95 |
author: { id: "1", name: "Link" }, |
| 96 |
}, |
| 97 |
], |
| 98 |
}); |
| 99 |
}); |
| 100 |
}); |