| 1 |
import Helper from "./_helper"; |
| 2 |
import { Model } from "miragejs"; |
| 3 |
|
| 4 |
describe("External | Shared | ORM | Belongs To | One-way Polymorphic | create", () => { |
| 5 |
let helper; |
| 6 |
beforeEach(() => { |
| 7 |
helper = new Helper(); |
| 8 |
helper.schema.registerModel("foo", Model); |
| 9 |
}); |
| 10 |
afterEach(() => { |
| 11 |
helper.shutdown(); |
| 12 |
}); |
| 13 |
|
| 14 |
test("it sets up associations correctly when passing in the foreign key", () => { |
| 15 |
let post = helper.schema.create("post"); |
| 16 |
let comment = helper.schema.create("comment", { |
| 17 |
commentableId: { id: post.id, type: "post" }, |
| 18 |
}); |
| 19 |
|
| 20 |
expect(comment.commentableId).toEqual({ id: post.id, type: "post" }); |
| 21 |
expect(comment.commentable.attrs).toEqual(post.attrs); |
| 22 |
expect(helper.schema.db.posts).toHaveLength(1); |
| 23 |
expect(helper.schema.db.posts[0]).toEqual({ id: "1" }); |
| 24 |
expect(helper.schema.db.comments).toHaveLength(1); |
| 25 |
expect(helper.schema.db.comments[0]).toEqual({ |
| 26 |
id: "1", |
| 27 |
commentableId: { id: "1", type: "post" }, |
| 28 |
}); |
| 29 |
}); |
| 30 |
|
| 31 |
test("it sets up associations correctly when passing in the association itself", () => { |
| 32 |
let post = helper.schema.create("post"); |
| 33 |
let comment = helper.schema.create("comment", { |
| 34 |
commentable: post, |
| 35 |
}); |
| 36 |
|
| 37 |
expect(comment.commentableId).toEqual({ id: post.id, type: "post" }); |
| 38 |
expect(comment.commentable.attrs).toEqual(post.attrs); |
| 39 |
expect(helper.schema.db.posts).toHaveLength(1); |
| 40 |
expect(helper.schema.db.posts[0]).toEqual({ id: "1" }); |
| 41 |
expect(helper.schema.db.comments).toHaveLength(1); |
| 42 |
expect(helper.schema.db.comments[0]).toEqual({ |
| 43 |
id: "1", |
| 44 |
commentableId: { id: "1", type: "post" }, |
| 45 |
}); |
| 46 |
}); |
| 47 |
}); |