| 1 |
import Helper from "./_helper"; |
| 2 |
import { Model } from "miragejs"; |
| 3 |
|
| 4 |
describe("External | Shared | ORM | Belongs To | One-to-one 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 { schema } = helper; |
| 16 |
let post = schema.create("post"); |
| 17 |
let comment = schema.create("comment", { |
| 18 |
commentableId: { type: "post", id: post.id }, |
| 19 |
}); |
| 20 |
post.reload(); |
| 21 |
|
| 22 |
expect(comment.commentableId).toEqual({ type: "post", id: post.id }); |
| 23 |
expect(comment.commentable.attrs).toEqual(post.attrs); |
| 24 |
expect(post.comment.attrs).toEqual(comment.attrs); |
| 25 |
expect(schema.db.comments).toHaveLength(1); |
| 26 |
expect(schema.db.posts).toHaveLength(1); |
| 27 |
expect(schema.db.comments[0]).toEqual({ |
| 28 |
id: "1", |
| 29 |
commentableId: { type: "post", id: "1" }, |
| 30 |
}); |
| 31 |
expect(schema.db.posts[0]).toEqual({ id: "1", commentId: "1" }); |
| 32 |
}); |
| 33 |
|
| 34 |
test("it sets up associations correctly when passing in the association itself", () => { |
| 35 |
let { schema } = helper; |
| 36 |
let post = schema.create("post"); |
| 37 |
let comment = schema.create("comment", { |
| 38 |
commentable: post, |
| 39 |
}); |
| 40 |
|
| 41 |
expect(comment.commentableId).toEqual({ type: "post", id: post.id }); |
| 42 |
expect(comment.commentable.attrs).toEqual(post.attrs); |
| 43 |
expect(post.comment.attrs).toEqual(comment.attrs); |
| 44 |
expect(schema.db.comments).toHaveLength(1); |
| 45 |
expect(schema.db.posts).toHaveLength(1); |
| 46 |
expect(schema.db.comments[0]).toEqual({ |
| 47 |
id: "1", |
| 48 |
commentableId: { type: "post", id: "1" }, |
| 49 |
}); |
| 50 |
expect(schema.db.posts[0]).toEqual({ id: "1", commentId: "1" }); |
| 51 |
}); |
| 52 |
|
| 53 |
test("it throws an error if a model is passed in without a defined relationship", () => { |
| 54 |
let { schema } = helper; |
| 55 |
|
| 56 |
expect(function () { |
| 57 |
schema.create("comment", { |
| 58 |
foo: schema.create("foo"), |
| 59 |
}); |
| 60 |
}).toThrow(); |
| 61 |
}); |
| 62 |
|
| 63 |
test("it throws an error if a collection is passed in without a defined relationship", () => { |
| 64 |
let { schema } = helper; |
| 65 |
schema.create("foo"); |
| 66 |
schema.create("foo"); |
| 67 |
|
| 68 |
expect(function () { |
| 69 |
schema.create("comment", { |
| 70 |
foos: schema.foos.all(), |
| 71 |
}); |
| 72 |
}).toThrow(); |
| 73 |
}); |
| 74 |
}); |