| 1 |
import Helper from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Has Many | Many-to-many Polymorphic | create", () => { |
| 4 |
let helper; |
| 5 |
beforeEach(() => { |
| 6 |
helper = new Helper(); |
| 7 |
}); |
| 8 |
afterEach(() => { |
| 9 |
helper.shutdown(); |
| 10 |
}); |
| 11 |
|
| 12 |
test("it sets up associations correctly when passing in the foreign key", () => { |
| 13 |
let post = helper.schema.create("post"); |
| 14 |
let user = helper.schema.create("user", { |
| 15 |
commentableIds: [{ type: "post", id: post.id }], |
| 16 |
}); |
| 17 |
|
| 18 |
post.reload(); |
| 19 |
|
| 20 |
expect(user.commentableIds).toEqual([{ type: "post", id: post.id }]); |
| 21 |
expect(user.attrs.commentableIds).toEqual([{ type: "post", id: post.id }]); |
| 22 |
expect(user.commentables.models[0].attrs).toEqual(post.attrs); |
| 23 |
expect(helper.db.posts).toHaveLength(1); |
| 24 |
expect(helper.db.users).toHaveLength(1); |
| 25 |
expect(helper.db.posts[0]).toEqual({ id: "1", userIds: ["1"] }); |
| 26 |
expect(helper.db.users[0]).toEqual({ |
| 27 |
id: "1", |
| 28 |
commentableIds: [{ type: "post", id: "1" }], |
| 29 |
}); |
| 30 |
}); |
| 31 |
|
| 32 |
test("it sets up associations correctly when passing in an array of models", () => { |
| 33 |
let post = helper.schema.create("post"); |
| 34 |
let user = helper.schema.create("user", { |
| 35 |
commentables: [post], |
| 36 |
}); |
| 37 |
|
| 38 |
post.reload(); |
| 39 |
|
| 40 |
expect(user.commentableIds).toEqual([{ type: "post", id: post.id }]); |
| 41 |
expect(post.userIds).toEqual(["1"]); |
| 42 |
expect(user.attrs.commentableIds).toEqual([{ type: "post", id: post.id }]); |
| 43 |
expect(post.attrs.userIds).toEqual([user.id]); |
| 44 |
expect(helper.db.users).toHaveLength(1); |
| 45 |
expect(helper.db.posts).toHaveLength(1); |
| 46 |
}); |
| 47 |
|
| 48 |
test("it sets up associations correctly when passing in a collection", () => { |
| 49 |
let post = helper.schema.create("post"); |
| 50 |
let user = helper.schema.create("user", { |
| 51 |
commentables: helper.schema.posts.all(), |
| 52 |
}); |
| 53 |
|
| 54 |
post.reload(); |
| 55 |
|
| 56 |
expect(user.commentableIds).toEqual([{ type: "post", id: post.id }]); |
| 57 |
expect(post.userIds).toEqual([user.id]); |
| 58 |
expect(user.attrs.commentableIds).toEqual([{ type: "post", id: post.id }]); |
| 59 |
expect(post.attrs.userIds).toEqual([user.id]); |
| 60 |
expect(helper.db.users).toHaveLength(1); |
| 61 |
expect(helper.db.posts).toHaveLength(1); |
| 62 |
}); |
| 63 |
}); |