| 1 |
import Helper from "./_helper"; |
| 2 |
import { Model } from "miragejs"; |
| 3 |
|
| 4 |
describe("External | Shared | ORM | Belongs To | One To One | create", () => { |
| 5 |
let helper; |
| 6 |
beforeEach(() => { |
| 7 |
helper = new Helper(); |
| 8 |
helper.schema.registerModel("foo", Model); |
| 9 |
}); |
| 10 |
afterEach(() => { |
| 11 |
helper.shutdown(); |
| 12 |
}); |
| 13 |
|
| 14 |
test("it sets up associations correctly when passing in the foreign key", () => { |
| 15 |
let { schema } = helper; |
| 16 |
let profile = schema.create("profile"); |
| 17 |
let user = schema.create("user", { |
| 18 |
profileId: profile.id, |
| 19 |
}); |
| 20 |
profile.reload(); |
| 21 |
|
| 22 |
expect(user.profileId).toEqual(profile.id); |
| 23 |
expect(user.profile.attrs).toEqual(profile.attrs); |
| 24 |
expect(profile.user.attrs).toEqual(user.attrs); |
| 25 |
expect(schema.db.users).toHaveLength(1); |
| 26 |
expect(schema.db.profiles).toHaveLength(1); |
| 27 |
expect(schema.db.users[0]).toEqual({ id: "1", profileId: "1" }); |
| 28 |
expect(schema.db.profiles[0]).toEqual({ id: "1", userId: "1" }); |
| 29 |
}); |
| 30 |
|
| 31 |
test("it sets up associations correctly when passing in the association itself", () => { |
| 32 |
let { schema } = helper; |
| 33 |
let profile = schema.create("profile"); |
| 34 |
let user = schema.create("user", { |
| 35 |
profile, |
| 36 |
}); |
| 37 |
|
| 38 |
expect(user.profileId).toEqual(profile.id); |
| 39 |
expect(user.profile.attrs).toEqual(profile.attrs); |
| 40 |
expect(profile.user.attrs).toEqual(user.attrs); |
| 41 |
expect(schema.db.users).toHaveLength(1); |
| 42 |
expect(schema.db.profiles).toHaveLength(1); |
| 43 |
expect(schema.db.users[0]).toEqual({ id: "1", profileId: "1" }); |
| 44 |
expect(schema.db.profiles[0]).toEqual({ id: "1", userId: "1" }); |
| 45 |
}); |
| 46 |
|
| 47 |
test("it throws an error if a model is passed in without a defined relationship", () => { |
| 48 |
let { schema } = helper; |
| 49 |
|
| 50 |
expect(function () { |
| 51 |
schema.create("user", { |
| 52 |
foo: schema.create("foo"), |
| 53 |
}); |
| 54 |
}).toThrow(); |
| 55 |
}); |
| 56 |
|
| 57 |
test("it throws an error if a collection is passed in without a defined relationship", () => { |
| 58 |
let { schema } = helper; |
| 59 |
schema.create("foo"); |
| 60 |
schema.create("foo"); |
| 61 |
|
| 62 |
expect(function () { |
| 63 |
schema.create("user", { |
| 64 |
foos: schema.foos.all(), |
| 65 |
}); |
| 66 |
}).toThrow(); |
| 67 |
}); |
| 68 |
}); |