Debian Codegraph @

__tests__/external/shared/orm/belongs-to/10-one-to-one-polymorphic/_helper.js

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