| 1 |
import { Server, Model, hasMany, belongsTo } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | associationsFor", function () { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ environment: "test" }); |
| 8 |
}); |
| 9 |
|
| 10 |
afterEach(() => { |
| 11 |
server.shutdown(); |
| 12 |
}); |
| 13 |
|
| 14 |
test("it returns an empty object for a model with no relationships", () => { |
| 15 |
server.config({ |
| 16 |
models: { |
| 17 |
user: Model, |
| 18 |
}, |
| 19 |
}); |
| 20 |
|
| 21 |
expect(server.schema.associationsFor("user")).toEqual({}); |
| 22 |
}); |
| 23 |
|
| 24 |
test("it returns an object containing all a model's relationships", () => { |
| 25 |
server.config({ |
| 26 |
models: { |
| 27 |
user: Model, |
| 28 |
article: Model.extend({ |
| 29 |
fineAuthor: belongsTo("user"), |
| 30 |
comments: hasMany(), |
| 31 |
}), |
| 32 |
comment: Model, |
| 33 |
}, |
| 34 |
}); |
| 35 |
|
| 36 |
let associations = server.schema.associationsFor("article"); |
| 37 |
|
| 38 |
expect(Object.keys(associations)).toEqual(["fineAuthor", "comments"]); |
| 39 |
|
| 40 |
let fineAuthorAssociation = associations.fineAuthor; |
| 41 |
|
| 42 |
expect(fineAuthorAssociation.type).toEqual("belongsTo"); |
| 43 |
expect(fineAuthorAssociation.modelName).toEqual("user"); |
| 44 |
expect(fineAuthorAssociation.name).toEqual("fineAuthor"); |
| 45 |
|
| 46 |
let commentsAssociation = associations.comments; |
| 47 |
expect(commentsAssociation.type).toEqual("hasMany"); |
| 48 |
expect(commentsAssociation.modelName).toEqual("comment"); |
| 49 |
expect(commentsAssociation.name).toEqual("comments"); |
| 50 |
}); |
| 51 |
}); |