| 1 |
import Helper from "./_helper"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | Belongs To | Named | 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 author = helper.savedParent(); |
| 16 |
let post = schema.posts.new({ authorId: author.id }); |
| 17 |
|
| 18 |
expect(post.authorId).toEqual(author.id); |
| 19 |
expect(post.author).toEqual(author); |
| 20 |
expect(post.attrs).toEqual({ authorId: author.id }); |
| 21 |
}); |
| 22 |
|
| 23 |
test("the child errors if the parent id doesnt exist", () => { |
| 24 |
expect(function () { |
| 25 |
schema.posts.new({ authorId: 2 }); |
| 26 |
}).toThrow(); |
| 27 |
}); |
| 28 |
|
| 29 |
test("the child accepts a null parent id", () => { |
| 30 |
let post = schema.posts.new({ authorId: null }); |
| 31 |
|
| 32 |
expect(post.authorId).toBeNil(); |
| 33 |
expect(post.author).toBeNil(); |
| 34 |
expect(post.attrs).toEqual({ authorId: null }); |
| 35 |
}); |
| 36 |
|
| 37 |
test("the child accepts a saved parent model", () => { |
| 38 |
let author = helper.savedParent(); |
| 39 |
let post = schema.posts.new({ author }); |
| 40 |
|
| 41 |
expect(post.authorId).toEqual("1"); |
| 42 |
expect(post.author).toEqual(author); |
| 43 |
}); |
| 44 |
|
| 45 |
test("the child accepts a new parent model", () => { |
| 46 |
let zelda = schema.users.new({ name: "Zelda" }); |
| 47 |
let post = schema.posts.new({ author: zelda }); |
| 48 |
|
| 49 |
expect(post.authorId).toBeNil(); |
| 50 |
expect(post.author).toEqual(zelda); |
| 51 |
expect(post.attrs).toEqual({ authorId: null }); |
| 52 |
}); |
| 53 |
|
| 54 |
test("the child accepts a null parent model", () => { |
| 55 |
let post = schema.posts.new({ author: null }); |
| 56 |
|
| 57 |
expect(post.authorId).toBeNil(); |
| 58 |
expect(post.author).toBeNil(); |
| 59 |
expect(post.attrs).toEqual({ authorId: null }); |
| 60 |
}); |
| 61 |
|
| 62 |
test("the child accepts a parent model and id", () => { |
| 63 |
let author = helper.savedParent(); |
| 64 |
let post = schema.posts.new({ author, authorId: author.id }); |
| 65 |
|
| 66 |
expect(post.authorId).toEqual("1"); |
| 67 |
expect(post.author).toEqual(author); |
| 68 |
expect(post.attrs).toEqual({ authorId: author.id }); |
| 69 |
}); |
| 70 |
|
| 71 |
test("the child accepts no reference to a parent id or model as empty obj", () => { |
| 72 |
let post = schema.posts.new({}); |
| 73 |
|
| 74 |
expect(post.authorId).toBeNil(); |
| 75 |
expect(post.author).toBeNil(); |
| 76 |
expect(post.attrs).toEqual({ authorId: null }); |
| 77 |
}); |
| 78 |
|
| 79 |
test("the child accepts no reference to a parent id or model", () => { |
| 80 |
let post = schema.posts.new(); |
| 81 |
|
| 82 |
expect(post.authorId).toBeNil(); |
| 83 |
expect(post.author).toBeNil(); |
| 84 |
expect(post.attrs).toEqual({ authorId: null }); |
| 85 |
}); |
| 86 |
}); |