| 1 |
import { Server, Model, belongsTo, Serializer } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | Serializers | Base | Associations | Polymorphic | Belongs To", function () { |
| 4 |
let server, BaseSerializer; |
| 5 |
|
| 6 |
beforeEach(function () { |
| 7 |
server = new Server({ |
| 8 |
models: { |
| 9 |
post: Model.extend(), |
| 10 |
comment: Model.extend({ |
| 11 |
commentable: belongsTo({ polymorphic: true }), |
| 12 |
}), |
| 13 |
}, |
| 14 |
}); |
| 15 |
|
| 16 |
let post = server.schema.posts.create({ title: "Lorem ipsum" }); |
| 17 |
server.schema.comments.create({ commentable: post, text: "Foo" }); |
| 18 |
|
| 19 |
BaseSerializer = Serializer.extend({ |
| 20 |
embed: false, |
| 21 |
}); |
| 22 |
}); |
| 23 |
|
| 24 |
afterEach(function () { |
| 25 |
server.shutdown(); |
| 26 |
}); |
| 27 |
|
| 28 |
test(`it can serialize a polymorphic belongs-to relationship`, () => { |
| 29 |
server.config({ |
| 30 |
serializers: { |
| 31 |
application: BaseSerializer, |
| 32 |
comment: BaseSerializer.extend({ |
| 33 |
include: ["commentable"], |
| 34 |
}), |
| 35 |
}, |
| 36 |
}); |
| 37 |
|
| 38 |
let comment = server.schema.comments.find(1); |
| 39 |
let result = server.serializerOrRegistry.serialize(comment); |
| 40 |
|
| 41 |
expect(result).toEqual({ |
| 42 |
comment: { |
| 43 |
id: "1", |
| 44 |
text: "Foo", |
| 45 |
commentableType: "post", |
| 46 |
commentableId: "1", |
| 47 |
}, |
| 48 |
posts: [{ id: "1", title: "Lorem ipsum" }], |
| 49 |
}); |
| 50 |
}); |
| 51 |
}); |