Debian Codegraph @

__tests__/external/shared/orm/has-many/2-named/_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 user: Model.extend({
20 blogPosts: hasMany("post"),
21 }),
22 post: Model,
23 },
24 });
25
26 this.db = this.server.db;
27 this.schema = this.server.schema;
28 }
29
30 shutdown() {
31 this.server.shutdown();
32 }
33
34 savedParentNoChildren() {
35 let user = this.db.users.insert({ name: "Link" });
36
37 return [this.schema.users.find(user.id), []];
38 }
39
40 savedParentNewChildren() {
41 let user = this.schema.users.create({ name: "Link" });
42 let post1 = this.schema.posts.new({ title: "Lorem" });
43 let post2 = this.schema.posts.new({ title: "Ipsum" });
44
45 user.blogPosts = [post1, post2];
46
47 return [user, [post1, post2]];
48 }
49
50 savedParentSavedChildren() {
51 let user = this.schema.users.create({ name: "Link" });
52 let post1 = this.schema.posts.create({ title: "Lorem" });
53 let post2 = this.schema.posts.create({ title: "Ipsum" });
54
55 user.blogPosts = [post1, post2];
56
57 return [user, [post1, post2]];
58 }
59
60 savedParentMixedChildren() {
61 let user = this.schema.users.create({ name: "Link" });
62 let post1 = this.schema.posts.create({ title: "Lorem" });
63 let post2 = this.schema.posts.new({ title: "Ipsum" });
64
65 user.blogPosts = [post1, post2];
66
67 return [user, [post1, post2]];
68 }
69
70 newParentNoChildren() {
71 let user = this.schema.users.new({ name: "Link" });
72
73 return [user, []];
74 }
75
76 newParentNewChildren() {
77 let user = this.schema.users.new({ name: "Link" });
78 let post1 = this.schema.posts.new({ title: "Lorem" });
79 let post2 = this.schema.posts.new({ title: "Ipsum" });
80
81 user.blogPosts = [post1, post2];
82
83 return [user, [post1, post2]];
84 }
85
86 newParentSavedChildren() {
87 let user = this.schema.users.new({ name: "Link" });
88 let post1 = this.schema.posts.create({ title: "Lorem" });
89 let post2 = this.schema.posts.create({ title: "Ipsum" });
90
91 user.blogPosts = [post1, post2];
92
93 return [user, [post1, post2]];
94 }
95
96 newParentMixedChildren() {
97 let user = this.schema.users.new({ name: "Link" });
98 let post1 = this.schema.posts.create({ title: "Lorem" });
99 let post2 = this.schema.posts.new({ title: "Ipsum" });
100
101 user.blogPosts = [post1, post2];
102
103 return [user, [post1, post2]];
104 }
105
106 // Unassociated child models, used for setting tests
107 savedChild() {
108 let insertedPost = this.db.posts.insert({ title: "Lorem" });
109
110 return this.schema.posts.find(insertedPost.id);
111 }
112
113 newChild() {
114 return this.schema.posts.new({ title: "Lorem" });
115 }
116 }
117
118 export const states = [
119 "savedParentNoChildren",
120 "savedParentNewChildren",
121 "savedParentSavedChildren",
122 "savedParentMixedChildren",
123 "newParentNoChildren",
124 "newParentNewChildren",
125 "newParentSavedChildren",
126 "newParentMixedChildren",
127 ];