Debian Codegraph @

__tests__/internal/integration/schema/schema-verification/belongs-to-test.js

raw
1 import "@lib/container";
2 import Db from "@lib/db";
3 import Schema from "@lib/orm/schema";
4 import { Model, belongsTo } from "miragejs";
5
6 describe("Internal | Integration | Schema | Schema Verification | Belongs To", function () {
7 test("a one-way belongsTo association is correct", () => {
8 let schema = new Schema(
9 new Db({
10 authors: [{ id: 1, name: "Frodo" }],
11 posts: [{ id: 1, title: "Lorem ipsum" }],
12 }),
13 {
14 author: Model.extend(),
15 post: Model.extend({
16 author: belongsTo(),
17 }),
18 }
19 );
20
21 let post = schema.posts.find(1);
22 let association = post.associationFor("author");
23 let frodo = schema.authors.find(1);
24
25 expect(association.name).toEqual("author");
26 expect(association.modelName).toEqual("author");
27 expect(association.ownerModelName).toEqual("post");
28 expect(frodo.inverseFor(association) === null).toBeTruthy();
29 });
30
31 test("a one-way named belongsTo association is correct", () => {
32 let schema = new Schema(
33 new Db({
34 users: [{ id: 1, name: "Frodo" }],
35 posts: [{ id: 1, title: "Lorem ipsum" }],
36 }),
37 {
38 user: Model.extend(),
39 post: Model.extend({
40 author: belongsTo("user"),
41 }),
42 }
43 );
44
45 let post = schema.posts.find(1);
46 let association = post.associationFor("author");
47 let frodo = schema.users.find(1);
48
49 expect(association.name).toEqual("author");
50 expect(association.modelName).toEqual("user");
51 expect(association.ownerModelName).toEqual("post");
52 expect(frodo.inverseFor(association) === null).toBeTruthy();
53 });
54
55 test("a reflexive belongsTo association is correct and has an implicit inverse", () => {
56 let schema = new Schema(
57 new Db({
58 users: [{ id: 1, name: "Frodo" }],
59 }),
60 {
61 user: Model.extend({
62 user: belongsTo(),
63 }),
64 }
65 );
66
67 let frodo = schema.users.find(1);
68 let association = frodo.associationFor("user");
69
70 expect(association.name).toEqual("user");
71 expect(association.modelName).toEqual("user");
72 expect(association.ownerModelName).toEqual("user");
73 expect(frodo.inverseFor(association) === association).toBeTruthy();
74 });
75
76 test("a named reflexive belongsTo association with an implicit inverse is correct", () => {
77 let schema = new Schema(
78 new Db({
79 users: [{ id: 1, name: "Frodo" }],
80 }),
81 {
82 user: Model.extend({
83 bestFriend: belongsTo("user"),
84 }),
85 }
86 );
87
88 let frodo = schema.users.find(1);
89 let association = frodo.associationFor("bestFriend");
90
91 expect(association.name).toEqual("bestFriend");
92 expect(association.modelName).toEqual("user");
93 expect(association.ownerModelName).toEqual("user");
94 expect(frodo.inverseFor(association) === association).toBeTruthy();
95 });
96
97 test("a named reflexive belongsTo association with an explicit inverse is correct", () => {
98 let schema = new Schema(
99 new Db({
100 users: [{ id: 1, name: "Frodo" }],
101 }),
102 {
103 user: Model.extend({
104 bestFriend: belongsTo("user", { inverse: "bestFriend" }),
105 }),
106 }
107 );
108
109 let frodo = schema.users.find(1);
110 let association = frodo.associationFor("bestFriend");
111
112 expect(association.name).toEqual("bestFriend");
113 expect(association.modelName).toEqual("user");
114 expect(association.ownerModelName).toEqual("user");
115 expect(frodo.inverseFor(association) === association).toBeTruthy();
116 });
117
118 test("a one-way reflexive belongsTo association with a null inverse is correct", () => {
119 let schema = new Schema(
120 new Db({
121 users: [{ id: 1, name: "Frodo" }],
122 }),
123 {
124 user: Model.extend({
125 user: belongsTo("user", { inverse: null }),
126 }),
127 }
128 );
129
130 let frodo = schema.users.find(1);
131 let association = frodo.associationFor("user");
132
133 expect(association.name).toEqual("user");
134 expect(association.modelName).toEqual("user");
135 expect(association.ownerModelName).toEqual("user");
136 expect(frodo.inverseFor(association) === null).toBeTruthy();
137 });
138
139 test("a named one-way way reflexive belongsTo association with a null inverse is correct", () => {
140 let schema = new Schema(
141 new Db({
142 users: [{ id: 1, name: "Frodo" }],
143 }),
144 {
145 user: Model.extend({
146 parent: belongsTo("user", { inverse: null }),
147 }),
148 }
149 );
150
151 let frodo = schema.users.find(1);
152 let association = frodo.associationFor("parent");
153
154 expect(association.name).toEqual("parent");
155 expect(association.modelName).toEqual("user");
156 expect(association.ownerModelName).toEqual("user");
157 expect(frodo.inverseFor(association) === null).toBeTruthy();
158 });
159
160 test("a one-to-one belongsTo association with an implicit inverse is correct", () => {
161 let schema = new Schema(
162 new Db({
163 users: [{ id: 1, name: "Frodo" }],
164 profiles: [{ id: 1, type: "Admin" }],
165 }),
166 {
167 user: Model.extend({
168 profile: belongsTo(),
169 }),
170 profile: Model.extend({
171 user: belongsTo(),
172 }),
173 }
174 );
175
176 let admin = schema.profiles.find(1);
177 let association = admin.associationFor("user");
178
179 expect(association.name).toEqual("user");
180 expect(association.modelName).toEqual("user");
181 expect(association.ownerModelName).toEqual("profile");
182
183 let frodo = schema.users.find(1);
184 let inverse = frodo.inverseFor(association);
185
186 expect(inverse.name).toEqual("profile");
187 expect(inverse.modelName).toEqual("profile");
188 expect(inverse.ownerModelName).toEqual("user");
189 });
190 });