| 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 |
user: 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 insertedUser = this.db.users.insert({ name: "Link" }); |
| 35 |
|
| 36 |
return [this.schema.users.find(insertedUser.id), undefined]; |
| 37 |
} |
| 38 |
|
| 39 |
savedChildNewParent() { |
| 40 |
let child = this.schema.users.create({ name: "Link" }); |
| 41 |
let parent = this.schema.users.new({ name: "Bob" }); |
| 42 |
|
| 43 |
child.user = parent; |
| 44 |
|
| 45 |
return [child, parent]; |
| 46 |
} |
| 47 |
|
| 48 |
savedChildSavedParent() { |
| 49 |
let insertedParent = this.db.users.insert({ name: "Bob" }); |
| 50 |
let insertedChild = this.db.users.insert({ |
| 51 |
name: "Link", |
| 52 |
userId: insertedParent.id, |
| 53 |
}); |
| 54 |
let child = this.schema.users.find(insertedChild.id); |
| 55 |
let parent = this.schema.users.find(insertedParent.id); |
| 56 |
|
| 57 |
return [child, parent]; |
| 58 |
} |
| 59 |
|
| 60 |
newChildNoParent() { |
| 61 |
return [this.schema.users.new({ name: "Link" }), undefined]; |
| 62 |
} |
| 63 |
|
| 64 |
newChildNewParent() { |
| 65 |
let parent = this.schema.users.new({ name: "Link" }); |
| 66 |
let child = this.schema.users.new({ name: "Bob" }); |
| 67 |
child.user = parent; |
| 68 |
|
| 69 |
return [child, parent]; |
| 70 |
} |
| 71 |
|
| 72 |
newChildSavedParent() { |
| 73 |
let insertedParent = this.db.users.insert({ name: "Bob" }); |
| 74 |
let child = this.schema.users.new({ name: "Link" }); |
| 75 |
let savedParent = this.schema.users.find(insertedParent.id); |
| 76 |
|
| 77 |
child.user = savedParent; |
| 78 |
|
| 79 |
return [child, savedParent]; |
| 80 |
} |
| 81 |
|
| 82 |
// Just a saved unassociated parent. |
| 83 |
savedParent() { |
| 84 |
let insertedParent = this.db.users.insert({ name: "Bob" }); |
| 85 |
|
| 86 |
return this.schema.users.find(insertedParent.id); |
| 87 |
} |
| 88 |
|
| 89 |
newParent() { |
| 90 |
return this.schema.users.new({ name: "Bob" }); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
export const states = [ |
| 95 |
"savedChildNoParent", |
| 96 |
"savedChildNewParent", |
| 97 |
"savedChildSavedParent", |
| 98 |
"newChildNoParent", |
| 99 |
"newChildNewParent", |
| 100 |
"newChildSavedParent", |
| 101 |
]; |