| 1 |
import "@lib/container"; |
| 2 |
import Db from "@lib/db"; |
| 3 |
import Schema from "@lib/orm/schema"; |
| 4 |
import { Model, hasMany } from "miragejs"; |
| 5 |
|
| 6 |
describe("Integration | ORM | Schema Verification | Has Many", function () { |
| 7 |
test("a one-way has many association is correct", () => { |
| 8 |
let schema = new Schema( |
| 9 |
new Db({ |
| 10 |
users: [{ id: 1, name: "Frodo" }], |
| 11 |
posts: [{ id: 1, title: "Lorem" }], |
| 12 |
}), |
| 13 |
{ |
| 14 |
user: Model.extend({ |
| 15 |
posts: hasMany(), |
| 16 |
}), |
| 17 |
post: Model.extend(), |
| 18 |
} |
| 19 |
); |
| 20 |
|
| 21 |
let frodo = schema.users.find(1); |
| 22 |
let association = frodo.associationFor("posts"); |
| 23 |
|
| 24 |
expect(association.name).toEqual("posts"); |
| 25 |
expect(association.modelName).toEqual("post"); |
| 26 |
expect(association.ownerModelName).toEqual("user"); |
| 27 |
|
| 28 |
let post = schema.posts.find(1); |
| 29 |
|
| 30 |
expect(post.inverseFor(association) === null).toBeTruthy(); |
| 31 |
}); |
| 32 |
|
| 33 |
test("a named one-way has many association is correct", () => { |
| 34 |
let schema = new Schema( |
| 35 |
new Db({ |
| 36 |
users: [{ id: 1, name: "Frodo" }], |
| 37 |
posts: [{ id: 1, title: "Lorem" }], |
| 38 |
}), |
| 39 |
{ |
| 40 |
user: Model.extend({ |
| 41 |
blogPosts: hasMany("post"), |
| 42 |
}), |
| 43 |
post: Model.extend(), |
| 44 |
} |
| 45 |
); |
| 46 |
|
| 47 |
let frodo = schema.users.find(1); |
| 48 |
let association = frodo.associationFor("blogPosts"); |
| 49 |
|
| 50 |
expect(association.name).toEqual("blogPosts"); |
| 51 |
expect(association.modelName).toEqual("post"); |
| 52 |
expect(association.ownerModelName).toEqual("user"); |
| 53 |
|
| 54 |
let post = schema.posts.find(1); |
| 55 |
|
| 56 |
expect(post.inverseFor(association) === null).toBeTruthy(); |
| 57 |
}); |
| 58 |
|
| 59 |
test("a reflexive hasMany association with an implicit inverse is correct", () => { |
| 60 |
let schema = new Schema( |
| 61 |
new Db({ |
| 62 |
tags: [{ id: 1, name: "economics" }], |
| 63 |
}), |
| 64 |
{ |
| 65 |
tag: Model.extend({ |
| 66 |
tags: hasMany(), |
| 67 |
}), |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
let tag = schema.tags.find(1); |
| 72 |
let association = tag.associationFor("tags"); |
| 73 |
|
| 74 |
expect(association.name).toEqual("tags"); |
| 75 |
expect(association.modelName).toEqual("tag"); |
| 76 |
expect(association.ownerModelName).toEqual("tag"); |
| 77 |
|
| 78 |
expect(tag.inverseFor(association) === association).toBeTruthy(); |
| 79 |
}); |
| 80 |
}); |