Debian Codegraph @

__tests__/external/shared/orm/belongs-to/1-basic/_helper.js

raw
1 import { Server, Model, belongsTo } from "miragejs";
2
3 /*
4 A model with a belongsTo association can be in six states
5 with respect to its association. This helper class
6 returns a child (and its association) in these various states.
7
8 The return value is an array of the form
9
10 [ child, parent ]
11
12 where the parent may be undefined.
13 */
14 export default class BelongsToHelper {
15 constructor() {
16 this.server = new Server({
17 environment: "test",
18 models: {
19 author: Model.extend(),
20 post: Model.extend({
21 author: belongsTo(),
22 }),
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 savedChildNoParent() {
35 let insertedPost = this.db.posts.insert({ title: "Lorem" });
36
37 return [this.schema.posts.find(insertedPost.id), undefined];
38 }
39
40 savedChildNewParent() {
41 let insertedPost = this.db.posts.insert({ title: "Lorem" });
42 let post = this.schema.posts.find(insertedPost.id);
43 let author = this.schema.authors.new({ name: "Bob" });
44
45 post.author = author;
46
47 return [post, author];
48 }
49
50 savedChildSavedParent() {
51 let insertedAuthor = this.db.authors.insert({ name: "Bob" });
52 let insertedPost = this.db.posts.insert({
53 title: "Lorem",
54 authorId: insertedAuthor.id,
55 });
56 let post = this.schema.posts.find(insertedPost.id);
57 let author = this.schema.authors.find(insertedAuthor.id);
58
59 return [post, author];
60 }
61
62 newChildNoParent() {
63 return [this.schema.posts.new({ title: "Lorem" }), undefined];
64 }
65
66 newChildNewParent() {
67 let post = this.schema.posts.new({ title: "Lorem" });
68 let newAuthor = this.schema.authors.new({ name: "Bob" });
69 post.author = newAuthor;
70
71 return [post, newAuthor];
72 }
73
74 newChildSavedParent() {
75 let insertedAuthor = this.db.authors.insert({ name: "Bob" });
76 let post = this.schema.posts.new({ title: "Lorem" });
77 let savedAuthor = this.schema.authors.find(insertedAuthor.id);
78
79 post.author = savedAuthor;
80
81 return [post, savedAuthor];
82 }
83
84 // Just a saved unassociated parent.
85 savedParent() {
86 let insertedAuthor = this.db.authors.insert({ name: "Bob" });
87
88 return this.schema.authors.find(insertedAuthor.id);
89 }
90
91 newParent() {
92 return this.schema.authors.new({ name: "Bob" });
93 }
94 }
95
96 export const states = [
97 "savedChildNoParent",
98 "savedChildNewParent",
99 "savedChildSavedParent",
100 "newChildNoParent",
101 "newChildNewParent",
102 "newChildSavedParent",
103 ];