| 1 |
import Helper from "./_helper"; |
| 2 |
import { Model } from "miragejs"; |
| 3 |
|
| 4 |
describe("External | Shared | ORM | Belongs To | Named | 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 author = helper.schema.create("user"); |
| 16 |
let post = helper.schema.create("post", { |
| 17 |
authorId: author.id, |
| 18 |
}); |
| 19 |
|
| 20 |
expect(post.authorId).toEqual(author.id); |
| 21 |
expect(post.author.attrs).toEqual(author.attrs); |
| 22 |
expect(helper.schema.db.users).toHaveLength(1); |
| 23 |
expect(helper.schema.db.users[0]).toEqual({ id: "1" }); |
| 24 |
expect(helper.schema.db.posts).toHaveLength(1); |
| 25 |
expect(helper.schema.db.posts[0]).toEqual({ id: "1", authorId: "1" }); |
| 26 |
}); |
| 27 |
|
| 28 |
test("it sets up associations correctly when passing in the association itself", () => { |
| 29 |
let author = helper.schema.create("user"); |
| 30 |
let post = helper.schema.create("post", { |
| 31 |
author, |
| 32 |
}); |
| 33 |
|
| 34 |
expect(post.authorId).toEqual(author.id); |
| 35 |
expect(post.author.attrs).toEqual(author.attrs); |
| 36 |
expect(helper.schema.db.users).toHaveLength(1); |
| 37 |
expect(helper.schema.db.users[0]).toEqual({ id: "1" }); |
| 38 |
expect(helper.schema.db.posts).toHaveLength(1); |
| 39 |
expect(helper.schema.db.posts[0]).toEqual({ id: "1", authorId: "1" }); |
| 40 |
}); |
| 41 |
|
| 42 |
test("it throws an error if a model is passed in without a defined relationship", () => { |
| 43 |
let { schema } = helper; |
| 44 |
|
| 45 |
expect(function () { |
| 46 |
schema.create("post", { |
| 47 |
foo: schema.create("foo"), |
| 48 |
}); |
| 49 |
}).toThrow(); |
| 50 |
}); |
| 51 |
|
| 52 |
test("it throws an error if a collection is passed in without a defined relationship", () => { |
| 53 |
let { schema } = helper; |
| 54 |
schema.create("foo"); |
| 55 |
schema.create("foo"); |
| 56 |
|
| 57 |
expect(function () { |
| 58 |
schema.create("post", { |
| 59 |
foos: schema.foos.all(), |
| 60 |
}); |
| 61 |
}).toThrow(); |
| 62 |
}); |
| 63 |
}); |