Debian Codegraph @

__tests__/external/shared/orm/has-many/10-many-to-many-polymorphic/_helper.js

raw
1 import { Server, Model, hasMany } from "miragejs";
2
3 /*
4 A model with a hasMany association can be in eight states
5 with respect to its association. This helper class
6 returns a parent (and its children) in these various states.
7
8 The return value is an array of the form
9
10 [ parent, [child1, child2...] ]
11
12 where the children array may be empty.
13 */
14 export default class Helper {
15 constructor() {
16 this.server = new Server({
17 environment: "test",
18 models: {
19 post: Model.extend({
20 users: hasMany(),
21 }),
22 user: Model.extend({
23 commentables: hasMany({ 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 savedParentNoChildren() {
38 let user = this.db.users.insert({ name: "Link" });
39
40 return [this.schema.users.find(user.id), []];
41 }
42
43 savedParentNewChildren() {
44 let user = this.schema.users.create({ name: "Link" });
45 let post1 = this.schema.posts.new({ title: "Lorem" });
46 let post2 = this.schema.posts.new({ title: "Ipsum" });
47
48 user.commentables = [post1, post2];
49
50 return [user, [post1, post2]];
51 }
52
53 savedParentSavedChildren() {
54 this.loadData({
55 posts: [
56 { id: "1", title: "Lorem", userIds: ["1"] },
57 { id: "2", title: "Ipsum", userIds: ["1"] },
58 ],
59 users: [
60 {
61 id: "1",
62 name: "Link",
63 commentableIds: [
64 { type: "post", id: "1" },
65 { type: "post", id: "2" },
66 ],
67 },
68 ],
69 });
70
71 let user = this.schema.users.find(1);
72 let post1 = this.schema.posts.find(1);
73 let post2 = this.schema.posts.find(2);
74
75 return [user, [post1, post2]];
76 }
77
78 savedParentMixedChildren() {
79 this.loadData({
80 posts: [{ id: "1", title: "Lorem", userIds: [] }],
81 users: [{ id: "1", name: "Link", commentableIds: [] }],
82 });
83
84 let user = this.schema.users.find(1);
85 let post1 = this.schema.posts.find(1);
86 let post2 = this.schema.posts.new({ title: "Ipsum" });
87
88 user.commentables = [post1, post2];
89
90 return [user, [post1, post2]];
91 }
92
93 newParentNoChildren() {
94 let user = this.schema.users.new({ name: "Link" });
95
96 return [user, []];
97 }
98
99 newParentNewChildren() {
100 let user = this.schema.users.new({ name: "Link" });
101 let post1 = this.schema.posts.new({ title: "Lorem" });
102 let post2 = this.schema.posts.new({ title: "Ipsum" });
103
104 user.commentables = [post1, post2];
105
106 return [user, [post1, post2]];
107 }
108
109 newParentSavedChildren() {
110 let user = this.schema.users.new({ name: "Link" });
111 let post1 = this.schema.posts.create({ title: "Lorem" });
112 let post2 = this.schema.posts.create({ title: "Ipsum" });
113
114 user.commentables = [post1, post2];
115
116 return [user, [post1, post2]];
117 }
118
119 newParentMixedChildren() {
120 let user = this.schema.users.new({ name: "Link" });
121 let post1 = this.schema.posts.create({ title: "Lorem" });
122 let post2 = this.schema.posts.new({ title: "Ipsum" });
123
124 user.commentables = [post1, post2];
125
126 return [user, [post1, post2]];
127 }
128
129 // Unassociated child models, used for setting tests
130 savedChild() {
131 let insertedPost = this.db.posts.insert({ title: "Lorem" });
132
133 return this.schema.posts.find(insertedPost.id);
134 }
135
136 newChild() {
137 return this.schema.posts.new({ title: "Lorem" });
138 }
139 }
140
141 export const states = [
142 "savedParentNoChildren",
143 "savedParentNewChildren",
144 "savedParentSavedChildren",
145 "savedParentMixedChildren",
146 "newParentNoChildren",
147 "newParentNewChildren",
148 "newParentSavedChildren",
149 "newParentMixedChildren",
150 ];