| 1 |
import { Server, Model, hasMany, Serializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Associations | Sideloading Assorted Collections", function () { |
| 4 |
let server, wordSmiths, blogPosts, greatPhotos; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
let BaseSerializer = Serializer.extend({ |
| 8 |
embed: false, |
| 9 |
}); |
| 10 |
|
| 11 |
server = new Server({ |
| 12 |
models: { |
| 13 |
wordSmith: Model.extend({ |
| 14 |
blogPosts: hasMany(), |
| 15 |
}), |
| 16 |
blogPost: Model, |
| 17 |
greatPhoto: Model, |
| 18 |
}, |
| 19 |
serializers: { |
| 20 |
application: BaseSerializer, |
| 21 |
wordSmith: BaseSerializer.extend({ |
| 22 |
include: ["blogPosts"], |
| 23 |
}), |
| 24 |
greatPhoto: BaseSerializer.extend({ |
| 25 |
attrs: ["id", "title"], |
| 26 |
}), |
| 27 |
}, |
| 28 |
}); |
| 29 |
|
| 30 |
wordSmiths = [ |
| 31 |
{ id: "1", name: "Link", blogPostIds: ["1", "2"] }, |
| 32 |
{ id: "2", name: "Zelda", blogPostIds: [] }, |
| 33 |
{ id: "3", name: "Epona", blogPostIds: [] }, |
| 34 |
]; |
| 35 |
blogPosts = [ |
| 36 |
{ id: "1", title: "Lorem" }, |
| 37 |
{ id: "2", title: "Ipsum" }, |
| 38 |
]; |
| 39 |
greatPhotos = [ |
| 40 |
{ id: "1", title: "Amazing", location: "Hyrule" }, |
| 41 |
{ id: "2", title: "greatPhoto", location: "Goron City" }, |
| 42 |
]; |
| 43 |
|
| 44 |
server.db.loadData({ |
| 45 |
wordSmiths: wordSmiths, |
| 46 |
blogPosts: blogPosts, |
| 47 |
greatPhotos: greatPhotos, |
| 48 |
}); |
| 49 |
}); |
| 50 |
|
| 51 |
afterEach(function () { |
| 52 |
server.shutdown(); |
| 53 |
}); |
| 54 |
|
| 55 |
/* |
| 56 |
This is a strange response from a route handler, but it's used in the array get shorthand. Deprecate that shorthand? |
| 57 |
*/ |
| 58 |
test(`it can sideload an array of assorted collections that have relationships`, () => { |
| 59 |
let result = server.serializerOrRegistry.serialize([ |
| 60 |
server.schema.wordSmiths.all(), |
| 61 |
server.schema.greatPhotos.all(), |
| 62 |
]); |
| 63 |
|
| 64 |
expect(result).toEqual({ |
| 65 |
wordSmiths: wordSmiths, |
| 66 |
blogPosts: blogPosts, |
| 67 |
greatPhotos: greatPhotos.map((attrs) => { |
| 68 |
delete attrs.location; |
| 69 |
return attrs; |
| 70 |
}), |
| 71 |
}); |
| 72 |
}); |
| 73 |
}); |