| 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 |
post: Model.extend(), |
| 20 |
comment: Model.extend({ |
| 21 |
commentable: belongsTo("commentable", { polymorphic: true }), |
| 22 |
}), |
| 23 |
}, |
| 24 |
}); |
| 25 |
|
| 26 |
this.db = this.server.db; |
| 27 |
this.loadData = this.db.loadData.bind(this.db); |
| 28 |
this.schema = this.server.schema; |
| 29 |
} |
| 30 |
|
| 31 |
shutdown() { |
| 32 |
this.server.shutdown(); |
| 33 |
} |
| 34 |
|
| 35 |
savedChildNoParent() { |
| 36 |
let insertedComment = this.db.comments.insert({ text: "Lorem" }); |
| 37 |
|
| 38 |
return [this.schema.comments.find(insertedComment.id), undefined]; |
| 39 |
} |
| 40 |
|
| 41 |
savedChildNewParent() { |
| 42 |
this.loadData({ |
| 43 |
comments: [{ id: "1", text: "Lorem" }], |
| 44 |
}); |
| 45 |
|
| 46 |
let comment = this.schema.comments.find(1); |
| 47 |
let post = this.schema.posts.new({ title: "Bob" }); |
| 48 |
|
| 49 |
comment.commentable = post; |
| 50 |
|
| 51 |
return [comment, post]; |
| 52 |
} |
| 53 |
|
| 54 |
savedChildSavedParent() { |
| 55 |
this.loadData({ |
| 56 |
posts: [{ id: "1", title: "Lorem ipsum" }], |
| 57 |
comments: [ |
| 58 |
{ id: "1", text: "Trolling", commentableId: { id: "1", type: "post" } }, |
| 59 |
], |
| 60 |
}); |
| 61 |
|
| 62 |
let comment = this.schema.comments.find(1); |
| 63 |
let post = this.schema.posts.find(1); |
| 64 |
|
| 65 |
return [comment, post]; |
| 66 |
} |
| 67 |
|
| 68 |
newChildNoParent() { |
| 69 |
return [this.schema.comments.new({ text: "Lorem" }), undefined]; |
| 70 |
} |
| 71 |
|
| 72 |
newChildNewParent() { |
| 73 |
let comment = this.schema.comments.new({ text: "Lorem" }); |
| 74 |
let newPost = this.schema.posts.new({ title: "Bob" }); |
| 75 |
comment.commentable = newPost; |
| 76 |
|
| 77 |
return [comment, newPost]; |
| 78 |
} |
| 79 |
|
| 80 |
newChildSavedParent() { |
| 81 |
this.loadData({ |
| 82 |
posts: [{ id: "1", title: "Lorem ipsum" }], |
| 83 |
}); |
| 84 |
let comment = this.schema.comments.new({ text: "Lorem" }); |
| 85 |
let savedPost = this.schema.posts.find(1); |
| 86 |
|
| 87 |
comment.commentable = savedPost; |
| 88 |
|
| 89 |
return [comment, savedPost]; |
| 90 |
} |
| 91 |
|
| 92 |
// Just a saved unassociated parent. |
| 93 |
savedParent() { |
| 94 |
let insertedPost = this.db.posts.insert({ title: "Bob" }); |
| 95 |
|
| 96 |
return this.schema.posts.find(insertedPost.id); |
| 97 |
} |
| 98 |
|
| 99 |
newParent() { |
| 100 |
return this.schema.posts.new({ title: "Bob" }); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
export const states = [ |
| 105 |
"savedChildNoParent", |
| 106 |
"savedChildNewParent", |
| 107 |
"savedChildSavedParent", |
| 108 |
"newChildNoParent", |
| 109 |
"newChildNewParent", |
| 110 |
"newChildSavedParent", |
| 111 |
]; |