Debian Codegraph @

__tests__/external/shared/orm/mixed/1-one-to-many/_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 posts: hasMany(),
21 }),
22 post: 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 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.posts = [post1, post2];
48
49 return [user, [post1, post2]];
50 }
51
52 savedParentSavedChildren() {
53 let { schema } = this;
54 schema.db.loadData({
55 users: [{ id: "1", name: "Link", postIds: ["1", "2"] }],
56 posts: [
57 { id: "1", title: "Lorem", userId: "1" },
58 { id: "2", title: "Ipsum", userId: "1" },
59 ],
60 });
61
62 return [schema.users.find(1), [schema.posts.find(1), schema.posts.find(2)]];
63 }
64
65 savedParentMixedChildren() {
66 this.schema.db.loadData({
67 users: [{ id: "1", name: "Link", postIds: ["1"] }],
68 posts: [{ id: "1", title: "Lorem", userId: "1" }],
69 });
70 let user = this.schema.users.find(1);
71 let post1 = this.schema.posts.find(1);
72 let post2 = this.schema.posts.new({ name: "Ipsum" });
73
74 user.posts = [post1, post2];
75
76 return [user, [post1, post2]];
77 }
78
79 newParentNoChildren() {
80 let user = this.schema.users.new({ name: "Link" });
81
82 return [user, []];
83 }
84
85 newParentNewChildren() {
86 let user = this.schema.users.new({ name: "Link" });
87 let post1 = this.schema.posts.new({ title: "Lorem" });
88 let post2 = this.schema.posts.new({ title: "Ipsum" });
89
90 user.posts = [post1, post2];
91
92 return [user, [post1, post2]];
93 }
94
95 newParentSavedChildren() {
96 let user = this.schema.users.new({ name: "Link" });
97 let post1 = this.schema.posts.create({ title: "Lorem" });
98 let post2 = this.schema.posts.create({ title: "Ipsum" });
99
100 user.posts = [post1, post2];
101
102 return [user, [post1, post2]];
103 }
104
105 newParentMixedChildren() {
106 let user = this.schema.users.new({ name: "Link" });
107 let post1 = this.schema.posts.create({ title: "Lorem" });
108 let post2 = this.schema.posts.new({ title: "Ipsum" });
109
110 user.posts = [post1, post2];
111
112 return [user, [post1, post2]];
113 }
114
115 // Unassociated child models, used for setting tests
116 savedChild() {
117 let insertedPost = this.db.posts.insert({ title: "Lorem" });
118
119 return this.schema.posts.find(insertedPost.id);
120 }
121
122 newChild() {
123 return this.schema.posts.new({ title: "Lorem" });
124 }
125 }
126
127 export const states = [
128 "savedParentNoChildren",
129 "savedParentNewChildren",
130 "savedParentSavedChildren",
131 "savedParentMixedChildren",
132 "newParentNoChildren",
133 "newParentNewChildren",
134 "newParentSavedChildren",
135 "newParentMixedChildren",
136 ];