Debian Codegraph @

__tests__/external/shared/orm/mixed/1-one-to-many/create-test.js

raw
1 import Helper from "./_helper";
2 import { Model } from "miragejs";
3
4 describe("External | Shared | ORM | Mixed | One To Many | 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 post = helper.schema.create("post");
16 let user = helper.schema.create("user", {
17 postIds: [post.id],
18 });
19 post.reload();
20
21 expect(user.postIds).toEqual([post.id]);
22 expect(user.attrs.postIds).toEqual([post.id]);
23 expect(user.posts.includes(post)).toBeTruthy();
24 expect(post.user.attrs).toEqual(user.attrs);
25
26 let { db } = helper;
27 expect(db.posts).toHaveLength(1);
28 expect(db.posts[0]).toEqual({ id: "1", userId: "1" });
29 expect(db.users).toHaveLength(1);
30 expect(db.users[0]).toEqual({ id: "1", postIds: ["1"] });
31 });
32
33 test("it sets up associations correctly when passing in an array of models", () => {
34 let post = helper.schema.create("post");
35 let user = helper.schema.create("user", {
36 posts: [post],
37 });
38
39 expect(user.postIds).toEqual([post.id]);
40 expect(user.attrs.postIds).toEqual([post.id]);
41 expect(user.posts.includes(post)).toBeTruthy();
42 expect(post.user.attrs).toEqual(user.attrs);
43
44 let { db } = helper;
45 expect(db.posts).toHaveLength(1);
46 expect(db.posts[0]).toEqual({ id: "1", userId: "1" });
47 expect(db.users).toHaveLength(1);
48 expect(db.users[0]).toEqual({ id: "1", postIds: ["1"] });
49 });
50
51 test("it sets up associations correctly when passing in a collection", () => {
52 let post = helper.schema.create("post");
53 let user = helper.schema.create("user", {
54 posts: helper.schema.posts.all(),
55 });
56 post.reload();
57
58 expect(user.postIds).toEqual([post.id]);
59 expect(user.attrs.postIds).toEqual([post.id]);
60 expect(user.posts.includes(post)).toBeTruthy();
61
62 let { db } = helper;
63 expect(db.posts).toHaveLength(1);
64 expect(db.posts[0]).toEqual({ id: "1", userId: "1" });
65 expect(db.users).toHaveLength(1);
66 expect(db.users[0]).toEqual({ id: "1", postIds: ["1"] });
67 });
68
69 test("it throws an error if a model is passed in without a defined relationship", () => {
70 let { schema } = helper;
71
72 expect(function () {
73 schema.create("user", {
74 foo: schema.create("foo"),
75 });
76 }).toThrow();
77 });
78
79 test("it throws an error if an array of models is passed in without a defined relationship", () => {
80 let { schema } = helper;
81
82 expect(function () {
83 schema.create("user", {
84 foos: [schema.create("foo")],
85 });
86 }).toThrow();
87 });
88
89 test("it throws an error if a collection is passed in without a defined relationship", () => {
90 let { schema } = helper;
91 schema.create("foo");
92 schema.create("foo");
93
94 expect(function () {
95 schema.create("post", {
96 foos: schema.foos.all(),
97 });
98 }).toThrow();
99 });
100 });