| 1 |
import { Server, Model, hasMany, belongsTo, JSONAPISerializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | JSON API Serializer | Associations | Collection", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
wordSmith: Model.extend({ |
| 10 |
posts: hasMany("blogPost", { inverse: "author" }), |
| 11 |
}), |
| 12 |
blogPost: Model.extend({ |
| 13 |
author: belongsTo("wordSmith", { inverse: "posts" }), |
| 14 |
comments: hasMany("fineComment", { inverse: "post" }), |
| 15 |
}), |
| 16 |
fineComment: Model.extend({ |
| 17 |
post: belongsTo("blogPost"), |
| 18 |
}), |
| 19 |
}, |
| 20 |
}); |
| 21 |
}); |
| 22 |
|
| 23 |
afterEach(() => { |
| 24 |
server.shutdown(); |
| 25 |
}); |
| 26 |
|
| 27 |
test(`by default, it doesn't include a collection's relationships if those relationships are not included in the document and no links are defined`, () => { |
| 28 |
server.config({ |
| 29 |
serializers: { |
| 30 |
application: JSONAPISerializer, |
| 31 |
}, |
| 32 |
}); |
| 33 |
server.schema.wordSmiths.create({ firstName: "Link", age: 123 }); |
| 34 |
server.schema.wordSmiths.create({ firstName: "Zelda", age: 456 }); |
| 35 |
|
| 36 |
let collection = server.schema.wordSmiths.all(); |
| 37 |
let result = server.serializerOrRegistry.serialize(collection); |
| 38 |
|
| 39 |
expect(result).toEqual({ |
| 40 |
data: [ |
| 41 |
{ |
| 42 |
type: "word-smiths", |
| 43 |
id: "1", |
| 44 |
attributes: { |
| 45 |
"first-name": "Link", |
| 46 |
age: 123, |
| 47 |
}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
type: "word-smiths", |
| 51 |
id: "2", |
| 52 |
attributes: { |
| 53 |
"first-name": "Zelda", |
| 54 |
age: 456, |
| 55 |
}, |
| 56 |
}, |
| 57 |
], |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
test(`when alwaysIncludeLinkageData is true, it contains linkage data for all a collection's relationships, regardless of includes`, () => { |
| 62 |
server.config({ |
| 63 |
serializers: { |
| 64 |
application: JSONAPISerializer.extend({ |
| 65 |
alwaysIncludeLinkageData: true, |
| 66 |
}), |
| 67 |
}, |
| 68 |
}); |
| 69 |
server.schema.wordSmiths.create({ firstName: "Link", age: 123 }); |
| 70 |
server.schema.wordSmiths.create({ firstName: "Zelda", age: 456 }); |
| 71 |
|
| 72 |
let collection = server.schema.wordSmiths.all(); |
| 73 |
let result = server.serializerOrRegistry.serialize(collection); |
| 74 |
|
| 75 |
expect(result).toEqual({ |
| 76 |
data: [ |
| 77 |
{ |
| 78 |
type: "word-smiths", |
| 79 |
id: "1", |
| 80 |
attributes: { |
| 81 |
"first-name": "Link", |
| 82 |
age: 123, |
| 83 |
}, |
| 84 |
relationships: { |
| 85 |
posts: { |
| 86 |
data: [], |
| 87 |
}, |
| 88 |
}, |
| 89 |
}, |
| 90 |
{ |
| 91 |
type: "word-smiths", |
| 92 |
id: "2", |
| 93 |
attributes: { |
| 94 |
"first-name": "Zelda", |
| 95 |
age: 456, |
| 96 |
}, |
| 97 |
relationships: { |
| 98 |
posts: { |
| 99 |
data: [], |
| 100 |
}, |
| 101 |
}, |
| 102 |
}, |
| 103 |
], |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
test(`when shouldIncludeLinkageData returns true for a relationship, it contains linkage data for that relationship on all of the collection, regardless of includes`, () => { |
| 108 |
server.config({ |
| 109 |
serializers: { |
| 110 |
application: JSONAPISerializer.extend({ |
| 111 |
shouldIncludeLinkageData(relationshipName, model) { |
| 112 |
if (relationshipName == "posts") { |
| 113 |
return true; |
| 114 |
} |
| 115 |
}, |
| 116 |
}), |
| 117 |
}, |
| 118 |
}); |
| 119 |
server.schema.wordSmiths.create({ firstName: "Link", age: 123 }); |
| 120 |
server.schema.wordSmiths.create({ firstName: "Zelda", age: 456 }); |
| 121 |
|
| 122 |
let collection = server.schema.wordSmiths.all(); |
| 123 |
let result = server.serializerOrRegistry.serialize(collection); |
| 124 |
|
| 125 |
expect(result).toEqual({ |
| 126 |
data: [ |
| 127 |
{ |
| 128 |
type: "word-smiths", |
| 129 |
id: "1", |
| 130 |
attributes: { |
| 131 |
"first-name": "Link", |
| 132 |
age: 123, |
| 133 |
}, |
| 134 |
relationships: { |
| 135 |
posts: { |
| 136 |
data: [], |
| 137 |
}, |
| 138 |
}, |
| 139 |
}, |
| 140 |
{ |
| 141 |
type: "word-smiths", |
| 142 |
id: "2", |
| 143 |
attributes: { |
| 144 |
"first-name": "Zelda", |
| 145 |
age: 456, |
| 146 |
}, |
| 147 |
relationships: { |
| 148 |
posts: { |
| 149 |
data: [], |
| 150 |
}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
], |
| 154 |
}); |
| 155 |
}); |
| 156 |
|
| 157 |
test(`it includes linkage data for a has-many relationship that's being included`, () => { |
| 158 |
server.config({ |
| 159 |
serializers: { |
| 160 |
application: JSONAPISerializer, |
| 161 |
wordSmith: JSONAPISerializer.extend({ |
| 162 |
include: ["posts"], |
| 163 |
}), |
| 164 |
}, |
| 165 |
}); |
| 166 |
let link = server.schema.wordSmiths.create({ firstName: "Link" }); |
| 167 |
link.createPost({ title: "Lorem" }); |
| 168 |
link.createPost({ title: "Ipsum" }); |
| 169 |
server.schema.wordSmiths.create({ firstName: "Zelda" }); |
| 170 |
|
| 171 |
let collection = server.schema.wordSmiths.all(); |
| 172 |
let result = server.serializerOrRegistry.serialize(collection); |
| 173 |
|
| 174 |
expect(result).toEqual({ |
| 175 |
data: [ |
| 176 |
{ |
| 177 |
type: "word-smiths", |
| 178 |
id: "1", |
| 179 |
attributes: { |
| 180 |
"first-name": "Link", |
| 181 |
}, |
| 182 |
relationships: { |
| 183 |
posts: { |
| 184 |
data: [ |
| 185 |
{ type: "blog-posts", id: "1" }, |
| 186 |
{ type: "blog-posts", id: "2" }, |
| 187 |
], |
| 188 |
}, |
| 189 |
}, |
| 190 |
}, |
| 191 |
{ |
| 192 |
type: "word-smiths", |
| 193 |
id: "2", |
| 194 |
attributes: { |
| 195 |
"first-name": "Zelda", |
| 196 |
}, |
| 197 |
relationships: { |
| 198 |
posts: { |
| 199 |
data: [], |
| 200 |
}, |
| 201 |
}, |
| 202 |
}, |
| 203 |
], |
| 204 |
included: [ |
| 205 |
{ |
| 206 |
type: "blog-posts", |
| 207 |
id: "1", |
| 208 |
attributes: { |
| 209 |
title: "Lorem", |
| 210 |
}, |
| 211 |
}, |
| 212 |
{ |
| 213 |
type: "blog-posts", |
| 214 |
id: "2", |
| 215 |
attributes: { |
| 216 |
title: "Ipsum", |
| 217 |
}, |
| 218 |
}, |
| 219 |
], |
| 220 |
}); |
| 221 |
}); |
| 222 |
|
| 223 |
test(`it can serialize a collection with a chain of has-many relationships`, () => { |
| 224 |
server.config({ |
| 225 |
serializers: { |
| 226 |
application: JSONAPISerializer, |
| 227 |
wordSmith: JSONAPISerializer.extend({ |
| 228 |
include: ["posts"], |
| 229 |
}), |
| 230 |
blogPost: JSONAPISerializer.extend({ |
| 231 |
include: ["comments"], |
| 232 |
}), |
| 233 |
}, |
| 234 |
}); |
| 235 |
|
| 236 |
let link = server.schema.wordSmiths.create({ firstName: "Link" }); |
| 237 |
let lorem = link.createPost({ title: "Lorem" }); |
| 238 |
lorem.createComment({ text: "pwned" }); |
| 239 |
link.createPost({ title: "Ipsum" }); |
| 240 |
server.schema.wordSmiths.create({ firstName: "Zelda" }); |
| 241 |
|
| 242 |
let collection = server.schema.wordSmiths.all(); |
| 243 |
let result = server.serializerOrRegistry.serialize(collection); |
| 244 |
|
| 245 |
expect(result).toEqual({ |
| 246 |
data: [ |
| 247 |
{ |
| 248 |
type: "word-smiths", |
| 249 |
id: "1", |
| 250 |
attributes: { |
| 251 |
"first-name": "Link", |
| 252 |
}, |
| 253 |
relationships: { |
| 254 |
posts: { |
| 255 |
data: [ |
| 256 |
{ type: "blog-posts", id: "1" }, |
| 257 |
{ type: "blog-posts", id: "2" }, |
| 258 |
], |
| 259 |
}, |
| 260 |
}, |
| 261 |
}, |
| 262 |
{ |
| 263 |
type: "word-smiths", |
| 264 |
id: "2", |
| 265 |
attributes: { |
| 266 |
"first-name": "Zelda", |
| 267 |
}, |
| 268 |
relationships: { |
| 269 |
posts: { |
| 270 |
data: [], |
| 271 |
}, |
| 272 |
}, |
| 273 |
}, |
| 274 |
], |
| 275 |
included: [ |
| 276 |
{ |
| 277 |
type: "blog-posts", |
| 278 |
id: "1", |
| 279 |
attributes: { |
| 280 |
title: "Lorem", |
| 281 |
}, |
| 282 |
relationships: { |
| 283 |
comments: { |
| 284 |
data: [{ type: "fine-comments", id: "1" }], |
| 285 |
}, |
| 286 |
}, |
| 287 |
}, |
| 288 |
{ |
| 289 |
type: "fine-comments", |
| 290 |
id: "1", |
| 291 |
attributes: { |
| 292 |
text: "pwned", |
| 293 |
}, |
| 294 |
}, |
| 295 |
{ |
| 296 |
type: "blog-posts", |
| 297 |
id: "2", |
| 298 |
attributes: { |
| 299 |
title: "Ipsum", |
| 300 |
}, |
| 301 |
relationships: { |
| 302 |
comments: { |
| 303 |
data: [], |
| 304 |
}, |
| 305 |
}, |
| 306 |
}, |
| 307 |
], |
| 308 |
}); |
| 309 |
}); |
| 310 |
|
| 311 |
test(`it can serialize a collection with a belongs-to relationship`, () => { |
| 312 |
server.config({ |
| 313 |
serializers: { |
| 314 |
application: JSONAPISerializer, |
| 315 |
blogPost: JSONAPISerializer.extend({ |
| 316 |
include: ["author"], |
| 317 |
}), |
| 318 |
}, |
| 319 |
}); |
| 320 |
|
| 321 |
let link = server.schema.wordSmiths.create({ firstName: "Link" }); |
| 322 |
let post = link.createPost({ title: "Lorem" }); |
| 323 |
post.createComment(); |
| 324 |
link.createPost({ title: "Ipsum" }); |
| 325 |
server.schema.wordSmiths.create({ firstName: "Zelda" }); |
| 326 |
|
| 327 |
let blogPosts = server.schema.blogPosts.all(); |
| 328 |
let result = server.serializerOrRegistry.serialize(blogPosts); |
| 329 |
|
| 330 |
expect(result).toEqual({ |
| 331 |
data: [ |
| 332 |
{ |
| 333 |
type: "blog-posts", |
| 334 |
id: "1", |
| 335 |
attributes: { |
| 336 |
title: "Lorem", |
| 337 |
}, |
| 338 |
relationships: { |
| 339 |
author: { |
| 340 |
data: { type: "word-smiths", id: "1" }, |
| 341 |
}, |
| 342 |
}, |
| 343 |
}, |
| 344 |
{ |
| 345 |
type: "blog-posts", |
| 346 |
id: "2", |
| 347 |
attributes: { |
| 348 |
title: "Ipsum", |
| 349 |
}, |
| 350 |
relationships: { |
| 351 |
author: { |
| 352 |
data: { type: "word-smiths", id: "1" }, |
| 353 |
}, |
| 354 |
}, |
| 355 |
}, |
| 356 |
], |
| 357 |
included: [ |
| 358 |
{ |
| 359 |
type: "word-smiths", |
| 360 |
id: "1", |
| 361 |
attributes: { |
| 362 |
"first-name": "Link", |
| 363 |
}, |
| 364 |
}, |
| 365 |
], |
| 366 |
}); |
| 367 |
}); |
| 368 |
|
| 369 |
test(`it can serialize a collection with a chain of belongs-to relationships`, () => { |
| 370 |
server.config({ |
| 371 |
serializers: { |
| 372 |
application: JSONAPISerializer, |
| 373 |
fineComment: JSONAPISerializer.extend({ |
| 374 |
include: ["post"], |
| 375 |
}), |
| 376 |
blogPost: JSONAPISerializer.extend({ |
| 377 |
include: ["author"], |
| 378 |
}), |
| 379 |
}, |
| 380 |
}); |
| 381 |
|
| 382 |
let link = server.schema.wordSmiths.create({ firstName: "Link" }); |
| 383 |
let post = link.createPost({ title: "Lorem" }); |
| 384 |
post.createComment({ text: "pwned" }); |
| 385 |
link.createPost({ title: "Ipsum" }); |
| 386 |
server.schema.wordSmiths.create({ firstName: "Zelda" }); |
| 387 |
|
| 388 |
let fineComments = server.schema.fineComments.all(); |
| 389 |
let result = server.serializerOrRegistry.serialize(fineComments); |
| 390 |
|
| 391 |
expect(result).toEqual({ |
| 392 |
data: [ |
| 393 |
{ |
| 394 |
type: "fine-comments", |
| 395 |
id: "1", |
| 396 |
attributes: { |
| 397 |
text: "pwned", |
| 398 |
}, |
| 399 |
relationships: { |
| 400 |
post: { |
| 401 |
data: { type: "blog-posts", id: "1" }, |
| 402 |
}, |
| 403 |
}, |
| 404 |
}, |
| 405 |
], |
| 406 |
included: [ |
| 407 |
{ |
| 408 |
type: "blog-posts", |
| 409 |
id: "1", |
| 410 |
attributes: { |
| 411 |
title: "Lorem", |
| 412 |
}, |
| 413 |
relationships: { |
| 414 |
author: { |
| 415 |
data: { type: "word-smiths", id: "1" }, |
| 416 |
}, |
| 417 |
}, |
| 418 |
}, |
| 419 |
{ |
| 420 |
type: "word-smiths", |
| 421 |
id: "1", |
| 422 |
attributes: { |
| 423 |
"first-name": "Link", |
| 424 |
}, |
| 425 |
}, |
| 426 |
], |
| 427 |
}); |
| 428 |
}); |
| 429 |
|
| 430 |
test(`it propertly serializes complex relationships`, () => { |
| 431 |
server.config({ |
| 432 |
serializers: { |
| 433 |
application: JSONAPISerializer, |
| 434 |
blogPost: JSONAPISerializer.extend({ |
| 435 |
include: ["author", "comments"], |
| 436 |
}), |
| 437 |
wordSmith: JSONAPISerializer.extend({ |
| 438 |
include: ["posts"], |
| 439 |
}), |
| 440 |
fineComment: JSONAPISerializer.extend({ |
| 441 |
include: ["post"], |
| 442 |
}), |
| 443 |
}, |
| 444 |
}); |
| 445 |
|
| 446 |
let link = server.schema.wordSmiths.create({ firstName: "Link" }); |
| 447 |
let post = link.createPost({ title: "Lorem" }); |
| 448 |
post.createComment({ text: "pwned" }); |
| 449 |
link.createPost({ title: "Ipsum" }); |
| 450 |
server.schema.wordSmiths.create({ firstName: "Zelda" }); |
| 451 |
|
| 452 |
let blogPost = server.schema.blogPosts.find(1); |
| 453 |
let result = server.serializerOrRegistry.serialize(blogPost); |
| 454 |
|
| 455 |
expect(result).toEqual({ |
| 456 |
data: { |
| 457 |
type: "blog-posts", |
| 458 |
id: "1", |
| 459 |
attributes: { |
| 460 |
title: "Lorem", |
| 461 |
}, |
| 462 |
relationships: { |
| 463 |
author: { |
| 464 |
data: { type: "word-smiths", id: "1" }, |
| 465 |
}, |
| 466 |
comments: { |
| 467 |
data: [{ type: "fine-comments", id: "1" }], |
| 468 |
}, |
| 469 |
}, |
| 470 |
}, |
| 471 |
included: [ |
| 472 |
{ |
| 473 |
type: "word-smiths", |
| 474 |
id: "1", |
| 475 |
attributes: { |
| 476 |
"first-name": "Link", |
| 477 |
}, |
| 478 |
relationships: { |
| 479 |
posts: { |
| 480 |
data: [ |
| 481 |
{ |
| 482 |
id: "1", |
| 483 |
type: "blog-posts", |
| 484 |
}, |
| 485 |
{ |
| 486 |
id: "2", |
| 487 |
type: "blog-posts", |
| 488 |
}, |
| 489 |
], |
| 490 |
}, |
| 491 |
}, |
| 492 |
}, |
| 493 |
{ |
| 494 |
type: "blog-posts", |
| 495 |
id: "2", |
| 496 |
attributes: { |
| 497 |
title: "Ipsum", |
| 498 |
}, |
| 499 |
relationships: { |
| 500 |
author: { |
| 501 |
data: { type: "word-smiths", id: "1" }, |
| 502 |
}, |
| 503 |
comments: { |
| 504 |
data: [], |
| 505 |
}, |
| 506 |
}, |
| 507 |
}, |
| 508 |
{ |
| 509 |
type: "fine-comments", |
| 510 |
id: "1", |
| 511 |
attributes: { |
| 512 |
text: "pwned", |
| 513 |
}, |
| 514 |
relationships: { |
| 515 |
post: { |
| 516 |
data: { |
| 517 |
id: "1", |
| 518 |
type: "blog-posts", |
| 519 |
}, |
| 520 |
}, |
| 521 |
}, |
| 522 |
}, |
| 523 |
], |
| 524 |
}); |
| 525 |
}); |
| 526 |
}); |