Debian Codegraph @

__tests__/external/shared/orm/mixed/3-one-to-many-polymorphic/_helper.js

raw
1 import { Server, Model, hasMany, belongsTo } 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 user: Model.extend({
20 things: hasMany({ polymorphic: true }),
21 }),
22 post: Model.extend({
23 user: belongsTo({ inverse: "things" }),
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 savedParentNoChildren() {
37 let user = this.db.users.insert({ name: "Link" });
38
39 return [this.schema.users.find(user.id), []];
40 }
41
42 savedParentNewChildren() {
43 let user = this.schema.users.create({ name: "Link" });
44 let post1 = this.schema.posts.new({ title: "Lorem" });
45 let post2 = this.schema.posts.new({ title: "Ipsum" });
46
47 user.things = [post1, post2];
48
49 return [user, [post1, post2]];
50 }
51
52 savedParentSavedChildren() {
53 let { schema } = this;
54 schema.db.loadData({
55 users: [
56 {
57 id: "1",
58 name: "Link",
59 thingIds: [
60 { type: "post", id: "1" },
61 { type: "post", id: "2" },
62 ],
63 },
64 ],
65 posts: [
66 { id: "1", title: "Lorem", userId: "1" },
67 { id: "2", title: "Ipsum", userId: "1" },
68 ],
69 });
70
71 return [schema.users.find(1), [schema.posts.find(1), schema.posts.find(2)]];
72 }
73
74 savedParentMixedChildren() {
75 this.schema.db.loadData({
76 users: [{ id: "1", name: "Link", thingIds: [{ type: "post", id: "1" }] }],
77 posts: [{ id: "1", title: "Lorem", userId: "1" }],
78 });
79 let user = this.schema.users.find(1);
80 let post1 = this.schema.posts.find(1);
81 let post2 = this.schema.posts.new({ name: "Ipsum" });
82
83 user.things = [post1, post2];
84
85 return [user, [post1, post2]];
86 }
87
88 newParentNoChildren() {
89 let user = this.schema.users.new({ name: "Link" });
90
91 return [user, []];
92 }
93
94 newParentNewChildren() {
95 let user = this.schema.users.new({ name: "Link" });
96 let post1 = this.schema.posts.new({ title: "Lorem" });
97 let post2 = this.schema.posts.new({ title: "Ipsum" });
98
99 user.things = [post1, post2];
100
101 return [user, [post1, post2]];
102 }
103
104 newParentSavedChildren() {
105 let user = this.schema.users.new({ name: "Link" });
106 let post1 = this.schema.posts.create({ title: "Lorem" });
107 let post2 = this.schema.posts.create({ title: "Ipsum" });
108
109 user.things = [post1, post2];
110
111 return [user, [post1, post2]];
112 }
113
114 newParentMixedChildren() {
115 let user = this.schema.users.new({ name: "Link" });
116 let post1 = this.schema.posts.create({ title: "Lorem" });
117 let post2 = this.schema.posts.new({ title: "Ipsum" });
118
119 user.things = [post1, post2];
120
121 return [user, [post1, post2]];
122 }
123
124 // Unassociated child models, used for setting tests
125 savedChild() {
126 let insertedPost = this.db.posts.insert({ title: "Lorem" });
127
128 return this.schema.posts.find(insertedPost.id);
129 }
130
131 newChild() {
132 return this.schema.posts.new({ title: "Lorem" });
133 }
134 }
135
136 export const states = [
137 "savedParentNoChildren",
138 "savedParentNewChildren",
139 "savedParentSavedChildren",
140 "savedParentMixedChildren",
141 "newParentNoChildren",
142 "newParentNewChildren",
143 "newParentSavedChildren",
144 "newParentMixedChildren",
145 ];