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