| 1 |
import { Server, Model, hasMany, belongsTo } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | associations", function () { |
| 4 |
let server; |
| 5 |
let fineAuthorAssociation; |
| 6 |
let commentsAssociation; |
| 7 |
|
| 8 |
beforeEach(() => { |
| 9 |
server = new Server({ |
| 10 |
environment: "test", |
| 11 |
models: { |
| 12 |
user: Model, |
| 13 |
post: Model.extend({ |
| 14 |
fineAuthor: belongsTo("user"), |
| 15 |
comments: hasMany(), |
| 16 |
}), |
| 17 |
comment: Model, |
| 18 |
}, |
| 19 |
}); |
| 20 |
let associations = server.schema.associationsFor("post"); |
| 21 |
fineAuthorAssociation = associations.fineAuthor; |
| 22 |
commentsAssociation = associations.comments; |
| 23 |
}); |
| 24 |
|
| 25 |
afterEach(() => { |
| 26 |
server.shutdown(); |
| 27 |
}); |
| 28 |
|
| 29 |
test("name returns the property used to define the association", function () { |
| 30 |
expect(fineAuthorAssociation.name).toEqual("fineAuthor"); |
| 31 |
expect(commentsAssociation.name).toEqual("comments"); |
| 32 |
}); |
| 33 |
|
| 34 |
test("modelName returns the modelName of the associated model", function () { |
| 35 |
expect(fineAuthorAssociation.modelName).toEqual("user"); |
| 36 |
expect(commentsAssociation.modelName).toEqual("comment"); |
| 37 |
}); |
| 38 |
|
| 39 |
test("type returns the type of association", function () { |
| 40 |
expect(fineAuthorAssociation.type).toEqual("belongsTo"); |
| 41 |
expect(commentsAssociation.type).toEqual("hasMany"); |
| 42 |
}); |
| 43 |
|
| 44 |
test("foreignKey returns the name used for the association's foreign key", function () { |
| 45 |
expect(fineAuthorAssociation.foreignKey).toEqual("fineAuthorId"); |
| 46 |
expect(commentsAssociation.foreignKey).toEqual("commentIds"); |
| 47 |
}); |
| 48 |
}); |