| 1 |
import { Server, Serializer, Model, hasMany } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Serialize ids", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
wordSmith: Model.extend({ |
| 10 |
blogPosts: hasMany(), |
| 11 |
specialPosts: hasMany("blog-post", { inverse: "specialAuthor" }), |
| 12 |
}), |
| 13 |
blogPost: Model, |
| 14 |
}, |
| 15 |
}); |
| 16 |
}); |
| 17 |
|
| 18 |
afterEach(function () { |
| 19 |
server.shutdown(); |
| 20 |
}); |
| 21 |
|
| 22 |
test(`if serializeIds is 'include' it serializes ids of hasMany associations that are included`, () => { |
| 23 |
let ApplicationSerializer = Serializer.extend({ |
| 24 |
serializeIds: "included", |
| 25 |
}); |
| 26 |
server.config({ |
| 27 |
serializers: { |
| 28 |
application: ApplicationSerializer, |
| 29 |
wordSmith: ApplicationSerializer.extend({ |
| 30 |
include: ["blogPosts"], |
| 31 |
}), |
| 32 |
}, |
| 33 |
}); |
| 34 |
|
| 35 |
let wordSmith = server.schema.wordSmiths.create({ |
| 36 |
id: 1, |
| 37 |
name: "Link", |
| 38 |
}); |
| 39 |
wordSmith.createBlogPost(); |
| 40 |
wordSmith.createBlogPost(); |
| 41 |
wordSmith.createSpecialPost(); |
| 42 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 43 |
|
| 44 |
expect(result).toEqual({ |
| 45 |
wordSmith: { |
| 46 |
id: "1", |
| 47 |
name: "Link", |
| 48 |
blogPostIds: ["1", "2"], |
| 49 |
}, |
| 50 |
blogPosts: [{ id: "1" }, { id: "2" }], |
| 51 |
}); |
| 52 |
}); |
| 53 |
|
| 54 |
test(`if serializeIds is 'always' it serializes ids of all hasMany associations`, () => { |
| 55 |
server.config({ |
| 56 |
serializers: { |
| 57 |
application: Serializer.extend({ |
| 58 |
serializeIds: "always", |
| 59 |
}), |
| 60 |
}, |
| 61 |
}); |
| 62 |
|
| 63 |
let wordSmith = server.schema.wordSmiths.create({ |
| 64 |
id: 1, |
| 65 |
name: "Link", |
| 66 |
}); |
| 67 |
wordSmith.createBlogPost(); |
| 68 |
wordSmith.createBlogPost(); |
| 69 |
wordSmith.createSpecialPost(); |
| 70 |
let result = server.serializerOrRegistry.serialize(wordSmith); |
| 71 |
|
| 72 |
expect(result).toEqual({ |
| 73 |
wordSmith: { |
| 74 |
id: "1", |
| 75 |
name: "Link", |
| 76 |
blogPostIds: ["1", "2"], |
| 77 |
specialPostIds: ["3"], |
| 78 |
}, |
| 79 |
}); |
| 80 |
}); |
| 81 |
}); |