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