| 1 |
import { Server, Model, belongsTo } from "miragejs"; |
| 2 |
|
| 3 |
/* |
| 4 |
A model with a belongsTo association can be in six states |
| 5 |
with respect to its association. This helper class |
| 6 |
returns a child (and its association) in these various states. |
| 7 |
|
| 8 |
The return value is an array of the form |
| 9 |
|
| 10 |
[ child, parent ] |
| 11 |
|
| 12 |
where the parent may be undefined. |
| 13 |
*/ |
| 14 |
export default class BelongsToHelper { |
| 15 |
constructor() { |
| 16 |
this.server = new Server({ |
| 17 |
environment: "test", |
| 18 |
models: { |
| 19 |
user: Model.extend({ |
| 20 |
representative: belongsTo("user", { inverse: null }), |
| 21 |
}), |
| 22 |
}, |
| 23 |
}); |
| 24 |
|
| 25 |
this.db = this.server.db; |
| 26 |
this.schema = this.server.schema; |
| 27 |
} |
| 28 |
|
| 29 |
shutdown() { |
| 30 |
this.server.shutdown(); |
| 31 |
} |
| 32 |
|
| 33 |
savedChildNoParent() { |
| 34 |
let link = this.db.users.insert({ name: "Link" }); |
| 35 |
|
| 36 |
return [this.schema.users.find(link.id), undefined]; |
| 37 |
} |
| 38 |
|
| 39 |
savedChildSavedParent() { |
| 40 |
let linkDbRecord = this.db.users.insert({ name: "Link" }); |
| 41 |
this.db.users.update(linkDbRecord.id, { |
| 42 |
representativeId: linkDbRecord.id, |
| 43 |
}); |
| 44 |
|
| 45 |
let link = this.schema.users.find(linkDbRecord.id); |
| 46 |
|
| 47 |
return [link, link]; |
| 48 |
} |
| 49 |
|
| 50 |
newChildNoParent() { |
| 51 |
return [this.schema.users.new({ name: "Link" }), undefined]; |
| 52 |
} |
| 53 |
|
| 54 |
newChildNewParent() { |
| 55 |
let link = this.schema.users.new({ name: "Link" }); |
| 56 |
link.representative = link; |
| 57 |
|
| 58 |
return [link, link]; |
| 59 |
} |
| 60 |
|
| 61 |
// Just a saved unassociated parent. |
| 62 |
// savedParent() { |
| 63 |
// let insertedParent = this.db.users.insert({ name: 'Bob' }); |
| 64 |
// |
| 65 |
// return this.schema.users.find(insertedParent.id); |
| 66 |
// } |
| 67 |
// |
| 68 |
// newParent() { |
| 69 |
// return this.schema.users.new({ name: 'Bob' }); |
| 70 |
// } |
| 71 |
} |
| 72 |
|
| 73 |
export const states = [ |
| 74 |
"savedChildNoParent", |
| 75 |
"savedChildSavedParent", |
| 76 |
"newChildNoParent", |
| 77 |
"newChildNewParent", |
| 78 |
]; |