| 1 |
import Helper from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Belongs To | One-to-one Polymorphic | instantiating", () => { |
| 4 |
let helper, schema; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
helper = new Helper(); |
| 8 |
schema = helper.schema; |
| 9 |
}); |
| 10 |
afterEach(() => { |
| 11 |
helper.shutdown(); |
| 12 |
}); |
| 13 |
|
| 14 |
test("the child accepts a saved parent id", () => { |
| 15 |
let post = helper.savedParent(); |
| 16 |
let comment = schema.comments.new({ |
| 17 |
commentableId: { type: "post", id: post.id }, |
| 18 |
}); |
| 19 |
|
| 20 |
expect(comment.commentableId).toEqual({ type: "post", id: post.id }); |
| 21 |
expect(comment.commentable.attrs).toEqual(post.attrs); |
| 22 |
expect(comment.attrs).toEqual({ |
| 23 |
commentableId: { type: "post", id: post.id }, |
| 24 |
}); |
| 25 |
}); |
| 26 |
|
| 27 |
test("the child errors if the parent id doesnt exist", () => { |
| 28 |
expect(function () { |
| 29 |
schema.comments.new({ commentableId: { type: "post", id: 2 } }); |
| 30 |
}).toThrow(); |
| 31 |
}); |
| 32 |
|
| 33 |
test("the child accepts a null parent id", () => { |
| 34 |
let comment = schema.comments.new({ commentableId: null }); |
| 35 |
|
| 36 |
expect(comment.commentableId).toBeNil(); |
| 37 |
expect(comment.commentable).toBeNil(); |
| 38 |
expect(comment.attrs).toEqual({ commentableId: null }); |
| 39 |
}); |
| 40 |
|
| 41 |
test("the child accepts a saved parent model", () => { |
| 42 |
let post = helper.savedParent(); |
| 43 |
let comment = schema.comments.new({ commentable: post }); |
| 44 |
|
| 45 |
expect(comment.commentableId).toEqual({ type: "post", id: post.id }); |
| 46 |
expect(comment.commentable.attrs).toEqual(post.attrs); |
| 47 |
expect(comment.attrs).toEqual({ commentableId: null }); // this would update when saved |
| 48 |
}); |
| 49 |
|
| 50 |
test("the child accepts a new parent model", () => { |
| 51 |
let post = schema.posts.new({ age: 300 }); |
| 52 |
let comment = schema.comments.new({ commentable: post }); |
| 53 |
|
| 54 |
expect(comment.commentableId).toEqual({ type: "post", id: undefined }); |
| 55 |
expect(comment.commentable).toEqual(post); |
| 56 |
expect(comment.attrs).toEqual({ commentableId: null }); |
| 57 |
}); |
| 58 |
|
| 59 |
test("the child accepts a null parent model", () => { |
| 60 |
let comment = schema.comments.new({ commentable: null }); |
| 61 |
|
| 62 |
expect(comment.commentableId).toBeNil(); |
| 63 |
expect(comment.commentable).toBeNil(); |
| 64 |
expect(comment.attrs).toEqual({ commentableId: null }); |
| 65 |
}); |
| 66 |
|
| 67 |
test("the child accepts a parent model and id", () => { |
| 68 |
let post = helper.savedParent(); |
| 69 |
let comment = schema.comments.new({ |
| 70 |
commentable: post, |
| 71 |
commentableId: { type: "post", id: post.id }, |
| 72 |
}); |
| 73 |
|
| 74 |
expect(comment.commentableId).toEqual({ type: "post", id: "1" }); |
| 75 |
expect(comment.commentable).toEqual(post); |
| 76 |
expect(comment.attrs).toEqual({ |
| 77 |
commentableId: { type: "post", id: post.id }, |
| 78 |
}); |
| 79 |
}); |
| 80 |
|
| 81 |
test("the child accepts no reference to a parent id or model as empty obj", () => { |
| 82 |
let comment = schema.comments.new({}); |
| 83 |
|
| 84 |
expect(comment.commentableId).toBeNil(); |
| 85 |
expect(comment.commentable).toBeNil(); |
| 86 |
expect(comment.attrs).toEqual({ commentableId: null }); |
| 87 |
}); |
| 88 |
|
| 89 |
test("the child accepts no reference to a parent id or model", () => { |
| 90 |
let comment = schema.comments.new(); |
| 91 |
|
| 92 |
expect(comment.commentableId).toBeNil(); |
| 93 |
expect(comment.commentable).toBeNil(); |
| 94 |
expect(comment.attrs).toEqual({ commentableId: null }); |
| 95 |
}); |
| 96 |
}); |