| 1 |
import { Server, Model, hasMany, belongsTo, Serializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Associations | Sideloading 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 throws an error if embed is false and root is false`, () => { |
| 41 |
server.config({ |
| 42 |
serializers: { |
| 43 |
wordSmith: BaseSerializer.extend({ |
| 44 |
root: false, |
| 45 |
include: ["posts"], |
| 46 |
}), |
| 47 |
}, |
| 48 |
}); |
| 49 |
|
| 50 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 51 |
|
| 52 |
expect(function () { |
| 53 |
server.serializerOrRegistry.serialize(wordSmiths); |
| 54 |
}).toThrow(); |
| 55 |
}); |
| 56 |
|
| 57 |
test(`it can sideload an empty collection`, () => { |
| 58 |
server.schema.db.emptyData(); |
| 59 |
server.config({ |
| 60 |
serializers: { |
| 61 |
application: BaseSerializer, |
| 62 |
wordSmith: BaseSerializer.extend({ |
| 63 |
include: ["posts"], |
| 64 |
}), |
| 65 |
}, |
| 66 |
}); |
| 67 |
|
| 68 |
let result = server.serializerOrRegistry.serialize( |
| 69 |
server.schema.wordSmiths.all() |
| 70 |
); |
| 71 |
|
| 72 |
expect(result).toEqual({ |
| 73 |
wordSmiths: [], |
| 74 |
}); |
| 75 |
}); |
| 76 |
|
| 77 |
test(`it can sideload a collection with a has-many relationship`, () => { |
| 78 |
server.config({ |
| 79 |
serializers: { |
| 80 |
application: BaseSerializer, |
| 81 |
wordSmith: BaseSerializer.extend({ |
| 82 |
embed: false, |
| 83 |
include: ["posts"], |
| 84 |
}), |
| 85 |
}, |
| 86 |
}); |
| 87 |
|
| 88 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 89 |
let result = server.serializerOrRegistry.serialize(wordSmiths); |
| 90 |
|
| 91 |
expect(result).toEqual({ |
| 92 |
wordSmiths: [ |
| 93 |
{ id: "1", name: "Link", postIds: ["1", "2"] }, |
| 94 |
{ id: "2", name: "Zelda", postIds: ["3"] }, |
| 95 |
], |
| 96 |
blogPosts: [ |
| 97 |
{ id: "1", title: "Lorem" }, |
| 98 |
{ id: "2", title: "Ipsum" }, |
| 99 |
{ id: "3", title: "Zeldas blogPost" }, |
| 100 |
], |
| 101 |
}); |
| 102 |
}); |
| 103 |
|
| 104 |
test(`it can sideload a collection with a chain of has-many relationships`, () => { |
| 105 |
server.config({ |
| 106 |
serializers: { |
| 107 |
application: BaseSerializer, |
| 108 |
wordSmith: BaseSerializer.extend({ |
| 109 |
embed: false, |
| 110 |
include: ["posts"], |
| 111 |
}), |
| 112 |
blogPost: BaseSerializer.extend({ |
| 113 |
include: ["comments"], |
| 114 |
}), |
| 115 |
}, |
| 116 |
}); |
| 117 |
|
| 118 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 119 |
let result = server.serializerOrRegistry.serialize(wordSmiths); |
| 120 |
|
| 121 |
expect(result).toEqual({ |
| 122 |
wordSmiths: [ |
| 123 |
{ id: "1", name: "Link", postIds: ["1", "2"] }, |
| 124 |
{ id: "2", name: "Zelda", postIds: ["3"] }, |
| 125 |
], |
| 126 |
blogPosts: [ |
| 127 |
{ id: "1", title: "Lorem", commentIds: ["1"] }, |
| 128 |
{ id: "2", title: "Ipsum", commentIds: [] }, |
| 129 |
{ id: "3", title: "Zeldas blogPost", commentIds: [] }, |
| 130 |
], |
| 131 |
fineComments: [{ id: "1", text: "pwned" }], |
| 132 |
}); |
| 133 |
}); |
| 134 |
|
| 135 |
test(`it avoids circularity when serializing a collection`, () => { |
| 136 |
server.config({ |
| 137 |
serializers: { |
| 138 |
application: BaseSerializer, |
| 139 |
wordSmith: BaseSerializer.extend({ |
| 140 |
embed: false, |
| 141 |
include: ["posts"], |
| 142 |
}), |
| 143 |
blogPost: BaseSerializer.extend({ |
| 144 |
include: ["author"], |
| 145 |
}), |
| 146 |
}, |
| 147 |
}); |
| 148 |
|
| 149 |
let wordSmiths = server.schema.wordSmiths.all(); |
| 150 |
let result = server.serializerOrRegistry.serialize(wordSmiths); |
| 151 |
|
| 152 |
expect(result).toEqual({ |
| 153 |
wordSmiths: [ |
| 154 |
{ id: "1", name: "Link", postIds: ["1", "2"] }, |
| 155 |
{ id: "2", name: "Zelda", postIds: ["3"] }, |
| 156 |
], |
| 157 |
blogPosts: [ |
| 158 |
{ id: "1", title: "Lorem", authorId: "1" }, |
| 159 |
{ id: "2", title: "Ipsum", authorId: "1" }, |
| 160 |
{ id: "3", title: "Zeldas blogPost", authorId: "2" }, |
| 161 |
], |
| 162 |
}); |
| 163 |
}); |
| 164 |
|
| 165 |
test(`it can sideload a collection with a belongs-to relationship`, () => { |
| 166 |
server.config({ |
| 167 |
serializers: { |
| 168 |
application: BaseSerializer, |
| 169 |
blogPost: BaseSerializer.extend({ |
| 170 |
embed: false, |
| 171 |
include: ["author"], |
| 172 |
}), |
| 173 |
}, |
| 174 |
}); |
| 175 |
|
| 176 |
let blogPosts = server.schema.blogPosts.all(); |
| 177 |
let result = server.serializerOrRegistry.serialize(blogPosts); |
| 178 |
|
| 179 |
expect(result).toEqual({ |
| 180 |
blogPosts: [ |
| 181 |
{ id: "1", title: "Lorem", authorId: "1" }, |
| 182 |
{ id: "2", title: "Ipsum", authorId: "1" }, |
| 183 |
{ id: "3", title: "Zeldas blogPost", authorId: "2" }, |
| 184 |
], |
| 185 |
wordSmiths: [ |
| 186 |
{ id: "1", name: "Link" }, |
| 187 |
{ id: "2", name: "Zelda" }, |
| 188 |
], |
| 189 |
}); |
| 190 |
}); |
| 191 |
|
| 192 |
test(`it can sideload a collection with a chain of belongs-to relationships`, () => { |
| 193 |
server.config({ |
| 194 |
serializers: { |
| 195 |
application: BaseSerializer, |
| 196 |
fineComment: BaseSerializer.extend({ |
| 197 |
embed: false, |
| 198 |
include: ["post"], |
| 199 |
}), |
| 200 |
blogPost: BaseSerializer.extend({ |
| 201 |
include: ["author"], |
| 202 |
}), |
| 203 |
}, |
| 204 |
}); |
| 205 |
|
| 206 |
let fineComments = server.schema.fineComments.all(); |
| 207 |
let result = server.serializerOrRegistry.serialize(fineComments); |
| 208 |
|
| 209 |
expect(result).toEqual({ |
| 210 |
fineComments: [{ id: "1", text: "pwned", postId: "1" }], |
| 211 |
blogPosts: [{ id: "1", title: "Lorem", authorId: "1" }], |
| 212 |
wordSmiths: [{ id: "1", name: "Link" }], |
| 213 |
}); |
| 214 |
}); |
| 215 |
}); |