| 1 |
import { Server, JSONAPISerializer, Model, hasMany, belongsTo } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | JSON API Serializer | Associations | Includes", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
wordSmith: Model.extend({ |
| 10 |
blogPosts: hasMany(), |
| 11 |
}), |
| 12 |
|
| 13 |
blogPost: Model.extend({ |
| 14 |
wordSmith: belongsTo(), |
| 15 |
fineComments: hasMany(), |
| 16 |
}), |
| 17 |
|
| 18 |
fineComment: Model.extend({ |
| 19 |
blogPost: belongsTo(), |
| 20 |
category: belongsTo(), |
| 21 |
}), |
| 22 |
|
| 23 |
category: Model.extend({ |
| 24 |
labels: hasMany(), |
| 25 |
}), |
| 26 |
|
| 27 |
label: Model.extend({}), |
| 28 |
}, |
| 29 |
}); |
| 30 |
}); |
| 31 |
|
| 32 |
afterEach(() => { |
| 33 |
server.shutdown(); |
| 34 |
}); |
| 35 |
|
| 36 |
test("includes get serialized with correct serializer", () => { |
| 37 |
server.config({ |
| 38 |
serializers: { |
| 39 |
application: JSONAPISerializer, |
| 40 |
blogPost: JSONAPISerializer.extend({ |
| 41 |
attrs: ["title"], |
| 42 |
include: ["wordSmith"], |
| 43 |
}), |
| 44 |
wordSmith: JSONAPISerializer.extend({ |
| 45 |
attrs: ["firstName"], |
| 46 |
}), |
| 47 |
}, |
| 48 |
}); |
| 49 |
|
| 50 |
let post = server.schema.blogPosts.create({ title: "We love Mirage!" }); |
| 51 |
post.createWordSmith({ firstName: "Sam" }); |
| 52 |
|
| 53 |
let result = server.serializerOrRegistry.serialize(post); |
| 54 |
|
| 55 |
expect(result).toEqual({ |
| 56 |
data: { |
| 57 |
type: "blog-posts", |
| 58 |
id: "1", |
| 59 |
attributes: { |
| 60 |
title: "We love Mirage!", |
| 61 |
}, |
| 62 |
relationships: { |
| 63 |
"word-smith": { |
| 64 |
data: { type: "word-smiths", id: "1" }, |
| 65 |
}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
included: [ |
| 69 |
{ |
| 70 |
type: "word-smiths", |
| 71 |
id: "1", |
| 72 |
attributes: { |
| 73 |
"first-name": "Sam", |
| 74 |
}, |
| 75 |
}, |
| 76 |
], |
| 77 |
}); |
| 78 |
}); |
| 79 |
|
| 80 |
test("includes can be a function", () => { |
| 81 |
server.config({ |
| 82 |
serializers: { |
| 83 |
application: JSONAPISerializer, |
| 84 |
blogPost: JSONAPISerializer.extend({ |
| 85 |
attrs: ["title"], |
| 86 |
include() { |
| 87 |
return ["wordSmith"]; |
| 88 |
}, |
| 89 |
}), |
| 90 |
wordSmith: JSONAPISerializer.extend({ |
| 91 |
attrs: ["firstName"], |
| 92 |
}), |
| 93 |
}, |
| 94 |
}); |
| 95 |
|
| 96 |
let post = server.schema.blogPosts.create({ title: "We love Mirage!" }); |
| 97 |
post.createWordSmith({ firstName: "Sam" }); |
| 98 |
|
| 99 |
let result = server.serializerOrRegistry.serialize(post); |
| 100 |
|
| 101 |
expect(result).toEqual({ |
| 102 |
data: { |
| 103 |
type: "blog-posts", |
| 104 |
id: "1", |
| 105 |
attributes: { |
| 106 |
title: "We love Mirage!", |
| 107 |
}, |
| 108 |
relationships: { |
| 109 |
"word-smith": { |
| 110 |
data: { type: "word-smiths", id: "1" }, |
| 111 |
}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
included: [ |
| 115 |
{ |
| 116 |
type: "word-smiths", |
| 117 |
id: "1", |
| 118 |
attributes: { |
| 119 |
"first-name": "Sam", |
| 120 |
}, |
| 121 |
}, |
| 122 |
], |
| 123 |
}); |
| 124 |
}); |
| 125 |
|
| 126 |
test("query param includes work when serializing a model", () => { |
| 127 |
server.config({ |
| 128 |
serializers: { |
| 129 |
application: JSONAPISerializer, |
| 130 |
}, |
| 131 |
}); |
| 132 |
|
| 133 |
let post = server.schema.blogPosts.create(); |
| 134 |
post.createWordSmith(); |
| 135 |
post.createFineComment(); |
| 136 |
post.createFineComment(); |
| 137 |
|
| 138 |
let request = { |
| 139 |
queryParams: { |
| 140 |
include: "word-smith,fine-comments", |
| 141 |
}, |
| 142 |
}; |
| 143 |
|
| 144 |
let result = server.serializerOrRegistry.serialize(post, request); |
| 145 |
|
| 146 |
expect(result).toEqual({ |
| 147 |
data: { |
| 148 |
type: "blog-posts", |
| 149 |
id: "1", |
| 150 |
attributes: {}, |
| 151 |
relationships: { |
| 152 |
"word-smith": { |
| 153 |
data: { type: "word-smiths", id: "1" }, |
| 154 |
}, |
| 155 |
"fine-comments": { |
| 156 |
data: [ |
| 157 |
{ type: "fine-comments", id: "1" }, |
| 158 |
{ type: "fine-comments", id: "2" }, |
| 159 |
], |
| 160 |
}, |
| 161 |
}, |
| 162 |
}, |
| 163 |
included: [ |
| 164 |
{ |
| 165 |
type: "word-smiths", |
| 166 |
id: "1", |
| 167 |
attributes: {}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
type: "fine-comments", |
| 171 |
id: "1", |
| 172 |
attributes: {}, |
| 173 |
}, |
| 174 |
{ |
| 175 |
type: "fine-comments", |
| 176 |
id: "2", |
| 177 |
attributes: {}, |
| 178 |
}, |
| 179 |
], |
| 180 |
}); |
| 181 |
}); |
| 182 |
|
| 183 |
test("query param array includes work when serializing a model", () => { |
| 184 |
server.config({ |
| 185 |
serializers: { |
| 186 |
application: JSONAPISerializer, |
| 187 |
}, |
| 188 |
}); |
| 189 |
|
| 190 |
let post = server.schema.blogPosts.create(); |
| 191 |
post.createWordSmith(); |
| 192 |
post.createFineComment(); |
| 193 |
post.createFineComment(); |
| 194 |
|
| 195 |
let request = { |
| 196 |
queryParams: { |
| 197 |
include: ["word-smith", "fine-comments"], |
| 198 |
}, |
| 199 |
}; |
| 200 |
|
| 201 |
let result = server.serializerOrRegistry.serialize(post, request); |
| 202 |
|
| 203 |
expect(result).toEqual({ |
| 204 |
data: { |
| 205 |
type: "blog-posts", |
| 206 |
id: "1", |
| 207 |
attributes: {}, |
| 208 |
relationships: { |
| 209 |
"word-smith": { |
| 210 |
data: { type: "word-smiths", id: "1" }, |
| 211 |
}, |
| 212 |
"fine-comments": { |
| 213 |
data: [ |
| 214 |
{ type: "fine-comments", id: "1" }, |
| 215 |
{ type: "fine-comments", id: "2" }, |
| 216 |
], |
| 217 |
}, |
| 218 |
}, |
| 219 |
}, |
| 220 |
included: [ |
| 221 |
{ |
| 222 |
type: "word-smiths", |
| 223 |
id: "1", |
| 224 |
attributes: {}, |
| 225 |
}, |
| 226 |
{ |
| 227 |
type: "fine-comments", |
| 228 |
id: "1", |
| 229 |
attributes: {}, |
| 230 |
}, |
| 231 |
{ |
| 232 |
type: "fine-comments", |
| 233 |
id: "2", |
| 234 |
attributes: {}, |
| 235 |
}, |
| 236 |
], |
| 237 |
}); |
| 238 |
}); |
| 239 |
|
| 240 |
test("query param includes work when serializing a collection", () => { |
| 241 |
server.config({ |
| 242 |
serializers: { |
| 243 |
application: JSONAPISerializer, |
| 244 |
}, |
| 245 |
}); |
| 246 |
|
| 247 |
let post1 = server.schema.blogPosts.create(); |
| 248 |
post1.createWordSmith(); |
| 249 |
post1.createFineComment(); |
| 250 |
post1.createFineComment(); |
| 251 |
server.schema.blogPosts.create(); |
| 252 |
|
| 253 |
let request = { |
| 254 |
queryParams: { |
| 255 |
include: "word-smith,fine-comments", |
| 256 |
}, |
| 257 |
}; |
| 258 |
|
| 259 |
let result = server.serializerOrRegistry.serialize( |
| 260 |
server.schema.blogPosts.all(), |
| 261 |
request |
| 262 |
); |
| 263 |
|
| 264 |
expect(result).toEqual({ |
| 265 |
data: [ |
| 266 |
{ |
| 267 |
type: "blog-posts", |
| 268 |
id: "1", |
| 269 |
attributes: {}, |
| 270 |
relationships: { |
| 271 |
"word-smith": { |
| 272 |
data: { type: "word-smiths", id: "1" }, |
| 273 |
}, |
| 274 |
"fine-comments": { |
| 275 |
data: [ |
| 276 |
{ type: "fine-comments", id: "1" }, |
| 277 |
{ type: "fine-comments", id: "2" }, |
| 278 |
], |
| 279 |
}, |
| 280 |
}, |
| 281 |
}, |
| 282 |
{ |
| 283 |
type: "blog-posts", |
| 284 |
id: "2", |
| 285 |
attributes: {}, |
| 286 |
relationships: { |
| 287 |
"word-smith": { |
| 288 |
data: null, |
| 289 |
}, |
| 290 |
"fine-comments": { |
| 291 |
data: [], |
| 292 |
}, |
| 293 |
}, |
| 294 |
}, |
| 295 |
], |
| 296 |
included: [ |
| 297 |
{ |
| 298 |
type: "word-smiths", |
| 299 |
id: "1", |
| 300 |
attributes: {}, |
| 301 |
}, |
| 302 |
{ |
| 303 |
type: "fine-comments", |
| 304 |
id: "1", |
| 305 |
attributes: {}, |
| 306 |
}, |
| 307 |
{ |
| 308 |
type: "fine-comments", |
| 309 |
id: "2", |
| 310 |
attributes: {}, |
| 311 |
}, |
| 312 |
], |
| 313 |
}); |
| 314 |
}); |
| 315 |
|
| 316 |
test("query param includes take precedence over default server includes", () => { |
| 317 |
server.config({ |
| 318 |
serializers: { |
| 319 |
application: JSONAPISerializer, |
| 320 |
blogPost: JSONAPISerializer.extend({ |
| 321 |
include: ["wordSmith"], |
| 322 |
}), |
| 323 |
}, |
| 324 |
}); |
| 325 |
|
| 326 |
let post = server.schema.blogPosts.create(); |
| 327 |
post.createWordSmith(); |
| 328 |
post.createFineComment(); |
| 329 |
post.createFineComment(); |
| 330 |
|
| 331 |
let request = { |
| 332 |
queryParams: { |
| 333 |
include: "fine-comments", |
| 334 |
}, |
| 335 |
}; |
| 336 |
|
| 337 |
let result = server.serializerOrRegistry.serialize(post, request); |
| 338 |
|
| 339 |
expect(result).toEqual({ |
| 340 |
data: { |
| 341 |
type: "blog-posts", |
| 342 |
id: "1", |
| 343 |
attributes: {}, |
| 344 |
relationships: { |
| 345 |
"fine-comments": { |
| 346 |
data: [ |
| 347 |
{ type: "fine-comments", id: "1" }, |
| 348 |
{ type: "fine-comments", id: "2" }, |
| 349 |
], |
| 350 |
}, |
| 351 |
}, |
| 352 |
}, |
| 353 |
included: [ |
| 354 |
{ |
| 355 |
type: "fine-comments", |
| 356 |
id: "1", |
| 357 |
attributes: {}, |
| 358 |
}, |
| 359 |
{ |
| 360 |
type: "fine-comments", |
| 361 |
id: "2", |
| 362 |
attributes: {}, |
| 363 |
}, |
| 364 |
], |
| 365 |
}); |
| 366 |
}); |
| 367 |
|
| 368 |
test("query param includes support dot-paths when serializing a model", () => { |
| 369 |
server.config({ |
| 370 |
serializers: { |
| 371 |
application: JSONAPISerializer, |
| 372 |
}, |
| 373 |
}); |
| 374 |
|
| 375 |
server.schema.db.loadData({ |
| 376 |
wordSmiths: [{ id: 1, name: "Sam", blogPostIds: [2] }], |
| 377 |
blogPosts: [ |
| 378 |
{ id: 2, wordSmithId: 1, fineCommentIds: [3], title: "Lorem Ipsum" }, |
| 379 |
], |
| 380 |
fineComments: [{ id: 3, text: "Foo", blogPostId: 2, categoryId: 10 }], |
| 381 |
categories: [{ id: 10, foo: "bar", labelIds: [20] }], |
| 382 |
labels: [{ id: 20, name: "Economics" }], |
| 383 |
}); |
| 384 |
let request = { |
| 385 |
queryParams: { |
| 386 |
include: "word-smith,fine-comments.category.labels", |
| 387 |
}, |
| 388 |
}; |
| 389 |
let result = server.serializerOrRegistry.serialize( |
| 390 |
server.schema.blogPosts.first(), |
| 391 |
request |
| 392 |
); |
| 393 |
|
| 394 |
expect(result).toEqual({ |
| 395 |
data: { |
| 396 |
type: "blog-posts", |
| 397 |
id: "2", |
| 398 |
attributes: { |
| 399 |
title: "Lorem Ipsum", |
| 400 |
}, |
| 401 |
relationships: { |
| 402 |
"word-smith": { |
| 403 |
data: { type: "word-smiths", id: "1" }, |
| 404 |
}, |
| 405 |
"fine-comments": { |
| 406 |
data: [{ type: "fine-comments", id: "3" }], |
| 407 |
}, |
| 408 |
}, |
| 409 |
}, |
| 410 |
included: [ |
| 411 |
{ |
| 412 |
type: "word-smiths", |
| 413 |
id: "1", |
| 414 |
attributes: { |
| 415 |
name: "Sam", |
| 416 |
}, |
| 417 |
}, |
| 418 |
{ |
| 419 |
type: "fine-comments", |
| 420 |
id: "3", |
| 421 |
attributes: { |
| 422 |
text: "Foo", |
| 423 |
}, |
| 424 |
relationships: { |
| 425 |
category: { |
| 426 |
data: { type: "categories", id: "10" }, |
| 427 |
}, |
| 428 |
}, |
| 429 |
}, |
| 430 |
{ |
| 431 |
type: "categories", |
| 432 |
id: "10", |
| 433 |
attributes: { |
| 434 |
foo: "bar", |
| 435 |
}, |
| 436 |
relationships: { |
| 437 |
labels: { |
| 438 |
data: [{ type: "labels", id: "20" }], |
| 439 |
}, |
| 440 |
}, |
| 441 |
}, |
| 442 |
{ |
| 443 |
type: "labels", |
| 444 |
id: "20", |
| 445 |
attributes: { |
| 446 |
name: "Economics", |
| 447 |
}, |
| 448 |
}, |
| 449 |
], |
| 450 |
}); |
| 451 |
}); |
| 452 |
|
| 453 |
test("query param includes support dot-paths when serializing a collection", () => { |
| 454 |
server.config({ |
| 455 |
serializers: { |
| 456 |
application: JSONAPISerializer, |
| 457 |
}, |
| 458 |
}); |
| 459 |
|
| 460 |
server.schema.db.loadData({ |
| 461 |
wordSmiths: [{ id: 1, name: "Sam", blogPostIds: [2, 5] }], |
| 462 |
blogPosts: [ |
| 463 |
{ id: 2, wordSmithId: 1, fineCommentIds: [3], title: "Lorem Ipsum" }, |
| 464 |
{ id: 5, wordSmithId: 1, title: "Dolor" }, |
| 465 |
], |
| 466 |
fineComments: [{ id: 3, text: "Foo", blogPostId: 2, categoryId: 10 }], |
| 467 |
categories: [{ id: 10, foo: "bar", labelIds: [20] }], |
| 468 |
labels: [{ id: 20, name: "Economics" }], |
| 469 |
}); |
| 470 |
let request = { |
| 471 |
queryParams: { |
| 472 |
include: "word-smith,fine-comments.category.labels", |
| 473 |
}, |
| 474 |
}; |
| 475 |
let result = server.serializerOrRegistry.serialize( |
| 476 |
server.schema.blogPosts.all(), |
| 477 |
request |
| 478 |
); |
| 479 |
|
| 480 |
expect(result).toEqual({ |
| 481 |
data: [ |
| 482 |
{ |
| 483 |
type: "blog-posts", |
| 484 |
id: "2", |
| 485 |
attributes: { |
| 486 |
title: "Lorem Ipsum", |
| 487 |
}, |
| 488 |
relationships: { |
| 489 |
"word-smith": { |
| 490 |
data: { type: "word-smiths", id: "1" }, |
| 491 |
}, |
| 492 |
"fine-comments": { |
| 493 |
data: [{ type: "fine-comments", id: "3" }], |
| 494 |
}, |
| 495 |
}, |
| 496 |
}, |
| 497 |
{ |
| 498 |
type: "blog-posts", |
| 499 |
id: "5", |
| 500 |
attributes: { |
| 501 |
title: "Dolor", |
| 502 |
}, |
| 503 |
relationships: { |
| 504 |
"word-smith": { |
| 505 |
data: { type: "word-smiths", id: "1" }, |
| 506 |
}, |
| 507 |
"fine-comments": { |
| 508 |
data: [], |
| 509 |
}, |
| 510 |
}, |
| 511 |
}, |
| 512 |
], |
| 513 |
included: [ |
| 514 |
{ |
| 515 |
type: "word-smiths", |
| 516 |
id: "1", |
| 517 |
attributes: { |
| 518 |
name: "Sam", |
| 519 |
}, |
| 520 |
}, |
| 521 |
{ |
| 522 |
type: "fine-comments", |
| 523 |
id: "3", |
| 524 |
attributes: { |
| 525 |
text: "Foo", |
| 526 |
}, |
| 527 |
relationships: { |
| 528 |
category: { |
| 529 |
data: { type: "categories", id: "10" }, |
| 530 |
}, |
| 531 |
}, |
| 532 |
}, |
| 533 |
{ |
| 534 |
type: "categories", |
| 535 |
id: "10", |
| 536 |
attributes: { |
| 537 |
foo: "bar", |
| 538 |
}, |
| 539 |
relationships: { |
| 540 |
labels: { |
| 541 |
data: [{ type: "labels", id: "20" }], |
| 542 |
}, |
| 543 |
}, |
| 544 |
}, |
| 545 |
{ |
| 546 |
type: "labels", |
| 547 |
id: "20", |
| 548 |
attributes: { |
| 549 |
name: "Economics", |
| 550 |
}, |
| 551 |
}, |
| 552 |
], |
| 553 |
}); |
| 554 |
}); |
| 555 |
|
| 556 |
test("queryParamIncludes throws if including something that is not an association", () => { |
| 557 |
server.config({ |
| 558 |
serializers: { |
| 559 |
application: JSONAPISerializer, |
| 560 |
}, |
| 561 |
}); |
| 562 |
|
| 563 |
server.schema.db.loadData({ |
| 564 |
blogPosts: [{ id: 2, title: "Lorem Ipsum" }], |
| 565 |
}); |
| 566 |
let request = { |
| 567 |
queryParams: { |
| 568 |
include: "title", |
| 569 |
}, |
| 570 |
}; |
| 571 |
|
| 572 |
expect(() => { |
| 573 |
server.serializerOrRegistry.serialize( |
| 574 |
server.schema.blogPosts.first(), |
| 575 |
request |
| 576 |
); |
| 577 |
}).toThrow(); |
| 578 |
}); |
| 579 |
|
| 580 |
test("Do not return includes when queryParamIncludes is empty", () => { |
| 581 |
server.config({ |
| 582 |
serializers: { |
| 583 |
application: JSONAPISerializer, |
| 584 |
}, |
| 585 |
}); |
| 586 |
|
| 587 |
let post = server.schema.blogPosts.create(); |
| 588 |
|
| 589 |
let request = { |
| 590 |
queryParams: { |
| 591 |
include: " ", |
| 592 |
}, |
| 593 |
}; |
| 594 |
|
| 595 |
let result = server.serializerOrRegistry.serialize(post, request); |
| 596 |
|
| 597 |
expect(result).toEqual({ |
| 598 |
data: { |
| 599 |
type: "blog-posts", |
| 600 |
id: "1", |
| 601 |
attributes: {}, |
| 602 |
}, |
| 603 |
}); |
| 604 |
}); |
| 605 |
|
| 606 |
test("Do not throw when queryParamIncludes has a trailing comma", () => { |
| 607 |
server.config({ |
| 608 |
serializers: { |
| 609 |
application: JSONAPISerializer, |
| 610 |
}, |
| 611 |
}); |
| 612 |
|
| 613 |
let post = server.schema.blogPosts.create(); |
| 614 |
post.createWordSmith({ name: "Sam" }); |
| 615 |
|
| 616 |
let request = { |
| 617 |
queryParams: { |
| 618 |
include: "word-smith,", |
| 619 |
}, |
| 620 |
}; |
| 621 |
|
| 622 |
let result = server.serializerOrRegistry.serialize(post, request); |
| 623 |
expect(result).toEqual({ |
| 624 |
data: { |
| 625 |
type: "blog-posts", |
| 626 |
id: "1", |
| 627 |
attributes: {}, |
| 628 |
relationships: { |
| 629 |
"word-smith": { |
| 630 |
data: { type: "word-smiths", id: "1" }, |
| 631 |
}, |
| 632 |
}, |
| 633 |
}, |
| 634 |
included: [ |
| 635 |
{ |
| 636 |
type: "word-smiths", |
| 637 |
id: "1", |
| 638 |
attributes: { |
| 639 |
name: "Sam", |
| 640 |
}, |
| 641 |
}, |
| 642 |
], |
| 643 |
}); |
| 644 |
}); |
| 645 |
}); |