Debian Codegraph @

__tests__/external/shared/orm/belongs-to/5-named-reflexive-explicit-inverse/create-test.js

raw
1 import Helper from "./_helper";
2 import { Model } from "miragejs";
3
4 describe("External | Shared | ORM | Belongs To | Named Reflexive Explicit Inverse | create", () => {
5 let helper;
6 beforeEach(() => {
7 helper = new Helper();
8 helper.schema.registerModel("foo", Model);
9 });
10 afterEach(() => {
11 helper.shutdown();
12 });
13
14 test("it sets up associations correctly when passing in the foreign key", () => {
15 let { schema } = helper;
16 let friend = schema.create("user");
17 let user = schema.create("user", {
18 bestFriendId: friend.id,
19 });
20
21 friend.reload();
22
23 expect(user.bestFriendId).toEqual(friend.id);
24 expect(user.bestFriend.attrs).toEqual(friend.attrs);
25 expect(schema.db.users).toHaveLength(2);
26 expect(schema.db.users[0]).toEqual({ id: "1", bestFriendId: "2" });
27 expect(schema.db.users[1]).toEqual({ id: "2", bestFriendId: "1" });
28 });
29
30 test("it sets up associations correctly when passing in the association itself", () => {
31 let { schema } = helper;
32 let friend = schema.create("user");
33 let user = schema.create("user", {
34 bestFriend: friend,
35 });
36
37 expect(user.bestFriendId).toEqual(friend.id);
38 expect(user.bestFriend.attrs).toEqual(friend.attrs);
39 expect(schema.db.users).toHaveLength(2);
40 expect(schema.db.users[0]).toEqual({ id: "1", bestFriendId: "2" });
41 expect(schema.db.users[1]).toEqual({ id: "2", bestFriendId: "1" });
42 });
43
44 test("it throws an error if a model is passed in without a defined relationship", () => {
45 let { schema } = helper;
46
47 expect(function () {
48 schema.create("user", {
49 foo: schema.create("foo"),
50 });
51 }).toThrow();
52 });
53
54 test("it throws an error if a collection is passed in without a defined relationship", () => {
55 let { schema } = helper;
56 schema.create("foo");
57 schema.create("foo");
58
59 expect(function () {
60 schema.create("user", {
61 foos: schema.foos.all(),
62 });
63 }).toThrow();
64 });
65 });