| 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 |
profile: belongsTo(), |
| 21 |
}), |
| 22 |
profile: Model.extend({ |
| 23 |
user: belongsTo(), |
| 24 |
}), |
| 25 |
}, |
| 26 |
}); |
| 27 |
|
| 28 |
this.db = this.server.db; |
| 29 |
this.schema = this.server.schema; |
| 30 |
} |
| 31 |
|
| 32 |
shutdown() { |
| 33 |
this.server.shutdown(); |
| 34 |
} |
| 35 |
|
| 36 |
savedChildNoParent() { |
| 37 |
let insertedUser = this.db.users.insert({ name: "Link" }); |
| 38 |
|
| 39 |
return [this.schema.users.find(insertedUser.id), undefined]; |
| 40 |
} |
| 41 |
|
| 42 |
savedChildNewParent() { |
| 43 |
let user = this.schema.users.create({ name: "Link" }); |
| 44 |
let profile = this.schema.profiles.new({ age: 300 }); |
| 45 |
|
| 46 |
user.profile = profile; |
| 47 |
|
| 48 |
return [user, profile]; |
| 49 |
} |
| 50 |
|
| 51 |
savedChildSavedParent() { |
| 52 |
let insertedProfile = this.db.profiles.insert({ age: 300 }); |
| 53 |
let insertedUser = this.db.users.insert({ |
| 54 |
name: "Link", |
| 55 |
profileId: insertedProfile.id, |
| 56 |
}); |
| 57 |
this.db.profiles.update(insertedProfile.id, { userId: insertedUser.id }); |
| 58 |
let user = this.schema.users.find(insertedUser.id); |
| 59 |
let profile = this.schema.profiles.find(insertedProfile.id); |
| 60 |
|
| 61 |
return [user, profile]; |
| 62 |
} |
| 63 |
|
| 64 |
newChildNoParent() { |
| 65 |
return [this.schema.users.new({ name: "Link" }), undefined]; |
| 66 |
} |
| 67 |
|
| 68 |
newChildNewParent() { |
| 69 |
let profile = this.schema.profiles.new({ age: 300 }); |
| 70 |
let user = this.schema.users.new({ name: "Link" }); |
| 71 |
user.profile = profile; |
| 72 |
|
| 73 |
return [user, profile]; |
| 74 |
} |
| 75 |
|
| 76 |
newChildSavedParent() { |
| 77 |
let insertedProfile = this.db.profiles.insert({ age: 300 }); |
| 78 |
let user = this.schema.users.new({ name: "Link" }); |
| 79 |
let savedProfile = this.schema.profiles.find(insertedProfile.id); |
| 80 |
|
| 81 |
user.profile = savedProfile; |
| 82 |
|
| 83 |
return [user, savedProfile]; |
| 84 |
} |
| 85 |
|
| 86 |
// Just a saved unassociated parent. |
| 87 |
savedParent() { |
| 88 |
let insertedProfile = this.db.profiles.insert({ age: 300 }); |
| 89 |
|
| 90 |
return this.schema.profiles.find(insertedProfile.id); |
| 91 |
} |
| 92 |
|
| 93 |
newParent() { |
| 94 |
return this.schema.profiles.new({ age: 300 }); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
export const states = [ |
| 99 |
"savedChildNoParent", |
| 100 |
"savedChildNewParent", |
| 101 |
"savedChildSavedParent", |
| 102 |
"newChildNoParent", |
| 103 |
"newChildNewParent", |
| 104 |
"newChildSavedParent", |
| 105 |
]; |