Debian Codegraph @

__tests__/external/shared/orm/has-many/1-basic/create-test.js

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