Debian Codegraph @

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