Debian Codegraph @

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